Вы находитесь на странице: 1из 36

2018

Name:
SAMARPAN
CHAKRAVARTY

Class: BCSE –
UG1

Section: A 2

Roll no.:
001810501026

[CPNA LAB ASSIGNMENTS]


ALGORITHMS
1. Write an algorithm to determine maximum of three numbers.

Step 1: START

Step 2: Declare variables a,b and c.

Step 3: Read variables a,b and c.

Step 4: If a>b

If a>c

Display a is the largest number.

Else

Display c is the largest number.

Else

If b>c

Display b is the largest number.

Else

Display c is the greatest number.

Step 5: STOP
2. Write an algorithm to determine the sum of individual digits of a given integer.
Step 1: START

Step 2: Input N

Step 3: Sum = 0

Step 4: While (N != 0)

Rem = N % 10

Sum = Sum + Rem

N = N / 10

Step 5: End while

Step 6: Print Sum

Step 7: STOP

3. Write an algorithm to print the reverse of a number read as input.

Step 1: START

Step 2: Input N

Step 3: rev=0

Step 4: while(N!=0)

Rem = N % 10

rev = rev * 10 + Rem

N = N / 10

Step 5: End while

Step 6: Print rev

Step 7: STOP
4. Write an algorithm to determine whether a number is prime or not.
Step 1: START

Step 2: Input N

Step 3: Set flag=0

Step 4: for i=2 to N/2 in steps of 1 do

Step 5: If N % i = 0 then

Step 6: Update flag=1 and break for loop

Step 7: If flag = 0 then

Step 8: Print ‘The number is prime’

Step 9: Else Print ‘The number is not prime’

Step 10:STOP

5. Write an algorithm to generate the first 100 prime numbers.


Step 1: START

Step 2: Set N = 2 and c = 0

Step 3: while ( 1 )

Step 4: Set flag = 0

Step 5: for i=2 to N/2 in steps of 1 do

Step 6: if N % i = 0 then

Step 7: Update flag = 1 and break for loop

Step 8: If flag = 0 then

Step 9: Print N and update c = c+1

Step 10: Update N = N+1

Step 11: If c = 0 then break while loop

Step 12: STOP


6. Write an algorithm to input 10 numbers, sort them in ascending order and to display them.
Step 1: START

Step 2: Declare int array A of size 10

Step 3: for i = 0 to 9 in steps of 1 do

Step 4: Input A [ i ]

Step 5: End of i for loop

Step 6: for i = 0 to 9 in steps of 1 do

Step 7: for j = 0 to 9-i in steps of 1 do

Step 8: If A [ j ] > A [ j+1 ] then

T=A[j]

A [ j ] = A [ j+1 ]

A [ j+1 ] = T

Step 9: End of j for loop

Step 10: End of i for loop

Step 11: for i = 0 to 9 in steps of 1 do

Step 12: Print A [ i ]

Step 13: End of i for loop

Step 14: STOP


7. Draw a flowchart to input three numbers in the variables a, b and c and hence to find the roots
of the quadratic equation ax2 + bx + c = 0. Consider carefully the zero input values of the
coefficients a, b and c.

Step 1: START

Step 2: Declare A , B , C , D , X1 , X2 , rp , ip

Step 3: Input A ,B and C

Step 4: Calculate D = B2 – 4AC

Step 5: If D >= 0 then

X1 = ( - B + squareroot( D ) ) / 2 A

X2 = ( - B - squareroot( D ) ) / 2 A

Print X1 and X2

Step 6: Else

Calculate real and imaginary parts

rp = B / 2 A

ip = squareroot( - D ) / 2 A

X1 = rp + i ( ip )

X2 = rp – i ( ip )

Print X1 and X2

Step 7: STOP
CONSOLE I/O AND
CONDITIONAL STATEMENTS
1. A cashier has currency notes of denominations 10, 50 and 100. Write a C program which accepts
an amount to be withdrawn, and prints the total number of currency notes of each denomination the
cashier will have to give to the withdrawer.

