Assignment 4 : Loop Control Structure Continued...                             Date : 29th  Jan, 07


Tutorial  4A:

Write a C program to check whether 3 given line  intersect each other at the same point.

Solution:

int main(){

int x0,y0,x1,y1; //Variables used to store Coordinates of line 1

int p0,q0,p1,q1; //Variable used to store Coordinates of line 2

int u0,v0,u1,v1; //Variable used to store Coordinates of line 3

int xi,yi; //Stores the point of intersection between line 1 and 2

int m1,m2,m3;// stores slope of line 1 2 and 3

int m4; // stores slope of the new line

 

int c1,c2; // y=m*x+c

printf("enter x,y coordinate for line 1\n");

scanf("%d%d%d%d",&x0,&y0,&x1,&y1);

printf("enter x,y coordinate for line  2\n");

scanf("%d%d%d%d",&p0,&q0,&p1,&q1);

printf("enter x,y coordinate for line  3\n");

scanf("%d%d%d%d",&u0,&v0,&u1,&v1);

// calculate slope of line 1, 2 & 3

if((x1-x0)==0)

m1=NaN;

else

m1=(y1-y0)/(x1-x0); //slope of line 1

if((p1-p0)==0)

m2=NaN;

else

m2=(q1-q0)/(p1-p0); //slope of line 2

if((u1-u0)==0)

m3=NaN;

else

m3=(v1-v0)/(u1-u0); //slope of line 3

// Slope calculation ends here

c1=y0-m1*x0;

c2=p1-m2*p0;

//calculate the point of intersection

if(m1==m2)

{

printf("Line segments are parallel, So cannot Intersect at same point");

return 0;

}

else{

xi=-(c1-c2)/(m1-m2);

yi=c1+m1*xi;

}

//point of intersection calculation ends here

m4=(yi-v0)/(xi-u0); //calculate slope of the line segment (u0,v0) and (xi,yi)

//if m4==m3 then the three lines intersect at the same point

if(m3==m4)

printf("\nThe Three Line intersect at the same point\n");

else

printf("The Three Line does not intersect at the same point\n");

}

/*Try with inputs like

a)(0,0),(4,4)

(0,2),(0,-2)

(-2,2),(2,-2)

b)(-1,-2),(4,5)

(-2,2),(-2,-2)

(0,2),(0,-2)

*/


Assign. 4A:

Write a C program to check whether 3 given points are collinear.

Example:

INPUT

OUTPUT

(0,0) , (1,1) , (2,2)

Collinear

(0,0) , (2,2), (3,0)

Noncollinear


Tutorial 4B:

    Write a C program to check whether a given integer number is prime.

Solution:

main()

{

int a,c=0,i,n;

printf("enter the number to be checked");

scanf("%d",&n);

for(i=2;i<=sqrt(n);i++)

{

a=n%i;

if(a==0)

c=c+1;

}

if (c>=1)

printf("the given number is not prime");

else

printf("the given number is prime");

}



Assignment 4B:

    Write a C program to check whether two given integer numbers are relatively prime.

Hint: Two integer numbers (a,b) are relatively prime if gcd(a,b)=1



Assign. 4C:

Write a C program that reads three integers Day, Month and Year and outputs date of the next day. For example :

INPUT

OUTPUT

28 2 2006

 1/3/2006

28 2 2004

29/2/2004

31 12 2006

1/1/2007



Assign 4D:

Problem 4:

A rational number, p/q , where p, q are non-negative integers and q != 0, may be expressed as a continued fraction. Following is an example for 61/27 .

                                                    

This may be represented in a compact form as (2,3,1,6)

 

Write a C program to read the numerator and the denominator (q!= 0) of a rational number (both non-negative) and print it as a continued fraction in compact form. You may use the following algorithm. 

Do not use any array.

Do the following at each steps in a loop :

(a) Find the quotient and the remainder of (numerator ÷ denominator).

(b) Print the quotient as an element of the continued fraction.

(c) Calculate the new numerator and the new denominator.

Until Remainder becomes Zero.

Example: