C Practical Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

www.sucomputersforum.

com
B.Sc. I Semester-Computer Science
PRACTICAL PROGRAMS
1. Write a program to Find the largest two (three) numbers using if and conditional operator
Biggest of Three Using Conditional Operator
# include <stdio.h>
# include
<conio.h> void
main()
{
int a, b, c, big ;
clrscr();
printf("Enter three numbers : ") ;
scanf("%d %d %d", &a, &b, &c) ;
big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
printf("\nThe biggest number is:%d", big) ;
}
Output:
Enter three numbers: 33 66 22
The biggest number is:66
Biggest of Two Using Conditional Operator
# include <stdio.h>
# include
<conio.h> void
main()
{
int a, b, big ;
clrscr();
printf("Enter two numbers : ") ;
scanf("%d %d ", &a, &b ) ;
big = a > b ? a : b ;
printf("\nThe biggest number is:%d", big) ;
}
Output:
Enter two numbers: 33 66
The biggest number is:66

Biggest of Two Using If Statement


#include <stdio.h>
void main()
{
int a, b;
printf("Please Enter Two different values\n");
scanf("%d %d", &a, &b);
if(a > b)
{
printf("%d is Largest\n", a);
}
else if (b > a)
{
printf("%d is Largest\n", b);
}
else
{
B.Sc. I Semester Programming in C 1
printf("Both are Equal\n");
}
}
OUTPUT:
Please Enter Two different values 33 66
66 is Largest.

Biggest of Three Using If Statement:


#include <stdio.h>
void main()
{
int a, b, c;
printf("Please Enter three different values\n");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c)
{
printf("\n%d is Greater Than both %d and %d", a, b, c);
}
else if (b > a && b > c)
{
printf("\n%d is Greater Than both %d and %d", b, a, c);
}
else if (c > a && c > b)
{
printf("\n%d is Greater Than both %d and %d", c, a, b);
}
else
{
printf("\nEither any two values or all the three values are equal");
}
}
OUTPUT:
Please Enter three different values 33 66 22
66 is Greater Than both 33 and 22

2. Write a program to print the reverse of a given number.


#include<stdio.h>
#include<conio.h>
void main()
{
int n,r;
clrscr();
printf("Enter a number: ");
scanf("%d",&n); while(n!
=0)
{ r=n
%10;
printf("%d",r);
n=n/10;
}}
Output:
Enter a number 4567
7654
www.sucomputersforum.com

3. Write a program to Print the prime number from 2 to n where n is given by user.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,c=0,i,j;
clrscr();
printf("\n Enter n value:");
scanf("%d",&n);
for(i=2;i<=n;i++)
{ c=
0;
for(j=1;j<=i;j++)
if(i%j==0)
c++;
if(c==2)
printf("\t %d ",i);
}
}
Output:
Enter n value:20
2 3 5 7 11 13 17 19
4. Write a program to find the roots of a quadratic equation using switch statement.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,r1,r2,real,img;
int op;
clrscr();
printf("\n Enter a,b,c values:");
scanf("%f%f%f", &a,&b,&c);
d=(b*b)-4*a*c;
if(d>0)
op=1;
if(d==0)
op=2;
if(d>0)
op=3;

Switch op
{
Case 1:
{
printf("\n Roots are Real and
Unequal"); r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("\n root1= %f, \t root2= %f",r1,r2);
}
B.Sc. I Semester Programming in C 3
break;
case 2:
{
printf("\n Roots are Real and Equal");
r1=-b/(2*a);
printf("\n root1= root2= %f",r1);
}
break;
case 3:
{
d=abs(d);
printf("\n Roots are Imaginary");
real=-b/(2*a);
img=sqrt(d)/(2*a);
printf("\n root1= %f+i%f",real,img);
printf("\n root2= %f-i%f",real,img);
}}}

Output:

5. Write a program to print a triangle stars as follows (take number of lines from user):
*
***
*****
*******
*********
#include<stdio.h>
void main()
{
int i,j,k,n;
printf("Enter number of rows of the triangle: \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}

for(k=1;k<=(2*i)-1;k++)
{
printf("*");
}
printf("\n");
}
getch();
}
Output:
www.sucomputersforum.com

6. Write a program to find largest and smallest elements in a given list of numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,min,max,i;
clrscr();
printf("Enter the no. of elements you want in list:");
scanf("%d",&n);
printf("\n Enter %d
elements:",n); for(i=0;i<n;i++)
scanf("%d",&a[i]);
/* finding min. element of an array */
min=a[0];
for(i=1;i<n;i++)
if(a[i]<min)
min=a[i];
/* finding max. element of an array */
max=a[0];
for(i=1;i<n;i++)
if(a[i]>min)
max=a[i];
printf("\n Min. element Is= %d",min);
printf("\n Max. element Is= %d", max);
}
Output:
Enter the no. of elements you want in list: 8
Enter 8 elements: 101 13 84 56 90 47 33
Min. element Is=13
Max. element Is=101

7. Write a program to find the product of two matrices