#include<stdio.h>
int main()
{
printf("ENTER THE AMOUNT OF MONEY TO BE WITHDRAWN : ");
int amount;
scanf("%d",&amount);
int no_of_notes100 = 0, no_of_notes50 = 0, no_of_notes10 = 0;
if (amount >= 100)
{
while (amount >= 100)
{
no_of_notes100++;
amount -= 100;
}
}
if (amount >= 50)
{
while (amount >=50)
{
no_of_notes50++;
amount -= 50;
}
}
if (amount >= 10)
{
while (amount >=10)
{
no_of_notes10++;
amount-=10;
}
}
printf("THE NUMBER OF 100 DENOMINATION NOTES = %d\n", no_of_notes100);
printf("THE NUMBER OF 50 DENOMINATION NOTES = %d\n", no_of_notes50);
printf("THE NUMBER OF 10 DENOMINATION NOTES = %d\n", no_of_notes10);
return 0;
}
OUTPUT::

2. If the marks obtained by a student in five different subjects are input through the keyboard, find
out the aggregate marks and percentage marks obtained by the student. Assume that the maximum
marks that can be obtained by a student in each subject is 100. Input error should be
checked.

#include<stdio.h>
int main()
{
int marks1=0,marks2=0,marks3=0,marks4=0,marks5=0;
int aggregate_marks = 0;
float percentage_marks = 0.0;
printf("ENTER THE MARKS ENTERED BY THE STUDENTS IN 5 SUBJECTS\n");
printf("ENTER THE MARKS IN SUBJECT 1 : ");
scanf("%d", &marks1);
if (marks1 > 100 || marks1 < 0)
{
printf("WRONG INPUT\n");
return 0;
}
printf("ENTER THE MARKS IN SUBJECT 2 : ");
scanf("%d", &marks2);
if (marks2 > 100 || marks2 < 0)
{
printf("WRONG INPUT\n");
return 0;
}
printf("ENTER THE MARKS IN SUBJECT 3 : ");
scanf("%d", &marks3);
if (marks3 > 100 || marks3 < 0)
{
printf("WRONG INPUT\n");
return 0;
}

printf("ENTER THE MARKS IN SUBJECT 4 : ");


scanf("%d", &marks4);
if (marks4 > 100 || marks4 < 0)
{
printf("WRONG INPUT\n");
return 0;
}
printf("ENTER THE MARKS IN SUBJECT 5 : ");
scanf("%d", &marks5);
if (marks5 > 100 || marks5 < 0)
{
printf("WRONG INPUT\n");
return 0;
}
aggregate_marks = marks1 + marks2 + marks3 + marks4 + marks5;
percentage_marks = ((float)aggregate_marks / 500) * 100;
printf("THE AGGREGATE MARKS = %d\n", aggregate_marks);
printf("THE PERCENTAGE MARKS = %3f\n", percentage_marks);
return 0;
}
OUTPUT::

3. In a town, the percentage of men is 52. The percentage of total literacy is 48. If total percentage
of literate men is 35 of the total population, write a program to find the total number of illiterate
men and women if the population of the town is 80,000.

#include<stdio.h>
#define population 80000

int main()
{
int no_of_men = (52 * population) / 100;
int no_of_women = population - no_of_men;
int total_literate = (48 * population) / 100;
int literate_men = (35 * population)/100;
int literate_women = total_literate - literate_men;
int illiterate_men = no_of_men - literate_men;
int illiterate_women = no_of_women - literate_women;
printf("TOTAL NUMBER OF ILLITERATE MEN = %d\n", illiterate_men);
printf("TOTAL NUMBER OF ILLITERATE WOMEN = %d\n", illiterate_women);
printf("TOTAL NUMBER OF ILLITERATE PEOPLE = %d\n", illiterate_men +
illiterate_women);
return 0;
}
OUTPUT::

4. If a five-digit integer is input through the keyboard, write a program to print a new number by
adding one to each of its digits. For example if the number that is input is 12391 then the output
should be displayed as 23402.

#include<stdio.h>

int main()
{
char q[80];
printf("Enter the integer: ");
gets(q);
int i=0;
printf("The required output is: ");
for(i=0;q[i]!='\0';i++)
{
if(q[i]==57)
printf("0");
else
printf("%c",q[i]+1);
}
printf("\n");
return 0;
}

OUTPUT::
5. Write a program to check whether a triangle is valid or not, when (i) the three angles of the
triangle are entered through the Keyboard (ii) three sides of the triangle are entered through the
keyboard.

