100% found this document useful (2 votes)
220 views

C Practical

The document contains 26 C programming examples demonstrating various concepts like: 1) Adding two numbers 2) Calculating average of 5 numbers 3) Multiplying three numbers 4) Finding the area of a circle 5) Checking if a number is even or odd 6) Using functions with and without arguments and return values 7) Implementing control structures like if-else, switch case, while, do-while and for loops.

Uploaded by

Piyush Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
220 views

C Practical

The document contains 26 C programming examples demonstrating various concepts like: 1) Adding two numbers 2) Calculating average of 5 numbers 3) Multiplying three numbers 4) Finding the area of a circle 5) Checking if a number is even or odd 6) Using functions with and without arguments and return values 7) Implementing control structures like if-else, switch case, while, do-while and for loops.

Uploaded by

Piyush Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 65

1. Write a Program to add two numbers .

#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:-

3. Write a program to multiply three numbers


#include<stdio.h>

#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();
}

8. Write a Program to check given year is leap year or


not?
#include<stdio.h>

#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:-

9. Write a Program to print the name of day


according to user choice.
#include<stdio.h>

#include<conio.h>

void main()
{

int day; clrscr();


printf("Enter the value for day 1 to 7");
scanf("%d",&day);
if (day==1)

{
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();
}

10. Write a Program to print the name of month.


#include<stdio.h>

#include<conio.h>

void main()
{

int month; clrscr();


printf("Enter the value for month 1 to 12");
scanf("%d",&month);
switch (month)

{
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:

printf("Please Enter Correct Value");


break;
}
getch();
}

Output:-
11. Write a Program to print 1 to 10 numbers
while loop
#include<stdio.h>

#include<conio.h>

void main()
{

int i=1; clrscr();

while(i<=10)
{

printf("\n %d",i); i++;


}

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:-

16. Write a Program to print the table of given no.


#include<stdio.h>

#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:-

17. Write a Program to print reverse of a number.


#include<stdio.h>

#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:-

18. Write a Program to print given number is


armstrong or not?
#include<stdio.h>

#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()
{

int i=1; while(i<=50)


{
if(i%2!=0)
printf("\n %d",i);

i++;
}
getch();
}

Output:-

20. Write a Program to check given number is


palindrome or not?
#include<stdio.h>

#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 sum(int x,int y)


{

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 fib(int m);


void main()

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)
{

static int a=0,b=1;

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;
}

26. Write a Program to show the


concept of function with argument and
NO return.
#include<stdio.h>

#include<conio.h>

void sub(int,int);

void main()
{

int a,b; clrscr();


printf("Enter the value of a and b");
scanf("%d%d",&a,&b);
sub(a,b);
getch();
}

void sub(int x,int y)


{
int z;

z=x-y;

printf("Result=%d",z);
}
Output:-

28.Writea Program to show concept of function


with no argument no return
#include<stdio.h>

#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:-

30. Write a Program to swap two no by call by


reference.
#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:-

31. Write a Program to print input elements using 1-


D array.
#include<stdio.h>

#include<conio.h>

void main()
{

int a[20],n,i;
printf("Enter Array Size ");

scanf("%d",&n);

for(i=0;i<n;i++)
{

printf("Enter the value of Elements %d",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;

printf("Enter the array size");


scanf("%d",&n);

for(i=0;i<n;i++)
{

printf("\n Enter the values of elements %d",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;

printf("Enter the array size");

scanf("%d",&n);

for(i=0;i<n;i++)
{

printf("\n Enter the values of elements %d",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++)
{

printf("\n Enter sum of student %d",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++)
{

printf("\n Enter the values of elements %d%d",i,j);

scanf("%d",&a[i][j]);
}
}

printf("\n Enter the values of second matrix");


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];
}

printf("\n After Addition \n");


for(i=0;i<r;i++)
{

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++)
{

printf("\n Enter the values of elements %d%d",i,j);

scanf("%d",&a[i][j]);
}

printf("\n Enter the values of second matrix");

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];
}
}

printf("\n After Multiplication \n");

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++)
{

printf("\n Enter the values of elements %d%d",i,j);

scanf("%d",&a[i][j]);
}

printf("\n Enter the values of second matrix");

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];
}
}

printf("\n After Subtraction \n");

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()
{

int a[5][5],i,j,r,c; clrscr();


printf("Enter the row and column size");
scanf("%d%d",&r,&c);
printf("\n Enter the values of first matrix");

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{

printf("\n Enter the values of elements %d%d",i,j);

scanf("%d",&a[i][j]);
}
}

printf("\n After Transpose \n");

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()
{

int a[5][5],i,j,r,c; clrscr();


printf("Enter the row and column size");
scanf("%d%d",&r,&c);
for(i=0;i<r;i++)

{
for(j=0;j<c;j++)
{

printf("\n Enter the values of elements %d%d",i,j);

scanf("%d",&a[i][j]);
}
}

printf("\n Diagonal Elements are \n");

for(i=0;i<j;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
{
printf("%d\t",a[i][j]);
}
}
}

getch();
}

Output:-

You might also like