C Practical
C Practical
#include<stdio.h>
#include<conio>
void main()
{
int a,b,c;
clrscr();
printf("enter the values of a and b");
scanf("%d%d",&a,&b);
c=a+b;
printf("sum=%d",c);
getch();
}
Output:-
2. Write a Program to calculate average of 5 numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,e,avg;
clrscr();
printf("enter the values of a, b, c, d, and e");
scanf("%d%d%d%d%d",&a,&b,&c,&d,e);
avg=(a+b+c+d+e)/5;
printf("avg=%d",avg);
getch();
}
Output:-
#include<conio.h>
void main()
{
int a=5,b=8,c=6,d;
clrscr();
d=a*b*c;
printf("mul=%d",d);
getch();
}
Output:-
4. Write a Program to print area of a circle.
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float pie=3.14;
float area;
printf("Enter the value of r");
scanf("%d",&r); area=(pie*r*r);
printf("Area=%f",area);
getch();
}
Output:-
5. Write a Program to print the biggest of three
numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the values of a,b,and c");
scanf("%d%d%d",&a,&b,&c);
if (a>b&&a>c)
{
printf(" A is Biggest");
}
if (b>a&&b>c)
{
printf("B is Biggest");
}
if (c>a&&c>b)
{
printf("C is Biggest");
}
getch();
}
Output:-
6. Write a Program to check given number is even or
odd.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("enter the value of a");
scanf("%d",&a);
if(a%2==0)
{
printf("Number is Even");
}
else
{
printf("Number is Odd");
}
getch();
}
Output:-
7. Write a Program to check given number is positive
or negative.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("enter the value of a");
scanf("%d",&a);
if (a>0)
{
printf("Number is Positive");
}
else
{
printf("Number is Negative");
}
getch();
}
#include<conio.h>
void main()
{
int a;
printf("Enter tha value of a");
scanf("%d",&a);
if (a%4==0)
{
printf("This year is Leap Year");
}
else
{
printf("This is not Leap Year");
}
getch();
}
Output:-
#include<conio.h>
void main()
{
{
printf("\n Monday");
}
else if (day==2)
{
printf("\n Tuesday");
}
else if (day==3)
{
printf("\n Wednesday");
}
else if (day==4)
{
printf("\n Thursday");
}
else if (day==5)
{
printf("\n Friday");
}
else if (day==6)
{
printf("\n Saturday");
}
else if (day==7)
{
printf("\n Sunday");
}
else
{
printf("Please Enter Correct Value");
}
getch();
}
#include<conio.h>
void main()
{
{
case 1:
printf("January");
break;
case 2:
printf("Febuary");
break;
case 3: printf("March");
break;
case 4:
printf("April");
break;
case 5:
printf("May");
break;
case 6:
printf("June");
break;
case 7:
printf("July");
break;
case 8: printf("August");
break;
case 9:
printf("September");
break;
case 10:
printf("October");
break;
case 11:
printf("November");
break;
case 12:
printf("December");
break;
default:
Output:-
11. Write a Program to print 1 to 10 numbers
while loop
#include<stdio.h>
#include<conio.h>
void main()
{
while(i<=10)
{
getch();
}
Output:-
12. Write a Program to print 1 to 10 Numbers by
using do while.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
do
{
printf("\n %d",i); i++;
}while(i<=10); getch();
}
Output:-
13. Write a Program to print 1 to 10 by using for
loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("\n %d",i);
}
getch();
}
Output:-
14. Write a Program to calculate factorial of a
given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact=1;
clrscr();
printf("Enter the value of n");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
fact=fact*i;
}
printf("Factorial is=%d",fact);
getch();
}
Output:-
15. Write a Program to print the fibonacci series.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a=0,b=1,c;
clrscr();
printf("Enter the term of fibonacci series");
scanf("%d",&n);
printf("%d%d",a,b);
for(i=1;i<n;i++)
{
c=a+b;
printf("%d",c);
a=b;
b=c;
}
getch();
}
Output:-
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("Enter a Number");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf("\n %d*%d=%d",n,i,n*i);
}
getch();
}
Output:-
#include<conio.h>
void main()
{
int num,n,rev=0,rem;
clrscr();
printf("Enter the Number");
scanf("%d",&num);
n=num;
while(n!=0)
rem=n%10; n=n/10;
rev=rev*(10+rem);
}
printf("Old Number=%d",num);
printf("Reversed NO=%d",rev);
getch();
}
Output:-
#include<conio.h>
void main()
{
int num,n,rem,sum=0;
clrscr();
printf("Enter a Number");
scanf("%d",&num);
n=num;
while(n!=0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
if (sum==num)
{
printf("\n Number is Armstrong");
}
else
{
printf("\n Number is not Armstrong");
}
getch();
}
Output:-
19. Write a Program to print odd numbers between
1 to 50.
#include<stdio.h>
#include<conio.h>
void main()
{
i++;
}
getch();
}
Output:-
#include<conio.h>
void main()
{
int n,rem,rev=0,num;
clrscr();
printf("Enter a Number");
scanf("%d",&n);
num=n;
while(n!=0)
{
rem=n%10; n=n/10;
rev=((rev*10)+rem);
}
if(num==rev)
printf("Entered Number is palindrome");
else
printf("Entered Number is not Palindrome");
getch();
}
Output:-
21. Write a Program to add two numbers using
Functions.
#include<stdio.h>
#include<conio.h>
int sum(int,int);
void main()
{
int a,b,c;
printf("enter the values of a and b");
scanf("%d%d",&a,&b);
c=sum(a,b);
printf("sum=%d",c);
getch();
}
int z;
z=x+y;
return z;
}
Output:-
22. Write a Program to calculate the factorial of
given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,result;
clrscr();
printf("Enter the value of n");
scanf("%d",&n); result=fact(n);
printf("factorial=%d",result);
getch();
}
int fact(int x)
{
if (x==0)
return(1);
else
{
return(x*fact(x-1));
}
}
Output:-
23. Write a Program to print the fibonacci
series.
#include<stdio.h>
#include<conio.h>
int n,i;
clrscr();
printf("Enter the term of fibonacci series");
scanf("%d",&n);
printf("\n0");
for(i=1;i<=n;i++)
printf("\n &d",fib(i));
getch();
}
int fib(int m)
{
int c;
return(0);
if(m==1)
return(1);
else
{
c=a+b;
a=b; b=c;
return(c);
Output:-
24. Write a Program to calculate simple interest.
#include<stdio.h>
#include<conio.h>
void main()
{
int p,t,r;
int splint;
clrscr();
printf("Enter the value of p r and t");
scanf("%d%d%d",&p,&r,&t);
splint=(p*r*t/100);
printf("Splint=%d",splint);
getch();
}
Output:-
25. Write a Program to show concept of
function no argument and with return.
#include<stdio.h>
#include<conio.h>
int mul();
void main()
{
int result;
result =mul();
printf("Multiplication=%d",result);
getch();
}
int mul()
{
int a,b,c;
printf("Enter the value of a and b");
scanf("%d%d",&a,&b);
c=a*b;
return c;
}
#include<conio.h>
void sub(int,int);
void main()
{
z=x-y;
printf("Result=%d",z);
}
Output:-
#include<conio.h>
void display();
void programinfo();
void main()
{
clrscr();
display();
programinfo();
display();
getch();
}
void display()
{
int i;
for(i=1;i<=20;i++)
printf("*");
printf("\n");
void programinfo()
{
printf(" No Argument NO Return \n");
}
Output:-
29. Write a Program to swap two numbers using call
by value.
#include<stdio.h>
#include<conio.h>
void swap(int,int);
void main()
{
int a,b;
clrscr();
printf("Enter the value of a and b");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
}
void swap(int x,int y)
{
int z;
z=x; x=y; y=z;
printf("%d%d",x,y);
}
Output:-
#include<conio.h>
void main()
{
printf("%d %d",*x,*y);
}
Output:-
#include<conio.h>
void main()
{
int a[20],n,i;
printf("Enter Array Size ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Array Elements are");
for(i=0;i<n;i++)
{
printf("\n %d",a[i]);
getch();
}
Output:-
32. Write a Program to find the maximum value
from array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,n,max;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
max=a[50];
for(i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
}
}
printf("Maximum value=%d",max);
getch()
}
Output:-
33. Write a Program to find the minimum value from
array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,n,min;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
min=a[0];
for(i=0;i<n;i++)
{
if(min>a[i])
{
min=a[i];
}
}
printf("Minimum value=%d",min);
getch();
}
Output:-
34. Write a Program to print the sum of all array
elements.
#include<stdio.h>
#include<conio.h>
void main()
{
int s[50],i,n,sum=0;
clrscr();
printf("Enter the array size");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&s[i]);
sum=sum+s[i];
printf("Sum of Student=%d",sum);
}
Output:-
35. Write a Program to print input elements using
Two-D array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],j,i,r,c;
clrscr();
printf("Enter row and column size");
scanf("%d%d",&r,&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter the value of Elements %d%d:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Array Elemnets are");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\n %d",a[i][j]);
}
printf("\n");
}
getch();
}
Output:-
36. Write a Program to add two matrices.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],c[5][5];
int i,j,r,col;
clrscr();
printf("Enter the row and column size");
scanf("%d%d",&r,&col);
printf("\n Enter the values of first matrix");
for(i=0;i<r;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
for(j=0;j<col;j++)
{
printf("\n Enter the values of elements %d%d",i,j);
scanf("%d",&b[i][j]);
}
for(i=0;i<r;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=0;
c[i][j]=a[i][j]+b[i][j];
}
for(j=0;j<col;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
Output:-
37. Write a Program to multiply two matrices.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],c[5][5];
int i,j,r,col;
clrscr();
printf("Enter the row and column size");
scanf("%d%d",&r,&col);
printf("\n Enter the values of first matrix");
for(i=0;i<r;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
for(i=0;i<r;i++)
{
for(j=0;j<col;j++)
{
printf("\n Enter the values of elements %d%d",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=0;
c[i][j]=a[i][j]*b[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<col;j++)
printf("%d\t",c[i][j]);
printf("\n\n");
}
getch();
}
Output:-
38. Write a Program to subtract two matrices.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],c[5][5];
int i,j,r,col;
clrscr();
printf("Enter the row and column size");
scanf("%d%d",&r,&col);
printf("\n Enter the values of first matrix");
for(i=0;i<r;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
for(i=0;i<r;i++)
{
for(j=0;j<col;j++)
{
printf("\n Enter the values of elements %d%d",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=0;
c[i][j]=a[i][j]-b[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<col;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
getch();
}
Output:-
39. Write a Program to Transpose a matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
getch();
}
Output:-
40. Write a Program to print diagonal elements of a
matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<j;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
{
printf("%d\t",a[i][j]);
}
}
}
getch();
}
Output:-