#include<stdio.h>
void sort(float A[])
{
int i=0,j=0;
float t=0;
for(i=0;i<3;i++)
{
for(j=0;j<2-i;j++)
{
if(A[j]>A[j+1])
{
t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}
}
}
}
int main()
{
int c=0,i=0;
float A[3];
float sum=0;
printf("1....Check with 3 angles\n");
printf("2....Check with three sides\n");
printf("Enter your choice: ");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter the three angles: ");
for(i=0;i<3;i++)
scanf("%f",&A[i]);
if(A[0]+A[1]+A[2]==180)
printf("Its a valid triangle\n");
else
printf("Its not a valid triangle\n");
break;
case 2:
printf("Enter the three sides: ");
for(i=0;i<3;i++)
{
scanf("%f",&A[i]);
}
sort(A);
sum=A[0]+A[1];
if(sum>A[2])
printf("Its a valid triangle\n");
else
printf("Its not a valid triangle\n");
break;
default: printf("WRONG CHOICE");
}
return 0;
}
OUTPUT::

6. Given the coordinates (x, y) of a center of a circle and its radius, write a program which will
determine whether a point lies inside the circle, on the circle or outside the circle.

#include <stdio.h>
#include <math.h>

float circle(float x, float y, float r, float xc, float yc)


{
float val = pow(x - xc,2) + pow(y - yc,2) - pow(r,2);
return val;
}
int main()
{
float xc, yc, xp, yp, r;
printf("ENTER THE X - COORDINATE OF CENTER : ");
scanf("%f", &xc);
printf("ENTER THE Y - COORDINATE OF CENTER : ");
scanf("%f", &yc);
printf("ENTER THR RADIUS OF THE CIRCLE : ");
scanf("%f", &r);
printf("ENTER THE X - COORDINATE OF POINT : ");
scanf("%f", &xp);
printf("ENTER THE Y - COORDINATE OF POINT : ");
scanf("%f", &yp);
float pos = circle(xp, yp, r, xc, yc);
if (pos > 0)
printf("RESULT : OUTSIDE THE CIRCLE\n");
if (pos < 0)
printf("RESULT : INSIDE THE CIRCLE\n");
if (pos == 0)
printf("RESULT : ON THE CIRCLE\n");
return 0;
}

OUTPUT::

7. Any character is entered through the keyboard, write a program to determine whether the
character entered is a capital letter, a small case letter, a digit or a special symbol.

#include <stdio.h>

int main()
{
char ch;
printf("Enter character : ");
scanf("%c",&ch);
if(ch>=65&&ch<=90)
printf("It is a capital letter\n");
else if(ch>=97&&ch<=122)
printf("It is a small letter\n");
else if(ch>=48&&ch<=57)
printf("It is a digit\n");
else
printf("It is a special character\n");
return 0;
}
OUTPUT::
8. Given as input an integer number of seconds, write a program to print as output the equivalent
time in hours, minutes and seconds. Recommended output format is something like 7322
seconds is equivalent to 2 hours 2 minutes 2 seconds.

#include <stdio.h>
int main(void)
{
int seconds;
printf("ENTER THE NUMBER OF SECONDS : ");
scanf("%d",&seconds);
int hours,minutes;
hours= seconds / 3600;
seconds = seconds % 3600;
minutes = seconds / 60;
seconds = seconds % 60;
printf("TIME : ");
if(hours != 0)
printf("%d HOURS ",hours);
if(minutes != 0)
printf("%d MINUTES ",minutes);
if(seconds!=0)
printf("%d SECONDS ", seconds);
if(seconds==0&&minutes==0&&hours==0)
printf("0 seconds");
printf("\n");
return 0;
}

OUTPUT::

9. Write a program which accepts two number X, Y and creates a third number Z by appending Y
after X. Example: if X=12 and Y=345 then Z=12345.

#include<stdio.h>
#include<math.h>

int main()
{
int x,y;
long int z;
printf("X = ");
scanf("%d", &x);
printf("Y = ");
scanf("%d", &y);
int digitsy = 0;
int suby = y;
while(suby!=0)
{
digitsy++;
suby = suby/10;
}
z = (x * (int)pow(10,digitsy)) + y;
printf("Z = %ld ", z);
printf("\n");
return 0;
}
OUTPUT::

10. A certain grade of steel is graded according to the following conditions:


