C Lab Programs By Tapasya Gutha in Gitam cse b · Edit Doc 1. Write a program, to read X,Y coordinates of three points and then calculate the area of the 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? VARIABLES USED: Variable name datatype purpose x1,x2,x3,y1,y2,y3 float coordinates of three points area float to store area of the triangle PROGRAM: #include<stdio.h> #include<conio.h> #include<math.h> main() { float x1,x2,x3,y1,y2,y3; float area;
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
C Lab Programs
By Tapasya Gutha in Gitam cse b · Edit Doc
1. Write a program, to read X,Y coordinates of three points and then calculate the area of the 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?
VARIABLES USED:
Variable name datatype purpose
x1,x2,x3,y1,y2,y3 float coordinates of three points
2. Write a program to find the roots of a quadratic equation using if else and switch statements.
VARIABLES USED:
Variable name datatype purpose
a,b,c float coefficients of Quadratic equation
D float to store determinant
r1,r2 float to store real roots
a1,a2 float to store imaginary roots
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main( )
{
float a,b,c,D,r1,r2,a1,a2;
int value;
printf("Enter the values of a,b,c");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
{
printf("It is not a quadratic equation");
}
else
{
D=(b*b)-(4*a*c);
if(D>0)
value=1;
else if(D<0)
value=2;
else
value=3;
switch(value)
{
case 1: printf("\nThe roots are real and distinct:");
r1=(-b+sqrt(D))/(2*a);
r2=(-b-sqrt(D))/(2*a);
printf("\nThe roots are:r1=%f\t r2=%f",r1,r2);
break;
case 2:printf("\nThe roots are imaginary and distinct:");
a1=(-b)/(2*a);
a2= (sqrt(fabs(D)))/(2*a);
printf("\nThe roots are r1=%f+i%f \t r2=%f-
i%f",a1,a2,a1,a2);
break;
case 3:printf("\nThe roots are real and same:");
r1=r2=(-b)/(2*a);
printf("\nThe roots are r1=%f \t r2=%f",r1,r2);
break;
}
}
getch();
}
OUTPUT:
Enter the values of a,b,c1 5 6
The roots are real and distinct
The roots are:r1=-2.000000 r2=-3.000000
3. Write a program which generates one hundred random integers in the range of 1 to 100, store them in an array and then prints the average. Write three versions of the program using different loop constructs(e.g., for, while and do while)
VARIABLES USED:
Variable name data purpose
i int to repeat loops
a[100] int to store random numbers
total int to find total of 100 random numbers
PROGRAM:
//Using for loop:
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
void main()
{
int i,a[100],total=0;
randomize();
printf("\n Hundred random numbers between 1 and 100 are \n");
for(i=0;i<100;i++)
{
a[i]=random(100)+1;
printf("%d\t",a[i]);
total=total+a[i];
}
printf("the average of 100 random numbers is %d ",total/100);
}
//Using while loop:
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
void main()
{
int i=0,a[100],total=0;
randomize();
printf("\n Hundred random numbers between 1 and 100 are \n");
while(i<100)
{
a[i]=random(100)+1;
printf("%d\t",a[i]);
total=total+a[i];
i++;
}
printf("the average of 100 random numbers is %d ",total/100);
}
//Using do-while loop:
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
void main()
{
int i=0,a[100],total=0;
randomize();
printf("\n Hundred random numbers between 1 and 100 are \n");
do
{
a[i]=random(100)+1;
printf("%d\t",a[i]);
total=total+a[i];
i++;
}while(i<100);
printf("the average of 100 random numbers is %d ",total/100);
}
OUTPUT:
Hundred random numbers between 1 and 100 are
95 82 73 12 91 75 100 11 2 45
76 13 1 81 9 69 9 6 9 51
42 50 87 32 57 26 86 20 96 76
45 61 85 36 85 47 76 83 30 80
73 33 38 100 57 29 57 42 42 97
93 20 61 22 96 14 32 5 39 56
5 55 26 74 25 76 82 90 50 33
53 14 39 20 199 45 3 52 82 60
91 56 43 68 96 40 64 10 6 80
16 12 45 52 91 72 5 55 2 94
the average of 100 random numbers is 49
4. Write a program for multiplication of square matrices
VARIABLES USED:
Variable name datatype purpose
x[ ],y[ ] int to store given matrices elements
z[ ] int to store result matrix elements
i,j,k int to repeat loops
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{ void multi(int x[2][2],int y[2][2]);
int x[2][2], y[2][2], i, j;
printf("enter 4 elements in x :");
for(i=0; i<2; i++)
for(j=0; j<2; j++)
scanf("%d",&x[i][j]);
printf("enter 4 elements in y :");
for(i=0; i<2; i++)
for(j=0; j<2; j++)
scanf("%d",&y[i][j]);
multi(x,y);
getch();
}
void multi(int x[2][2],int y[2][2])
{ int i,j,z[2][2],k;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
z[i][j] =0;
for(k=0; k<2;k++)
z[i][j] = z[i][j] + x[i][k] * y[k][j];
}
}
printf("\n Elements in x matrix : \n");
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",x[i][j]);
printf("\n");
}
printf("\n Elements in y matrix : \n");
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",y[i][j]);
printf("\n");
}
printf("\n Elements in z matrix : \n");
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",z[i][j]);
printf("\n");
}
}
OUTPUT:
Enter the 4 elements in x: 1 1 1 1
Enter the 4 elements in x: 1 1 1 1
Elements in x matrix:
1 1
1 1
Elements in y matrix:
1 1
1 1
Elements in z matrix:
2 2
2 2
5. Write a program to find maximum and minimum elements with their positions in a given array and then sort the above array.
VARIABLES USED:
Variable name datatype purpose
x[ ] int to store result matrix elements
i,j int to repeat loops
t int temporary variable
max,min int to store max and min values
maxpos,minpos int to store max and min positions
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{ int x[5],i,j,t,max,min,maxpos,minpos;
printf("enter 5 elements");
for(i=0;i<=4;i++)
scanf("%d",&x[i]);
max=min=x[0];
for(i=1;i<=4;i++)
{
if(x[i]>max)
{
max=x[i];
maxpos=i;
}
if(x[i]<min)
{
min=x[i];
minpos=i;
}
}
printf("max=%d is found at %d\n min=%d is found at %d",max,maxpos,min,minpos);
printf("\n before sorting elements:");
for(i=0;i<=4;i++)
printf("%d\t",x[i]);
for(i=0;i<=3;i++)
{
for(j=i+1;j<=4;j++)
{
if(x[i]>x[j])
{
t=x[i];
x[i]=x[j];
x[j]=t;
}
}
}
printf("\n after sorting elements:");
for(i=0;i<=4;i++)
printf("%d\t",x[i]);
getch();
}
OUTPUT:
Enter 5 elements:3 1 8 9 4
Max=9 is found at 3
Min=1 is found at 1
Before sorting elements:3 1 8 9 4
After sorting elements:1 3 4 8 9
6.Write a program to insert an element into an array
VARIABLES USED:
Variable name datatype purpose
a[10] int to store the array
n int size of the array
i int to repeat the loop
item int new item to be inserted
pos int to store the position
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,pos,item;
printf("Enter how many values u want:");
scanf("%d",&n);
printf("Enter %d elements into array a:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter which element u want to insert:");
scanf("%d",&item);
printf("Enter position for that element:");
scanf("%d",&pos);
for(i=n;i>=pos;i--)
{
a[i]=a[i-1];
}
a[pos-1]=item;
printf("\nAfter insertion the array elements are:");
for(i=0;i<=n;i++)
printf(" %d ",a[i]);
getch();
}
OUTPUT:
Enter how many values u want:5
Enter 5 elements into array a:12 4 8 45 89
Enter which element u want to insert:43
Enter position for that element:3
After insertion the array elements are: 12 4 43 8 45 89
7.Write a function for transposing a square matrix in place.(in place means that you are not allowed to have full temporary matrix)
VARIABLES USED:
Variable name datatype purpose
a[10][10] int to store the matrix
m,n int to store the order of the matrix
i,j int to repeat the loops
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{ int a[10][10],m,n,i,j;
void trans(int a[10][10],int,int);
printf("Enter Order of Matrix A:\n");
scanf("%d %d",&m,&n);
if(m= =n)
{
printf("Enter The Elements into Matrix A:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
trans(a,m,n);
}
else
printf("Not a Square Matrix");
getch();
}
void trans(int a[10][10],int m,int n)
{
int i,j;
printf("The Transpose of Matrix A is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%3d",a[j][i]);
printf("\n");
}
getch();
}
OUTPUT:
Enter Order of Matrix A:3 3
Enter The Elements into Matrix A:1 2 3 4 5 6 7 8 9
The Transpose of Matrix A is:
1 4 7
2 5 8
3 6 9
8.Write a program to print Fibonacci series using functions.
VARIABLES USED:
Variable name datatype purpose
n int to store given terms
fib1,fib2 int to store first,second temporary numbers
fib int to store fibonacci number
i int to repeat while loop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void fibo(int, int, int);
void main ( )
{
int fib1=0, fib2=1, n;
printf("\n\t Enter number of terms - ") ;
scanf("%d",&n);
fibo(fib1,fib2,n);
getch () ;
}
void fibo(int fib1, int fib2,int n)
{ int i=1,fib;
/* generation of Fibonacci series */
do
{
printf("\t %d",fib1);
fib=fib1+fib2;
fib1=fib2 ;
fib2=fib;
i++;
} while (i<=n) ;
}
OUTPUT:
enter n:5
0 1 1 2 3
9.Write a program to find the factorial of a given number using recursion.
VARIABLES USED:
Variable name datatype purpose
n int given number
f int to store factorial
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int fact(int n);
int n, f;
printf(“enter n :”);
scanf(“%d”,&n);
f = fact(n);
printf(“factorial of the given no. is :%d”,f);
getch( );
}
int fact(int n)
{
int f;
if(n = =0)
return(1);
else
if(n = =1)
return(1);
else
f = n * fact(n-1);
return(f);
}
OUTPUT:
enter n : 5
factorial of the given no. is : 120
10.Write a program to find ncr " using non recursive function while finding the factorial value using recursion.
VARIABLES USED:
Variable name datatype purpose
n,r int to find the ncr
result int to store the result
PROGRAM:
#include<stdio.h>
#include<conio.h>
int fact(int);
main()
{
float ncr(int,int);
int n,r;
printf("Enter n and r values for ncr:");
scanf("%d%d",&n,&r);
if(n<r)
printf("ncr is not possible");
else
printf("%dC%d is %.2f",n,r,ncr(n,r));
getch();
}
float ncr(int n,int r)
{
float result;
result=(float)fact(n)/(fact(n-r)*fact(r));
return (result);
}
int fact(int n)
{
int f;
if((n==0)||(n==1))
return 1;
else
f=n*fact(n-1);
return f;
}
OUTPUT:
Enter n and r values for ncr:5 3
5C3 is 10.00
11.Write a program to find whether given string is palindrome or not without using string functions.
VARIABLES USED:
Variable name datatype purpose
a[50],b[50] char to store the strings
length int string length
i,j int to repeat the loops
flag int to check for true or false
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
char a[50],b[50];
int length=0,i=0,j=0,flag;
printf("Enter The String:");
scanf("%s",a);
while(a[i]!='\0')
{
i++;
}
length=i;
printf("String Length is:%d",length);
for(i=length-1;i>=0;i--)
{
b[j]=a[i];
j++;
}
b[j]='\0';
for(i=0;i<length;i++)
{
if(b[i]==a[i])
flag=0;
else
{
flag=1;
break;
}
}
if(flag==0)
printf("\nThe String is Palindrome");
else
printf("\nThe String is Not Palindrome");
printf("\nThe Reverse String of %s is %s",a,b);
getch();
}
OUTPUT:
Enter The String:madam
String Length is:5
The String is Palindrome
The Reverse String of madam is madam
12.Given an array of strings write a program to sort the string in dictionary order.
VARIABLES USED:
Variable name datatype purpose
s[ 5][20],t[15] char array to store strings
i,j,n int to repeat loop
PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
char s[5][20],t[15];
int i,j,n;
printf("how many strings do you want to sort?");
scanf("%d",&n);
printf("\nenter the strings");
for(i=0;i<n;i++)
scanf("%s",s[i]);
for(i=1;i<n;i++)
{
for(j=1;j<n;j++)
{
if(strcmp(s[j-1],s[j])>0)
{
strcpy(t,s[j-1]);
strcpy(s[j-1],s[j]);
strcpy(s[j],t);
}
}
}
printf("the sorted strings are:\n");
for(i=0;i<n;i++)
printf("\n %s",s[i]);
}
OUTPUT:
how many strings do you want to sort?5
enter the strings Visakhapatnam
Vijayawada
Hyderabad
Guntur
Tirupati
The sorted strings are:
Guntur
Hyderabad
Tirupati
Vijayawada
Visakhapatnam
13.Develop a program to implement a structure to read and display the Name, Birthdate and salary of ten employers.
VARIABLES USED:
Variable name datatype purpose
name[20] char to store the name
day,month,year,salary int to store the employee details