Professional Documents
Culture Documents
Write a program to read x, y coordinates of 3 points and then calculate the area of a
triangle formed by them and print the coordinates of the three points and the area of the
triangle. What will be the output from your program if the three given points are in a
straight line?
Flow Chart:
Step – 1 : Start
Step – 6 : Calculate Area = 0.5 * fabs (x1 * (y2 – y3)+ x2 * (y3 – y2) +x3 * (y1 – y2))
Step – 8 : If area = 0.
Print the points are collinear otherwise points are not collinear
Step – 9 : Stop
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int x1, y1, x2, y2, x3, y3;
float area;
clrscr();
printf("Enter the Coordinate \n");
printf("Enter 1st coordinate : \n");
scanf("%d,%d",&x1,&y1);
printf("Enter 2nd coordinate : \n");
scanf("%d,%d",&x2,&y2);
printf("Enter 3rd coordinate : \n");
scanf("%d,%d",&x3,&y3);
printf("The 1st coordinate : %d, %d\n",x1,y1);
printf("The 2nd coordinate : %d, %d\n",x2,y2);
printf("The 3rd coordinate : %d, %d\n",x3,y3);
area = 0.5 * fabs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 -
y2));
printf("area = %lf \n",area);
if(area == 0)
{
printf("The points are Collinear \n");
}
else
{
printf("The points are not Collinear \n");
}
getch();
return 0;
}
Run 1:
Run 2:
Flow Chart:
Step – 1 : Start
Step – 10 : Stop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
main()
{
int A[100], i, sum = 0;
float avg;
time_t *timer;
clrscr();
srand(time(timer));
printf("The random numbers from 1 to 100 are :\n");
for(i = 1; i <= 100; i++)
{
A[i] = 1 + rand() % 100;
printf("%d \t",A[i]);
sum += A[i];
avg = sum / 100.0;
}
printf("\nAverage of the random numbers = %.2f\n",avg);
getch();
return 0;
}
Output:
The random numbers from 1 to 100 are :
54 11 53 81 53 69 19 79 82 42
58 69 82 90 29 17 33 29 43 16
28 30 96 19 96 65 78 5 23 2
47 12 72 67 73 88 29 15 63 45
48 71 35 68 82 8 45 1 23 15
47 48 25 50 73 57 89 95 6 55
71 41 67 15 77 96 7 34 93 57
3 97 22 52 64 53 85 42 7 29
80 30 25 50 68 29 50 29 82 76
42 7 50 6 5 78 100 3 18 54
Step – 1 : Start
Step – 12 : Stop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
main()
{
int A[100], i = 1, sum = 0;
float avg;
time_t *timer;
clrscr();
srand(time(timer));
printf("The random numbers from 1 to 100 are :\n");
while(i <= 100)
{
A[i] = 1 + rand() % 100;
printf("%d \t",A[i]);
sum += A[i];
i++;
}
avg = sum / 100.0;
printf("\nAverage of the random numbers = %.2f\n",avg);
getch();
return 0;
}
Output:
The random numbers from 1 to 100 are :
28 78 69 42 43 81 21 67 62 35
81 6 58 14 31 68 52 24 15 66
51 49 49 89 49 61 40 14 28 27
37 43 3 86 84 99 42 58 5 91
38 18 75 53 45 52 57 25 58 25
16 58 11 91 28 51 40 32 21 47
15 17 49 1 35 83 48 8 5 24
16 29 61 8 54 42 82 73 9 54
17 12 97 22 99 82 82 91 69 36
9 17 55 94 18 4 32 90 88 96
Step – 1 : Start
Step – 12 : Stop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
main()
{
int A[100], i = 1, sum = 0;
float avg;
time_t *timer;
clrscr();
srand(time(timer));
printf("The random numbers from 1 to 100 are :\n");
do
{
A[i] = 1 + rand() % 100;
printf("%d \t",A[i]);
sum += A[i];
i++;
}while(i <= 100);
avg = sum / 100.0;
printf("\nAverage of the random numbers = %.2f\n",avg);
getch();
return 0;
}
Output:
The random numbers from 1 to 100 are :
97 66 49 54 99 39 25 13 51 98
99 39 87 27 15 67 53 27 97 36
89 13 69 10 18 5 88 85 8 1
56 44 86 78 75 64 18 99 81 37
9 72 33 87 20 45 78 48 10 47
2 9 9 23 34 3 95 46 98 44
58 44 87 70 66 73 96 59 76 88
65 61 100 65 98 27 55 74 15 64
16 97 16 72 43 97 59 81 42 14
80 75 87 54 21 70 51 21 65 91
Flow Chart:
Step – 1 : Start
Step – 4 : If 1 Print enter your choice and execute from Step to Step otherwise go to
Stop
Step – 5 : If choice is 1. Print enter the source string and go to next Step otherwise go to
Step–13
Step – 8 : If s[i] not equal to NULL character is true then go to next Step otherwise go to
Step–11
Step – 13 : If choice is 2. Print enter the source string position, length and go to next Step
otherwise go to Step–21
Step – 21 : If choice is 3. Print enter the source string and declare variable i, j, temp and
go to next Step otherwise go to Step–25. Initialize i to zero and j to strlen(s)–1
Step – 22 : If i < j is true then perform temp = s[i], s[i] = s[j] and s[j] = temp and go to next
Step – 25 : If choice is 4. Print enter the source string and destination string otherwise go
to Step–24
Step – 29 : If s[i] not equal to NULL characters is true go to next Step otherwise go to
Step–33
Step – 34 : If choice is 5. Print enter the source string declare variable i and go to next
Step. Otherwise go to Stop
Step – 39 : Stop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
main()
{
int len, pos, choice, a;
char s[50], d[50];
clrscr();
printf("\n Welcome to storing functions \n");
printf("\n 1. Copy \n 2. Substring \n 3. Concatenation \n
4. Reverse \n 5. Length \n 6. Exit \n");
do
{
printf("Enter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter the string :");
scanf("%s",s);
string_copy(d,s);
printf("\n The copied string is %s \n",d);
break;
case 2:
printf("\n Enter the string :");
scanf("%s",s);
printf("\n Enter the starting position and length of string
\n");
scanf("%d %d",&pos,&len);
substring(d,s,pos,len);
printf("\n The substring is %s \n",d);
break;
case 3:
printf("\n Enter the string :");
scanf("%s %s",d,s);
string_concat(d,s);
printf("\n The string is %s \n",d);
break;
case 4:
printf("\n Enter the string :");
scanf("%s",s);
1. Copy
2. Substring
3. Concatenation
4. Reverse
5. Length
6. Exit
Enter your choice :1
Flow Chart:
Step – 1 : Start
Step – 2 : Initialize a, b
Step – 24 : Print a + 1
Step – 27 : Stop
#include<stdio.h>
#include<conio.h>
#include<float.h>
#include<limits.h>
main()
{
int a, b;
clrscr();
printf("Maximum limit of integer : %d \n",INT_MAX);
printf("Minimum limit of integer : %d \n",INT_MIN);
printf("Maximum limit of short integer : %d \n",SHRT_MAX);
printf("Minimum limit of short integer : %d \n",SHRT_MIN);
printf("Maximum limit of long integer : %d \n",LONG_MAX);
printf("Minimum limit of long integer : %d \n",LONG_MIN);
printf("Maximum limit of unsigned integer : %u \n",UINT_MAX);
printf("Maximum limit of unsigned short integer : %u
\n",USHRT_MAX);
printf("Maximum limit of unsigned long integer : %u
\n",ULONG_MAX);
printf("Maximum limit of float : %E \n",FLT_MAX);
printf("Minimum limit of float : %E \n",FLT_MIN);
printf("Maximum limit of double : %le \n",DBL_MAX);
printf("Minimum limit of double : %le \n",DBL_MIN);
printf("Maximum limit of long double : %Le \n",LDBL_MAX);
printf("Minimum limit of long double : %Le \n",LDBL_MIN);
printf("Maximum limit of double : %le \n",DBL_MAX);
printf("Minimum limit of double : %le \n",DBL_MIN);
printf("Maximum limit of character : %d \n",CHAR_MAX);
printf("Minimum limit of character : %d \n",CHAR_MIN);
printf("Maximum limit of signed character : %d \n",SCHAR_MAX);
printf("Minimum limit of signed character : %d \n",SCHAR_MIN);
printf("Maximum limit of unsigned character : %d \n",UCHAR_MAX);
a = INT_MAX;
printf("If we add 1 to maximum possible integer then they
obtained value is %d \n", a + 1);
b = INT_MIN;
printf("If we add 1 to minimum possible integer then they
obtained value is %d \n", b + 1);
getch();
return 0;
}
Flow Chart:
Step – 1 : Start
Step – 5 : Generate 100 real numbers between 10.0 and 20.0 and store them in the
array a[100]
Step – 7 : Sort the elements in a descending order and stored into array a[150]
Step – 9 : Stop
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
main()
{
int i, j;
float a[150], temp;
clrscr();
for(i = 0; i < 100; i++)
{
a[i] = 10.0 + random(1000) / 100.0;
}
printf("The generated 100 random real numbers : \n");
for(i = 0; i < 100; i++)
{
printf("%.2f \t",a[i]);
}
/* Sorting */
for(i = 0; i < 100; i++)
{
for(j = 0; j < 100; j++)
{
if(a[j] < a[j+1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("The generated 100 random real numbers in descending
order : \n");
for(i = 0; i < 100; i++)
{
printf("%.2f \t",a[i]);
}
getch();
return 0;
}
10.10 10.03 13.35 10.33 13.55 12.17 15.36 11.95 17.00 19.49
12.74 14.44 11.08 16.98 15.64 10.41 11.65 18.15 16.85 17.64
18.27 19.59 12.19 14.26 19.52 18.39 19.23 18.10 14.51 16.04
16.61 15.99 15.49 17.20 11.13 14.06 11.21 16.71 14.74 14.91
15.64 13.44 18.68 12.64 11.79 14.23 16.94 11.63 15.38 16.45
16.23 10.03 17.87 12.68 14.61 13.86 13.76 15.81 16.03 12.79
11.70 18.05 12.94 13.33 14.08 12.40 14.13 10.54 14.94 19.83
10.01 14.09 10.69 10.73 12.54 19.74 13.55 14.04 11.97 11.97
12.11 12.49 17.58 18.89 19.05 17.35 14.61 15.31 10.35 11.30
14.58 14.83 15.96 12.53 11.14 17.01 16.49 18.86 18.75 10.42
19.83 19.74 19.59 19.52 19.49 19.23 19.05 18.89 18.86 18.75
18.68 18.39 18.27 18.15 18.10 18.05 17.87 17.64 17.58 17.35
17.20 17.01 17.00 16.98 16.94 16.85 16.71 16.61 16.49 16.45
16.23 16.04 16.03 15.99 15.96 15.81 15.64 15.64 15.49 15.38
15.36 15.31 14.94 14.91 14.83 14.74 14.61 14.61 14.58 14.51
14.44 14.26 14.23 14.13 14.09 14.08 14.06 14.04 13.86 13.76
13.55 13.55 13.44 13.35 13.33 12.94 12.79 12.74 12.68 12.64
12.54 12.53 12.49 12.40 12.19 12.17 12.11 11.97 11.97 11.95
11.79 11.70 11.65 11.63 11.30 11.21 11.14 11.13 11.08 10.73
10.69 10.54 10.42 10.41 10.35 10.33 10.10 10.03 10.03 10.01
Flow Chart:
Step – 1 : Start
Step – 3 : If m = n enter elements into an array and generate from Step–4 to Step–13.
Otherwise print transpose of given matrix is not possible and go to Step–15
Step – 6 : Check whether is less than n. If true then initialize j to zero. Otherwise go to
Step–9
Step – 7 : Check whether is less than n. If true go to Step–8 and if false Step–6 by
incrementing i i.e., i+1.
Step – 10 : Check whether i < n. If true initialize j to zero and go to Step–11. Otherwise
increment i
Step – 11 : Check whether j is less than n. If true check whether i is less than j. If i < j is
true swap a[i][j] and a[j][i] and if i < j s false, increment j by 1 and repeat same
Step j < n and if j < n is false go to Step–10 and increment i by 1
Step – 13 : Check whether i < n. If true initialize j to zero and got Step–14. Otherwise go
to Step–15
Step – 14 : Check whether j < n. If true print the transposed matrix a[i][j] by incrementing
j by 1 and if false increment i by 1 and go to Step–13
Step – 15 : Stop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
main()
{
int a[10][10], i, j, m, n;
clrscr();
printf("Enter the order of the matrix: \n");
scanf("%d %d",&m, &n);
if(m == n)
{
printf("Type in matrix form: \n");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The given matrix is as follows \n");
print_matrix(a, n);
printf("The transposed matrix is as follows \n");
transpose_matrix(a,n);
print_matrix(a, n);
}
else
{
printf("For given values, transpose of matrix is not possible
:");
}
getch();
return 0;
}
Run 1:
Run 2:
Flow Chart:
Step – 1 : Start
Step – 10 : Print file to be open if(fp==NULL) condition is true and exit if file ended
Step – 13 : If true
Step – 18 : Stop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
main()
{
FILE *fp;
int n, count = 0, sum = 0, sumsq = 0;
float mean = 0.0, sd = 0.0;
clrscr();
fp = fopen("mean.dat","w");
if(fp==NULL)
{
puts("\n Can't open the file ");
exit(0);
}
printf("\n Enter some numbers. To stop use Ctrl + z :\n");
while(scanf("%d",&n)!=EOF)
fprintf(fp,"%d\t",n);
fclose(fp);
fp = fopen("mean.dat","r");
printf("\n The numbers stored in file : \n");
while(fscanf(fp,"%d",&n)!=EOF)
{
count++;
sum = sum + n;
printf("%d\t",n);
sumsq += n * n;
}
fclose(fp);
mean = sum / count;
printf("\n Mean = %0.2f",mean);
sd = sqrt((sumsq/count)-(mean*mean));
printf("\n standard derivation = %0.2f\n",sd);
getch();
return 0;
}
Flow Chart:
Step – 1 : Start
Step – 6 : Calculate l = (r * A)
Step – 8 : Stop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
main()
{
int x, y, z, x1, y1, z1, x2, y2, z2;
double r, l, d, A;
clrscr();
printf("Enter x, y, z, co-ordinates of the centre of the sphere
\n");
scanf("%d%d%d",&x,&y,&z);
printf("Enter x1, y1, z1, co-ordinates \n");
scanf("%d%d%d",&x1,&y1,&z1);
printf("Enter x2, y2, z2, co-ordinates \n");
scanf("%d%d%d",&x2,&y2,&z2);
r=sqrt(pow((x-x1),2)+pow((y-y1),2)+pow((z-z1),2));
d=sqrt(pow((x1-x2),2)+pow((y1-y2),2)+pow((z1-z2),2));
A = 2 * asin(d / (2 * r));
l=(r * A);
printf("\nThe smallest arc length between the given points on
the sphere is %f limits",l);
getch();
}
Output:
The smallest arc length between the given points on the sphere is 3.141593 limits
Flow Chart:
Step – 1 : Start
Step – 3 : Enter the no. for which square root has to be found
Step – 7 : Exit
Step – 8 : Xmid = (a + b) / 2
Step – 10 : If f(xmid == 0)
Step – 11 : Break
Step – 13 : B = xmid
Step – 15 : Xmid = a + b / 2
Step – 16 : Go to Step–10
Step – 18 : Stop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
float n;
float f(float x)
{
return(x * x - n);
}
main()
{
float a, b, epsilon, xmid;
clrscr();
printf("\nEnter the no. of which square root has to be found :
\n");
scanf("%f",&n);
printf("\nEnter the initial guess values and accuracy: \n");
scanf("%f,%f,%f",&a, &b, &epsilon);
if(f(a) * f(b) > 0)
{
printf("\n Unsuitable initial values \n");
exit(0);
}
xmid = (a + b) / 2;
while(fabs(xmid - b) / xmid >= epsilon)
{
if(f(xmid) == 0)
break;
if(f(a) * f(xmid) < 0)
b = xmid;
else
a = xmid;
xmid = (a + b) / 2;
}
printf("\nThe Square root = %2f \n",xmid);
getch();
return 0;
}
Run 1:
Run 2:
Flow Chart:
Step – 1 : Start
Step – 12 : Stop
Step – 2 : return(x * x * x - 2 * x - 5)
Step – 4 : return(3 * x * x - 2)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
float f(float x)
{
return(x * x * x - 2 * x - 5);
}
float f1(float x)
{
return(3 * x * x - 2);
}
main()
{
float x, epsilon, slope, xnew, rerror;
clrscr();
printf("\nEnter Initial approximation to root : \n");
scanf("%f",&x);
printf("\nEnter accuracy and limit on allowed slope : \n");
scanf("%f,%f",&epsilon, &slope);
do
{
if(f1(x) < slope)
{
printf("slope is too small \n");
break;
}
xnew = x - (f(x) / f1(x));
rerror = fabs((xnew - x) / xnew);
x = xnew;
}
while(rerror >= epsilon);
printf("Root = %f ", xnew);
getch();
return 0;
}
Run 1:
Run 2:
Flow Chart:
Step – 1 : Start
Step – 19 : Check whether i < n if true execute from Step–19 to Step–25 otherwise go to
Step–26
Step – 22 : Check whether j < n if true execute from Step–23 to Step–24 otherwise go to
Step–25
Step – 27 : Stop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
main()
{
int i, j, n;
float x[10], f[10], sum = 0, product = 1, y;
clrscr();
printf("Enter the value of n \n");
scanf("%d",&n);
printf("Enter %d values of x, f(x) \n",n);
for(i = 0; i < n; i++)
scanf("%f,%f",&x[i],&f[i]);
printf("Enter the intermediate value \n");
scanf("%f",&y);
printf("\n x f(x) \n");
for(i = 0; i < n; i++)
printf("\n %f \t %f\n",x[i],f[i]);
for(i = 0; i < n; i++)
{
product = 1;
for(j = 0; j< n; j++)
{
if(j != i)
product *=((y-x[j])/(x[i] - x[j]));
}
sum += f[i] * product;
}
printf("y = %f \t sum = %f \n",y,sum);
getch();
return 0;
}
x f(x)
5.000000 7.000000
6.000000 6.000000
7.000000 5.000000
Flow Chart:
Step – 1 : Start
Step – 5 : Check whether i < n if true go to next Step and execute from Step–9 otherwise
increment i by 1
Step – 7 : Check whether j < n if true execute Step–8 and Step–9 otherwise increment i
by 1and go to Step–5
Step – 13 : Check whether i < n if true execute from Step–14 to Step–19 otherwise go to
Step–20
Step – 15 : Check whether j < n if true execute Step–16 and Step–17 otherwise go to
Step–18
Step – 20 : Initialize k = 0
Step – 23 : Check whether i < n if true executes from Step–24 to Step–31 otherwise go to
Step–21 by increment k by 1
Step – 27 : Check whether j is less than if true execute from Step–28 to Step–30
otherwise go to Step–23 by i + 1
Step – 32 : Check whether i < n if true execute from Step–33 to Step–36 otherwise go to
Step–37
Step – 34 : Check whether j < n if true execute Step–35 and Step–36 otherwise go to
Step–32 by incrementing i by 1
Step – 38 : Check whether i < n if true execute from Step–39 to Step–42 otherwise return
to main
Step – 40 : Check whether j < n if true execute Step–40 and Step–41 otherwise increment
Step – 43 : Stop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
main()
{
int i, j, k, n;
float a[10][10];
clrscr();
printf("Enter the order of the matrix\n");
Output:
Run 1:
Run 2:
Flow Chart:
Step – 1 : Start
Step – 4 : Read a, b
Step – 7 : Assign x = a
Step – 8 : Initialize i = 1
Step – 10 : Assign x += h
Step – 17 : Stop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
float f(float x)
{
return(1/(1 + x));
}
main()
{
int i, n;
float s1 = 0, s2 = 0, a, b, h, x, integral;
clrscr();
printf("Enter a and b limits \n");
scanf("%f,%f",&a,&b);
printf("Enter no. of steps : \n");
scanf("%d",&n);
h = (b - a) / n;
x = a;
for(i = 1; i < n; i++)
{
x += h;
if(i % 2 == 0)
s1 += 2 * f(x);
else
s2 += 4 * f(x);
}
integral = (h / 3) * (f(a) + s1 + s2 + f(b));
printf("Integral is %f",integral);
getch();
return 0;
}
Run 1:
Run 2:
Flow Chart:
Step – 1 : Start
Step – 4 : Assign p = (a + b) / 2
Step – 5 : Assign q = (b – a) / 2
Step – 8 : Stop
Step – 2 : return(x * x + 2 * x)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
float f(float x)
{
//return(exp(-x*x));
return(x * x + 2 * x);
}
main()
{
float p, q, a, b, Integral;
clrscr();
printf("Enter a and b limits \n");
scanf("%f,%f",&a,&b);
p = (a + b) / 2;
q = (b - a) / 2;
Integral = q *(f(p + q / sqrt(3))+f(p-q/sqrt(3)));
printf("Integral = %f\n", Integral);
getch();
return 0;
}
Output:
Flow Chart:
Step – 1 : Start
Step – 6 : Check whether i < n if true execute from Step–9 to Step–10 otherwise go to
Step–11
Step – 8 : Check whether i < n + 1 if true execute from Step–9 to Step–10 otherwise
increment by 1 and go to Step–6
Step – 12 : Check whether k < n if true execute from Step–13 to Step–20 otherwise go to
Step–21
Step – 14 : Check whether i < n if true execute from Step to Step otherwise increment r
and go to Step–12
Step – 22 : Check whether i < n if true execute from Step–23 to Step–24 else Step–26
Step – 26 : Stop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
main()
{
int i, j, k, n;
float a[10][10], r;
clrscr();
printf("Enter the order of the matrix\n");
scanf("%d",&n);
printf("Enter the elements \n");
for(i = 0; i < n; i++)
for(j = 0; j < n + 1; j++)
scanf("%f",&a[i][j]);
for(k = 0; k < n; k++)
for(i = 0; i < n; i++)
{
if(i == k)
continue;
r = a[i][k] / a[k][k];
for(j = 0; j < n+1; j++)
a[i][j] -= r * a[k][j];
}
printf("The Solution is \n");
for(i = 0; i < n; i++)
{
a[i][n] = a[i][n] / a[i][i];
printf("%f\t",a[i][n]);
}
getch();
return 0;
}
Run 1:
The Solution is
0.133333 1.466667
Run 2:
The Solution is
0.166667 0.318627 0.975490