Hardness must be greater than 50
Carbon content must be less than 0.7
Tensile strength must be greater than 5600
The grades are as follows:
Grade is 10 if all three conditions are met
Grade is 9 if conditions (i) and (ii) are met
Grade is 8 if conditions (ii) and (iii) are met
Grade is 7 if conditions (i) and (iii) are met
Grade is 6 if only one condition is met
Grade is 5 if none of the conditions are met
Write a program, which will require the user to give values of hardness, carbon content and
tensile strength of the steel under consideration and output the grade of the steel.

#include <stdio.h>

int main()
{
float hardness,cc,ts;
printf("Hardness : ");
scanf("%f",&hardness);
printf("Carbon content : ");
scanf("%f",&cc);
printf("Tensile Strength : ");
scanf("%f",&ts);
int grade;
if(hardness>50&&cc<0.7&&ts>5600)
grade=10;
else if(hardness>50&&cc<.7)
grade=9;
else if(cc<.7&&ts>5600)
grade=8;
else if(hardness>50&&ts>5600)
grade=7;
else if(hardness>50||cc<0.7||ts>5600)
grade=6;
else
grade=5;
printf("The grade of iron is %d ",grade);
printf("\n");
return 0;
}

OUTPUT::
LOOPS
1. Write a C program which prints all prime numbers between 1 and 100.

#include<stdio.h>
#include<math.h>

int isprime(int n)
{
int j=0;
for(j=2;j<=sqrt(n);j++)
{
if(n%j==0)
return 0;
}
return 1;
}

int main()
{
int i=0;
printf("The prime numbers between 1 and 100 are :\n");
for(i=2;i<=100;i++)
{
if(isprime(i))
printf("%d\n",i);
}
return 0;
}
OUTPUT::

2. Write a C program to print the multiplication table of the number entered by the user. The table
should get displayed in the following form.
29 * 1 = 29
29 * 2 = 58
... ... ...

#include<stdio.h>

int main()
{
int n=0,i=0;
printf("Enter the number :");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf("%d * %d = %d\n",n,i,n*i);
}

return 0;
}

OUTPUT::
3. Write a C program to print the first n numbers of the Fibonacci sequence. The Fibonacci
sequence is constructed by adding the last two numbers of the sequence so far to get the next
number in the sequence. The first and second numbers of the sequence are defined as 0 and 1.
We get:
0, 1, 1, 2, 3, 5, 8, 13, 21…

#include<stdio.h>

int main()
{
int n=0,i=0,a=0,b=1,s=0;
printf("Enter the number of terms :");
scanf("%d",&n);

s=a+b;
printf("%d\n",a);

if(n==1)
return 0;

printf("%d\n",b);

if(n==2)
return 0;

n-=2;

for(i=1;i<=n;i++)
{
printf("%d\n",s);
a=b;
b=s;
s=a+b;
}

return 0;
}

OUTPUT::
4. Write a program to print all the ASCII values and their equivalent characters using a while loop.
The ASCII values vary from 0 to 255.

#include<stdio.h>

int main()
{
int i=0;
for(i=0;i<=255;i++)
{
printf("%d %c\n",i,char(i));
}

return 0;
}
OUTPUT::

5. Write a program to add first seven terms of the following series using a for loop:
1/1!+2/2!+3/3!+….

#include<stdio.h>

int fact(int n)
{
int f=1;
for(int i=1;i<=n;i++)
{
f*=i;
}
return f;
}

int main()
{
int i=1;
float sum=0;
for(i=1;i<=7;i++)
{
sum+=((i*1.0f)/(fact(i)*1.0f));
}
printf("The sum is = %f",sum);
return 0;
}

OUTPUT::
ARRAYS
1. Write a C program which accepts a matrix and prints its transpose.
#include<stdio.h>

int main()
{
int r=0,c=0,i=0,j=0;
printf("Enter the number of rows: ");
scanf("%d",&r);
printf("Enter the number of columns: ");
scanf("%d",&c);
int A[r][c];
printf("Enter the matrix:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("\nThe transpose of it is: \n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d ",A[j][i]);
}
printf("\n");
}
return 0;
}

OUTPUT::
2. Write a C program to replace a square matrix by its transpose without using a second matrix.
#include<stdio.h>

int main()
{
int n=0,i=0,j=0,t=0;

printf("Enter the order of the square matrix: ");


scanf("%d",&n);

int A[n][n];

printf("Enter the matrix: \n");

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&A[i][j]);
}
}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
{
t=A[i][j];
A[i][j]=A[j][i];
A[j][i]=t;
}
}
}

