Uploaded by Kamesh0579

C Language - Practical Programs

advertisement
/* ODD OR EVEN NUMBER*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Enter the n value”);
scanf(“%d”,&n);
if(n%2==0)
printf(“%d is even number”,n);
else
printf(“%d is odd number”,n);
getch();
}
OUTPUT
Enter the n value
7
7 is odd number
/* CENTIGRADE TO FAHRENHEIT*/
#include<stdio.h>
#include<conio.h>
void main()
{
float c,f;
clrscr();
printf(“Enter the Centigrade values “);
scanf(“%f”,&c);
f=c*9/5+32;
printf(“Fahrenheit=%f”,f);
getch();
}
OUTPUT
Enter the Centigrade value
10
Fahrenheit=50.0
/* SUM OF FIRST 10 NATURAL NUMBERS*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
clrscr();
for(i=1;i<=10;i++)
{
printf(“\t%d\n”,i);
sum=sum+i;
}
printf(“Sum of the first 10 natural numbers is = %d”,sum);
getch();
}
OUTPUT
Number=1
Number=2
Number=3
Number=4
Number=5
Number=6
Number=7
Number=8
Number=9
Number=10
Sum of the first 10 natural number is = 55
/* GREATEST OF THREE NUMBERS*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Input:\nEnter the three numbers:\n");
scanf("%d%d%d",&a,&b,&c);
if((a!=b)&&(a!=c))
{
if((a>b)&&(a>c))
{
printf("\nOutput:\n\n%d is greater",a);
}
else if((b>a)&&(b>c))
{
printf("\nOutput:\n\n%d is greater",b);
}
else if((b>a)&&(b>c))
{
printf("\nOutput:\n\n%d is greater",c);
}
}
else
{
printf("Output:\n Two or more Numbers are Equal");
}
}
OUTPUT
Input:
Enter the three numbers:
5
4
2
Output:
5 is greater
/* PRINT THE DAYS USING SWITCH CASE*/
#include<stdio.h>
#include<conio.h>
void main()
{
int day;
clrscr();
printf(“enter the any number between 1 to 7”);
scanf(“%d”,&day);
switch(day)
{
case 1:
printf(“Monday is the first day of the week”);
break;
case 2:
printf(“Tuesday is the Second day of the week”);
break;
case 3:
printf(“Wednesday is the third day of the week”);
break;
case 4:
printf(“Thursday is the fourth day of the week”);
break;
case 5:
printf(“Friday is the first fifth of the week”);
break;
case 6:
printf(“Saturday is the sixth day of the week”);
break;
case 7:
printf(“Sunday is the seventh day of the week”);
break;
default:
printf(“You have enter a wrong choice”);
break;
}
getch();
}
OUTPUT
Enter the any number between 1 to 7
5
Friday is the fifth day of the week.
Enter the any number between 1 to 7
8
You have enter a wrong choice.
/*MATRIX MULTIPLICATION*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[5][5],b[5][5],c[5][5];
int m,n,p,i,j,k;
clrscr();
printf(“Enter the size of the Matrix-A\n”);
scanf(“%d%d”,&m,&n);
printf(“Enter A-Matrix\n”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
printf(“Enter the size of the Matrix-b\n”);
scanf(“%d%d”,&n,&p);
printf(“Enter B-Matrix\n”);
for(j=0;j<n;j++)
for(k=0;k<p;k++)
scanf(“%d”,&b[j][k]);
for(i=0;i<m;i++)
{
for(k=0;k<p;k++)
{
c[i][k]=0;
for(j=0;j<n;j++)
c[i][k]+=a[i][j]*b[j][k];
}
}
printf(“\n A-Matrix\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf(“%d”,a[i][j]);
printf(“\n”);
}
printf(“\n B-Matrix\n”);
for(j=0;j<n;j++)
{
for(k=0;k<p;k++)
printf(“%d”,b[j][k]);
printf(“\n”);
}
printf(“\n Resultant Matrix\n”);
for(i=0;i<m;i++)
{
for(k=0;k<p;k++)
printf(“%d”,c[i][k]);
printf(“\n”);
}
getch();
}
OUTPUT
Enter the size of Matrix A
2 2
Enter A Matrix
2
3
4
2
Enter the size of Matrix B
2 2
Enter B Matrix
3
2
4
1
A-matrix
23
42
B-Matrx
32
41
Resultant Matrix is
18 7
20 10
/*QUADRATIC EQUATIONS USING FUNCTION*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void quad(int a,int b,int c);
void main()
{
int a,b,c;
clrscr();
printf(“Enter the value for a,b,c”);
scanf(%d%d%d”,&a,&b,&c);
quad(a,b,c);
getch();
void quad(int a,int b,int c)
{
float r, root1,root2;
r=b*b-4*a*c;
if(r==0)
{
printf(“The two roots are real and equal”);
root1=b/(2.0*a);
root2=b/(2.0*a);
printf(“Root1=%f Root2=%f”,root1,root2);
}
else if(r>0)
{
printf(“The two roots are real and unequal”);
root1=(-b+sqrt(r))/(2.0*a);
root2=(-b-sqrt(r))/(2.0*a);
printf(“Root1=%f Root2=%f”,root1,root2);
}
else
{
printf(“The two roots are real and imaginary”);
r=abs(r);
root1=b/(2.0*a);
root2=sqrt(r)/(2.0*a);
printf(“Real root=%f Imaginary root=%f”,root1,root2);
}
}
OUTPUT
Enter the value for a,b,c
121
The two roots are real and equal
Root1=-1.000000 Root2=-1.000000
Enter the value for a,b,c
253
The two roots are real and unequal
Root1=-1.000000 Root2=-1.500000
/*REVERSE NUMBER USING POINTER*/
#include<stddio.h>
#include<conio.h>
int main()
{
int num,rem,rev=0;
int *pn,*pr;
clrscr();
printf("Enter any Number\n");
scanf("%d",&num);
pn=#
pr=&rev;
do
{
rem=(*pn)%10;
*pr=(*pr*10)+rem;
*pn=(*pn)/10;
}
while(*pn>0);
printf("Reverse Number is %d",*pr);
getch();
return 0;
}
OUTPUT
Enter any Number
7859
Reverse Number is 9587.
/*ADDITION OF TWO NUMBERS USING POINTERS*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c=0;
int *m,*n;
clrscr();
print(“Enter any two numbers”);
scanf(“%d%d”,&a,&b);
m=&a;
n=&b;
c=(*m)+(*n);
printf(“Addition of two number is: %d”,c);
getch();
}
OUTPUT
Enter any two number
12
32
Addition of two number is: 44.
/*FACTORIAL NUMBER USING RECURSION */
#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
int n;
clrscr();
printf(“Enter the n value”);
scanf(“%d”,&n);
printf(“Factorial value of %d is %d”,n,factorial(n));
getch();
}
factorial(int n)
{
int fact;
if(n==1)
return(1);
else
fact=n+factorial(n-1);
return(fact);
}
OUTPUT
Enter the n value
6
Factorial value of 6 is 720
/*STUDENT FILE CREATION*/
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int rollno,m1,m2,m3.i,n;
char name[20],fname[20];
clrscr();
printf(“Enter a file name with extension to create a new File”);
scanf(“%s”,fname);
fp=fopen(fname,”w”);
printf(“Enter the total no of students”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“Enter rollno,name,mark1,mark2,mark3:”);
scanf(“%d%s%d%d%d”,&rollno,name,&m1,&m2,&m3);
fprintf(fp,”\n%d %s %d %d %d”,rollno,name,m1,m2,m3);
}
printf(“Student Details: %d %s %d %d %d\n”,rollno,name,m1,m2,m3);
printf(“%s file is created!\n”,fname);
fclose(fp);
getch();
}
OUTPUT
Enter a file name with extension to create to create a new File: student.txt
Enter the total number of students: 02
Enter rollno.name,mark1,mark2,mark3: 101
Anu
56
67
78
Enter rollno,name,mark1,mark2,mark3: 102
Banu
45
60
52
Student Details :
101 Anu 56 67 78
102 Banu 45 60 52
Student.txt file is created.
/*STUDENT FILE UPDATION*/
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp,*fp1;
int rollno,m1,m2,m3.i,tot,n;
float avg;
char name[20],fname[20];
clrscr();
printf(“Enter a file name with extension to create”);
scanf(“%s”,fname);
fp=fopen(fname,”w”);
printf(“Enter the total no of students”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“Enter rollno,name,mark1,mark2,mark3”);
scanf(“%d%s%d%d%d”,&rollno,name,&m1,&m2,&m3);
fprintf(fp,”\n%d %s %d %d %d”,rollno,name,m1,m2,m3);
}
fclose(fp);
printf(“Enter file name to read”);
scanf(“%s”,fname);
fp1=fopen(fname,”r+”);
while(!feof(fp1))
{
fscanf(fp1,“%d %s %d %d %d”,&rollno,name,&m1,&m2,&m3);
tot=m1+m2+m3;
avg=tot/3.0;
fprintf(fp1,”Total=%d”,tot);
fprintf(fp1,”Average=%f”,avg);
printf(“\n%d %s %d %d %d\n”,rollno,name,m1,m2,m3,tot,avg);
}
printf(“%s file is updated!\n”,fname);
fclose(fp1);
getch();
}
OUTPUT
Enter a file name to create: student.txt
Enter the total number of students: 01
Enter rollno, name, mark1, mark2, mark3
100
Roja
45
54
60
Enter file name to read: student.txt
100 Roja 45 54 60
Total=159
Average=53.00
Student.txt file is updated!
/*CALL BY VALUE AND CALL BY REFERENCE*/
#include<stdio.h>
#include<conio.h>
void callbyvalue(int x, int y)
{
int t;
t=x;
x=y;
y=t;
}
void callbyreference(int *p,int *q)
{
int t;
t=*p;
*p=*q;
*q=t;
}
int main()
{
int a=30,b=50;
int p=30,q=50;
clrscr();
callbyvalue(a,b);
callbyreference(&p,&q);
printf(“Call by value”);
printf(“\n a value is %d”,a);
printf(“\n b value is %d”,b);
printf(“Call by reference”);
printf(“\n p value is %d”,p);
printf(“\n q value is %d”,q);
getch();
return(0);
}
OUTPUT
Call by value
a value is 30
b value is 50
Call by reference
p value is 50
q value is 30
/*MAXIMUM NUMBERS IN ARRAY USING POINTER*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,max;
int *p;
clrscr();
printf(“Enter the size of array:”);
scanf(“%d”,&n);
printf(“Elements %d elements in the array are:\n”,n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
pritnf(“Elements in the array are\n”);
for(i=0;i<n;i++)
pritnf(“%d”,a[i]);
p=&a[0];
max=a[0];
for(i=0;i<n;i++)
{
if(max<*p)
max=*p;
p++;
}
printf(“Maximum elements in the array is: %d”,max);
getch();
}
OUTPUT
Enter the size of array: 5
Elements 5 in the array are
34
45
13
67
21
Elements in the array are
34
45
13
67
21
Maximum elements in the array is 67.
Download