#include<stdio.h>
#include<conio.h>
void read_mat(int a[][5],int m, int n);
void disp_mat(int a[][5],int m, int n);
void find_mul(int a[][5],int b[][5],int c[][5],int m, int n,int q);
void main()
{
int a[5][5],b[5][5],c[5][5];
int m,n,p,q;
clrscr();
B.Sc. I Semester Programming in C 5
printf("enter the dimensions of first matrix");
scanf("%d%d",&m,&n);
printf("\nenter the dimensions of second matrix");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("\nenter matrix A of dimensions %d X %d",m,n);
read_mat(a,m,n);
printf("\nenter matrix B of dimensions %d X %d",p,q);
read_mat(b,p,q);
find_mul(a,b,c,m,n,q); printf("\
n the matrix A is : \n");
disp_mat(a,m,n);
printf("\n the matrix B is : \n");
disp_mat(b,p,q);
printf("\n the result of A*B (matrix C) is : \n");
disp_mat(c,m,q);
}
else
printf("multiplication is not possible");
}
void read_mat(int a[][5], int m, int n)
{
int i,j; for(i=0;i<m;i+
+) for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void disp_mat(int a[][5], int m, int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}
}
void find_mul(int a[][5], int b[][5],int c[][5],int m, int n,int q)
{
int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{ c[i]
[j]=0;
for(k=0;k<n;k++) c[i][j]=c[i]
[j]+a[i][k]*b[k][j];
}
}

Output:
www.sucomputersforum.com

8. Write a program to find the GCD of two numbers using iteration and recursion
(Recursive Version) (iterative version)
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main()
{ void main()
int n1,n2,gcd; {
printf("\nEnter two numbers: "); int x,y,m,i;
scanf("%d %d",&n1,&n2); clrscr();
gcd=findgcd(n1,n2); printf("\ printf("Insert any two numbers: ");
nGCD of %d and %d is: scanf("%d%d",&x,&y);
%d",n1,n2,gcd); if(x>y)
} m=y;
int findgcd(int x,int y) else
{ m=x;
while(x!=y) for(i=m;i>=1;i--)
{ {
if(x>y) if(x%i==0&&y%i==0)
return findgcd(x-y,y); {
else printf("\nGCD of two number is :
return findgcd(x,y-x); %d",i) ;
} break;
return x; }
} }
}

B.Sc. I Semester Programming in C 7


9. Write a program to illustrate use of storage classes.

Auto variable Register Varaible


#include <stdio.h> #include<stdio.h>
void call1( ); void main( )
void call2( ); {
void main( ) register int m=1;
{ int v = 10; for( ; m<=5;m++)
call2( ); printf(“\t %d”, m);
printf(“\n V=%d”,v); }
} output:
void call1( ) 1 2 3 4 5
{ int v = 20;
printf(“\t V=%d”,v);
}
void call2( )
{ int v = 30;
call1( );
printf(“\t V=%d”,v);
}
output:
V=20
V=30
V=10

Static Variable Extern Variable


#include <stdio.h> #include <stdio.h>
void increment( ); int v=10;
void main( ) void call1( );
{ void call2( );
increment( ); void main( )
increment( ); {
increment( ); call1( );
} call2( );
void increment( ) printf(“\n In main( ) v = %d”,v);
{ }
int static m; void call1( )
m++; { printf(“\n In call1( ) v = %d”,v); }
printf(“\n m=%d”,m); void call2( )
} { printf(“\n In call2( ) v = %d”,v); }
Output: output:
m=1 In call1( ) v = 10
m=2 In call2( ) v = 10
m=3 In main( ) v = 10
www.sucomputersforum.com

10. Write a program to demonstrate the call by value and the call by reference concepts

/* call by value*/ /*call by reference*/


#include<stdio.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
void change(int x,int y); void change(int *p,int *q);
void main( ) main( )
{ {
int x,y; int x,y;
clrscr(); clrscr();
printf("Enter x,y,values:"); scanf(“%d printf(“Enter x,y,values:"); scanf(“%d
%d”, &x,&y) %d”,&x,&y);
printf("\n\nThe values of x,y before printf("\n\nThe values of x,y before
function call:"); printf("\nx=%d,\ty= function call:"); printf("\nx=%d,\ty=
%d”,x,y); change(x,y); %d”,x,y); change(&x,&y);
printf("\n\nThe values of x,y after printf("\n\nThe values of x,y after
function call:"); printf("\nx=%d,\ty= function call:"); printf("\nx=%d,\ty=
%d”,x,y); %d”,x,y);
} }
void change(int x,int y) void change(int *p,int *q)
{ {
printf("\n\nThe values of x,y in function printf("\n\nThe values of *p, *q in
before changing:"); printf("\nx=%d,\ty= function before changing:"); printf("\
%d”,x,y); n*p=%d,\t*q=%d”,*p,*q);
x=x+10; *p=*p+10;
y=y+10; *q=*q+10;
printf("\n\nThe values of x,y in function printf("\n\nThe values of *p, *q in
after changing:"); printf("\nx=%d,\ty= function after changing:"); printf("\n*p=
%d”,x,y); %d,\t*q=%d”,*p,*q);
} }