printf("\nThe updated matrix is: \n");

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}

return 0;
}
OUTPUT::
3. Write a C program which accepts ten integers from user and prints them in ascending order. Use
array to store the integers.

#include<stdio.h>

int main()
{
int A[10];
int i=0,j=0,t=0;
printf("Enter 10 integers: ");
for(i=0;i<10;i++)
{
scanf("%d",&A[i]);
}
for(i=0;i<10;i++)
{
for(j=0;j<9-i;j++)
{
if(A[j]>A[j+1])
{
t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}
}
}
printf("The integers in ascending order: ");
for(i=0;i<10;i++)
{
printf("%d ",A[i]);
}
return 0;
}

OUTPUT:
FUNCTION AND POINTERS
1. Write a C program which accepts a string from user and counts the number of characters in the
string without using string library functions.

#include<stdio.h>

int main()
{
char s[200];
int l=0,c=0,i=0;
printf("Enter the string: ");
gets(s);
for(l=0;s[l]!='\0';l++);
for(i=0;i<l;i++)
{
if((s[i]>='A' && s[i]<='Z') || (s[i]>='a' && s[i]<='z') || (s[i]>='0' && s[i]<='9'))
c++;
}

printf("The number of characters in it is = %d",c);

return 0;
}

OUTPUT::
2. Write a C program which accepts a string from user and prints the reverse of the string without
using string library functions.

#include<stdio.h>
int main()
{
char s[200];
int i=0,l=0;

printf("Enter the string: ");


gets(s);

for(l=0;s[l]!='\0';l++);

printf("The reverse is: ");

for(i=l-1;i>=0;i--)
printf("%c",s[i]);

printf("\n");

return 0;
}

OUTPUT::
3. Write a C program which accepts a full name from user prints the initials. Eg. SRT for Sachin
Ramesh Tendulkar.

#include<stdio.h>

int main()
{
int l=0,i=0;

char name[100];

printf("Enter the name: ");


gets(name);

for(l=0;name[l]!='\0';l++);

printf("\nThe initials are: %c",name[0]);

for(i=0;i<l;i++)
{
if(name[i]==' ')
{
i++;
printf("%c",name[i]);
}
}

return 0;
}

OUTPUT::
4. Write a C program which accepts any string of the form “Ustad Bade Ghulam Ali Khan was the
Tansen of the 20th century” and prints it as “Ustad|Bade|Ghulam|Ali| Khan|was|the|Tansen|
of|the|20th|century”.

#include<stdio.h>

int main()
{
char line[300];
int l=0,i=0;

printf("Enter the string: ");


gets(line);

for(l=0;line[l]!='\0';l++);

printf("\nThe required output: ");

for(i=0;i<l;i++)
{
if(line[i]==' ')
{
printf(" | ");
}
else
printf("%c",line[i]);

return 0;
}

OUTPUT::
5. Write a C program that reads a line and converts it into all capitals without using any string library
function. (input string may also contain capital letters).

#include<stdio.h>

int main()
{
char line[300];
int l=0,i=0;
printf("Enter the line: ");
gets(line);

for(l=0;line[l]!='\0';l++);

for(i=0;i<l;i++)
{
if(line[i]>=97 && line[i]<=122)
{
line[i]-=32;
}
}

printf("\nThe required output: \n");

for(i=0;i<l;i++)
{
printf("%c",line[i]);
}

return 0;
}

OUTPUT::
6. Write a program to count the number of occurrences of any two vowels in succession in a line
of text.

#include<stdio.h>
#define vow(i) s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='A' || s[i]=='E' || s[i]=='I' ||
s[i]=='O' || s[i]=='U'

int main()
{
char s[300];
int l=0,i=0;
printf("Enter the string: ");
gets(s);
for(l=0;s[l]!='\0';l++);
int c=0;
for(i=0;i<l;i++)
{
if(vow(i))
if(vow(i+1))
{
c++;
}
}

printf("No of consecutive vowel occurrences is = %d",c);

return 0;
}

OUTPUT::
7. Write a program that reads a line and delete from it all occurrences of the word “the”. Do not
use any string library function.
#include<stdio.h>

int main()
{
printf("Enter the string: ");
char s[200];
char s1[200];
gets(s);
int l=0,i=0,c=0;
for(l=0;s[l]!='\0';l++);

for(i=0;i<l;i++)
{
if((s[i]=='T' || s[i]=='t') && (s[i+1]=='H' || s[i+1]=='h') && (s[i+2]=='E' || s[i+2]=='e') &&
(s[i+3]==' '))
{
i+=2;
}

else
{
s1[c++]=s[i];
}

printf("\nThe required output after deleting \"the\" is: \n");


for(i=0;i<c;i++)
{
printf("%c",s1[i]);
}

return 0;
}

OUTPUT::
8. Write a program that converts a string like “123” to integer 123. Do not use any string library
function.

#include<stdio.h>
#include<math.h>
int main()
{

long long int ans=0,l=0,i=0,p=0;


printf("Enter the numeric string: ");
char num[200];
gets(num);

for(l=0;num[l]!='\0';l++);

p=l-1;
i=0;
ans=0;
while(p>=0)
{
ans=ans+(pow(10,p)*(num[i++]-48));

p--;

printf("The required integer is: %d",ans);

return 0;
}

OUTPUT::
9. Write a C program which accepts a string from user and checks whether it is palindrome or not.
Do not use any string library function. [Example of a palindrome string: "abcba", "abba"]

#include<stdio.h>
int check(char s[200],int l)
{
int i=0;
for(i=0;i<l;i++)
{
if(s[i]>=97 && s[i]<=122)
s[i]-=32;
}
for(i=0;i<l/2;i++)
{
if(s[i]!=s[l-1-i])
return 0;
}
return 1;
}
int main()
{
char s[200];
printf("Enter the string to be checked: ");
gets(s);
int l=0,i=0,flag=0;
for(l=0;s[l]!='\0';l++);
flag=check(s,l);
if(flag)
printf("Its a palindrome \n");
else
printf("Its not a palindrome \n");
return 0;
}

OUTPUT::
10. Write a function to compute the distance between two points and use it to develop another
function that will compute the area of the triangle whose vertices are A(x1, y1), B(x2, y2), and
C(x3, y3). Use these functions to develop a function which returns a value 1 if the point (x, y) lies
inside the triangle ABC, otherwise a value 0.

#include<stdio.h>
#include<math.h>

struct point
{ float x;
float y;
};

float computedist(struct point *p1,struct point *p2)


{
struct point point1 = *(p1);
struct point point2 = *(p2);
float part1=point1.x-point2.x;
float part2=point1.y-point2.y;
float distance=sqrt(part1*part1+part2*part2);
return distance;
}

float computearea(struct point *p1,struct point *p2, struct point *p3)


{
float dist1=computedist(p1,p2);
float dist2=computedist(p2,p3);
float dist3=computedist(p3,p1);
float semiperimeter=(dist1+dist2+dist3)/2;
float area2 = semiperimeter*(semiperimeter-dist1)*(semiperimeter-dist2)*(semiperimeter-
dist3);
float area = sqrt(area2);
return area;
}

int check(struct point *p1,struct point *p2,struct point *p3,struct point *p)
{
float area=computearea(p1,p2,p3);
float area2=computearea(p1,p2,p);
float area3=computearea(p2,p3,p);
float area1=computearea(p3,p1,p);
float areatot=area1+area2+area3;
if((area-areatot)<0.0001)
return 1;
else
return 0;
}
int main()
{
struct point p1;
struct point p2;
struct point p3;
struct point p;
int r=0;
printf("Enter the coordinates of point 1 ");
scanf("%f %f",&p1.x,&p1.y);
printf("Enter the coordinates of point 2 ");
scanf("%f %f",&p2.x,&p2.y);
printf("Enter the coordinates of point 3 ");
scanf("%f %f",&p3.x,&p3.y);
printf("Enter the coordinates of the point whose location you want to check ");
scanf("%f %f",&p.x,&p.y);
float area=computearea(&p1,&p2,&p3);
printf("Area of the 3 points: %f\n",area);
printf("Distance between points 1 and 2 is %f\n",computedist(&p1,&p2));
printf("Distance between points 2 and 3 is %f\n",computedist(&p2,&p3));
printf("Distance between points 3 and 1 is %f\n",computedist(&p3,&p1));
r=check(&p1,&p2,&p3,&p);
printf("\n");
if(r)
printf(“Its in the triangle\n”);
else
printf(“Its not in the triangle\n”);
return 0;
}

OUTPUT::

Вам также может понравиться