output: output:
Enter x,y values 12 10 Enter x,y values 12 10
The values of x,y before function call The values of x,y before function call
x=12 y=10 x=12 y=10
The values of x,y in function before The values of *p ,*q in function before
changing change
x=12 y=10 x=12 y=10
The values of x,y in function after changing The values of *p ,*q in function after
x=22 y=20 change
The values of x,y after function call x=22 y=20
x=12 y=10 The values of x,y after function call
x=22 y=20

B.Sc. I Semester Programming in C 9


11. Write a program that prints a table indicating the number of occurrences of each alphabet in
the text entered as command line arguments.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char ch;
do{
char a[20],f=0;
int i,n,ascii;
clrscr();
printf("enter a string:");
gets(a);
strlwr(a); /* Sting convert in small letter*/
for(ascii=97;ascii<=122;ascii++)
{ n=
0;
f=0;
for(i=0;a[i]!=NULL;i++)
{
if(ascii==a[i])
{
n++; /* If Checking sucessfull ..increment +1*/
f=1;
}
}
if(f==1) /* Checking f value is similar or not */
printf("\n\t%c value found %d times ",ascii,n);
}
printf("\n\t continue (y/n):");
ch=getch();
}while(ch=='y'||ch=='Y');
}

Output:

12. Write a program to illustrate use of data type enum.


www.sucomputersforum.com
#include<stdio.h>
#include<conio.h>
enum month {jan=1, feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec};
void main()
{
clrscr();
printf("\n jan = %d",jan);
printf("\n mar = %d",mar);
printf("\n oct = %d",oct);
printf("\n dec = %d",dec);
}
Output:

13. Write a program to demonstrate use of string functions string.h header file.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20],str3[40];
int n,p;
clrscr();
puts("enter a string");
gets(str1);
n=strlen(str1);
printf("\n the length of string %s is %d:",str1,n);
puts("\n enter str2");
gets(str2);
p=strcmp(str1,str2);
if(p)
printf("\n str1 and str2 are not equal");
else
printf("\n str1 and str2 are equal");
strcat(str1,str2);
printf("\n after concatenation str1 = %s",str1);
strcpy(str3,str1);
printf("\n after copy str3 = %s",str3);
}
Output:

B.Sc. I Semester Programming in C 11


14. Write a program that opens a file and counts the number of characters in a file.

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
void main()
{
char ch;
int i=0;
FILE *fp;
clrscr();
fp=fopen("sucf.txt","r");
while((ch=getc(fp))!=EOF)
{ i+
+;
}
printf("\n No. of characters in the given file is : %d",i);
}

Output:
No. of characters in the given file is :58
Note: for this program you have to create a file sucf.txt in NOTEPAD software and save it in the
same location where you are saving this program.

15. Write a program to create a structure Student containing fields for Roll No., Name, Class,
Year and Total Marks. Create 10 students and store them in a file.

#include<conio.h>
#include<stdio.h>
#include<ctype.h>

void main()
{
FILE *fp;
char *fname,ch='y';
int n=0,total=0;
struct stud
{
int roll_no;
char stud_name[20];
int sub1, sub2, sub3, total;
char group[20];
int year;
}s;
clrscr();
printf("\n enter source file name:\t");
gets(fname);
fp=fopen(fname,"w");
if(fp==NULL)
{
printf("\n unable to open");
exit(0);
www.sucomputersforum.com
}
while(ch=='Y'||ch=='y')
{
printf("\n enter Roll number");
scanf("%d",&s.roll_no);
printf("\n enter student name:\t");
scanf("%s",s.stud_name); printf("\
n enter Group\n");
scanf("%s",s.group);
printf("\n enter year");
scanf("%d",&s.year);
printf("\n Enter 3 subject marks"); scanf("%d%d
%d",&s.sub1,&s.sub2,&s.sub3); s.total=s.sub1+s.sub2+s.sub3;
fprintf(fp,"%d%s%s%d%d%d%d%d",s.roll_no,s.stud_name,s.group,s.year,s.sub1,s.sub2,s.sub3,s.total);
n++;
printf("\n do you want to add another record (Y/N)?");
fflush(stdin);
scanf("%c",&ch);
}
printf("\n%d record are store in %s file",n,fname);
fclose(fp);
getch();
}
Output:

16. Write a program that opens an existing text file and copies it to a new text file with all
lowercase letters changed to capital letters and all other characters unchanged.

#include<stdio.h>
#include<process.h>
#include<ctype.h>
void main() {
FILE *fp1, *fp2;
char a;
clrscr();
fp1 = fopen("test.txt", "r");
if (fp1 == NULL) {
puts("cannot open this file");
exit(1);
}
fp2 = fopen("test1.txt", "w");
if (fp2 == NULL) {
puts("Not able to open this file");
fclose(fp1);
exit(1);
}

do {
a = fgetc(fp1);
if(islower(a));

B.Sc. I Semester Programming in C 13


a=toupper(a);
fputc(a, fp2);
} while (a != EOF);
fcloseall();
getch();
}

You might also like