0% found this document useful (0 votes)
23 views31 pages

Computer Practical

The document contains multiple C programs that perform various tasks such as checking if a number is odd or even, finding the largest of three numbers, calculating grades based on marks, determining leap years, calculating simple interest, and performing matrix operations. Each program includes user prompts for input and displays the corresponding output. The document serves as a collection of programming examples demonstrating basic C programming concepts and functionalities.

Uploaded by

Ishan Aggarwal
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
0% found this document useful (0 votes)
23 views31 pages

Computer Practical

The document contains multiple C programs that perform various tasks such as checking if a number is odd or even, finding the largest of three numbers, calculating grades based on marks, determining leap years, calculating simple interest, and performing matrix operations. Each program includes user prompts for input and displays the corresponding output. The document serves as a collection of programming examples demonstrating basic C programming concepts and functionalities.

Uploaded by

Ishan Aggarwal
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

C

PROGRAM 1
PROGRAM 1

#include <stdio.h>

int main() {
int a;
printf("Enter the number you want to check if it is odd or even:
");
printf("\n");
scanf("%d",&a);
if(a%2==0){
printf("It is an even number");
}
if(a%2!=0){
printf("It is an odd number");
}

return 0;
}

OUTPUT
Enter the number you want to check if it is odd or even:
4
It is an even number.
PROGRAM 1

#include <stdio.h>

int main() {
int a,b,c,d;
printf("Enter the values of a,b and c: ");
printf("\n");
scanf("%d%d%d",&a,&b,&c);
d=(a>b)?(a>c?a:c):(b>c?b:c);
printf("The largest number is %d",d);

return 0;
}

OUTPUT
Enter the values of a,b and c:
4
6
1
The largest number is 6
PROGRAM 1

#include <stdio.h>

int main() {
int a;
printf("Enter the marks obtained by the student: \n");
scanf("%d",&a);
if(a>=90){
printf("The grade obtained is A");
}
else if(a>=80){
printf("The grade obtained is B");}
else if(a>=70){
printf("The grade obtained is C");}
else if(a>=60){
printf("The grade obtained is D");}
else if(a>=50){
printf("The grade obtained is E");}
else if(a<50){
printf("The grade obtained is F");}
return 0;
}

OUTPUT
Enter the marks obtained by the student:
78
The grade obtained is C
PROGRAM 1

#include <stdio.h>

int main() {
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t;
printf("Enter the value of money you want to calculate minimum
number of notes for:\n");
scanf("%d",&a);
b=a/2000;
c=a%2000;
d=c/500;
e=c%500;
f=e/200;
g=e%200;
h=g/100;
i=g%100;
j=i/50;
k=i%50;
l=k/20;
m=k%20;
n=m/10;
o=m%10;
p=o/5;
q=o%5;
r=q/2;
s=q%2;
t=b+d+f+h+j+l+n+p+r+s;
printf("The minimum number of notes required is %d",t);
return 0;
}

OUTPUT
Enter the value of money you want to calculate minimum number of notes for:
76481
The minimum number of notes required is 44
PROGRAM 1

#include <stdio.h>

int main() {
int a;
printf("Enter the year you want to check if it is a leap year:\n");
scanf("%d",&a);
if(a%4==0){
printf("It is a leap year!");
}
if(a%4!=0){
printf("It is a normal year!");
}
return 0;
}

OUTPUT
Enter the year you want to check if it is a leap year:
2023
It is a normal year!
PROGRAM 1

#include <stdio.h>

int main() {
int a,b,c,d,e,f;
printf("Enter the value of money you want to calculate simple
interest on:\n");
scanf("%d",&a);
printf("Enter the value of time in years after you want to pay
back:\n");
scanf("%d",&b);

printf("Enter the value of interest you want to offer:\n");


scanf("%d",&d);
e=(b*a*d)/100;
f=e+a;
printf("The tota amount payable is %d",f);

return 0;
}

OUTPUT
Enter the value of money you want to calculate simple interest on:
20000
Enter the value of time in years after you want to pay back:
2
Enter the value of interest you want to offer:
10
The total amount payable is 24000
PROGRAM 1

#include <stdio.h>

int main() {
int a,b,c,d=1;
printf("Enter the value of n you want to find factorial of:\n");
scanf("%d",&a);
for(b=a;b>0;b--){
for(c=b;c>0;c--){
d=d*c;
}
printf("The factorial of %d is %d",b,d);
printf("\n");
d=1;
}

return 0;
}

OUTPUT
Enter the value of n you want to find factorial of:
6
The factorial of 6 is 720
The factorial of 5 is 120
The factorial of 4 is 24
The factorial of 3 is 6
The factorial of 2 is 2
The factorial of 1 is 1
PROGRAM 1

#include <stdio.h>

int main() {
int a,b;
printf("Enter the values of m and n to determine order of the
matrix:\n");
scanf("%d%d",&a,&b);
int c,d,e[a][b];
printf("Enter the elements of the matrix\n");
for(c=0;c<a;c++){
for(d=0;d<b;d++){
scanf("%d",&e[c][d]);
}
}
printf("The required matrix is:\n");
for(c=0;c<a;c++){
for(d=0;d<b;d++){
printf("%d",e[c][d]);
printf("\t");
}
printf("\n");
}
return 0;
}

OUTPUT
Enter the values of m and n to determine order of the matrix:
3
3
Enter the elements of the matrix
123456789
The required matrix is:
1 2 3
4 5 6
PROGRAM 1
7 8 9

#include <stdio.h>

int main() {
int a,b;
printf("Enter the values of m and n to determine order of the
matrix:\n");
scanf("%d%d",&a,&b);
int c,d,e[a][b];
int f=0,g=1;
printf("Enter the elements of the matrix\n");
for(c=0;c<a;c++){
for(d=0;d<b;d++){
scanf("%d",&e[c][d]);
}
}
for(c=0;c<a;c++){
for(d=0;d<b;d++){
f=f+e[c][d];
}
}
for(c=0;c<a;c++){
for(d=0;d<b;d++){
g=g*e[c][d];
}
}
printf("The sum of all elements of the array is %d \n",f);
printf("The sum of all elements of the array is %d",g);
return 0;
}
OUTPUT
Enter the values of m and n to determine order of the matrix:
2
2
Enter the elements of the matrix
1
2
PROGRAM 1
3
4
The sum of all elements of the array is 10
The product of all elements of the array is 24
#include <stdio.h>

int main() {
int a,b;
printf("Enter the values of m and n to determine order of the
matrix:\n");
scanf("%d%d",&a,&b);
int c,d,e[a][b];
printf("Enter the elements of the matrix\n");
for(c=0;c<a;c++){
for(d=0;d<b;d++){
scanf("%d",&e[c][d]);
}
}
printf("The required matrix is:\n");
for(c=0;c<a;c++){
for(d=0;d<b;d++){
printf("%d",e[d][c]);
printf("\t");
}
printf("\n");
}
return 0;
}

OUTPUT
Enter the values of m and n to determine order of the matrix:
2
2
Enter the elements of the matrix
1
2
3
PROGRAM 1
4
The required matrix is:
1 3
2 4

#include <stdio.h>

int main() {
int a,b;
printf("Enter the values of m and n to determine order of the
matrix:\n");
scanf("%d%d",&a,&b);
int c,d,e[a][b];
int f=0;
printf("Enter the elements of the matrix\n");
for(c=0;c<a;c++){
for(d=0;d<b;d++){
scanf("%d",&e[c][d]);
}
}
printf("The diagnol elements are:\n");
for(c=0;c<a;c++){
for(d=0;d<b;d++){
if(c==d){
printf("%d ",e[c][d]);
}
}
}
printf("\n");
printf("The sum of diagnol elements is given as: ");
for(c=0;c<a;c++){
for(d=0;d<b;d++){
if(c==d){
f=f+e[c][d];
}
}
}
printf("%d",f);
return 0;
}

OUTPUT
Enter the values of m and n to determine order of the matrix:
PROGRAM 1
2 2
Enter the elements of the matrix
1 2 3 4
The diagnol elements are:
1 4
The sum of diagnol elements is given as: 5
#include <stdio.h>

int main() {
int a,b;
printf("Enter the values of m and n to determine order of the
matrix:\n");
scanf("%d%d",&a,&b);
int c,d,e[a][b],f[a][b],g[a][b],h[a][b];
printf("Enter the elements of the matrix 1:\n");
for(c=0;c<a;c++){
for(d=0;d<b;d++){
scanf("%d",&e[c][d]);
}
}
printf("Enter the elements of the matrix 2:\n");
for(c=0;c<a;c++){
for(d=0;d<b;d++){
scanf("%d",&f[c][d]);
}
}
for(c=0;c<a;c++){
for(d=0;d<b;d++){
g[c][d]=e[c][d] + f[c][d];
h[c][d]=e[c][d] - f[c][d];
}
}
printf("The addition matrix is given as: \n");
for(c=0;c<a;c++){
for(d=0;d<b;d++){
printf("%d",g[c][d]);
printf("\t");
}
printf("\n");
}
printf("The subtraction matrix is given as: \n");
for(c=0;c<a;c++){
for(d=0;d<b;d++){
printf("%d",h[c][d]);
PROGRAM 1
printf("\t");
}
printf("\n");
}
return 0;
}

OUTPUT
Enter the values of m and n to determine order of the
matrix:
2
2
Enter the elements of the matrix 1:
1
2
3
4
Enter the elements of the matrix 2:
5
6
7
8
The addition matrix is given as:
6 8
10 12
The subtraction matrix is given as:
-4 -4
-4 -4
PROGRAM 1

#include <stdio.h>

int main() {
int a,b,i,j;
printf("Enter the values of m and n to determine order of the
matrix 1:\n");
scanf("%d%d",&a,&b);
printf("Enter the values of m and n to determine order of the
matrix 2:\n");
scanf("%d%d",&i,&j);
int c,d,e[a][b],f[a][b],g[i][j],h,k;
printf("Enter the elements of the matrix 1:\n");
for(c=0;c<a;c++){
for(d=0;d<b;d++){
scanf("%d",&e[c][d]);
}
}
printf("Enter the elements of the matrix 2:\n");
for(c=0;c<i;c++){
for(d=0;d<j;d++){
scanf("%d",&f[c][d]);
}
}
printf("The multiplied matrix is:\n");
if(b==i){
for(c=0;c<a;c++) {
for(d=0;d<j;d++){
for(h=0;h<b;h++){
k=k + e[c][h]*f[h][d];
}
printf("%d",k);
printf("\t");
k=0;
}
printf("\n");
}
}
PROGRAM 1
return 0;
}

OUTPUT
Enter the values of m and n to determine order of the
matrix 1:
2
2
Enter the values of m and n to determine order of the
matrix 2:
2
2
Enter the elements of the matrix 1:
1
2
3
4
Enter the elements of the matrix 2:
5
6
7
8
The multiplied matrix is:
19 22
PROGRAM 1
43 50

#include <stdio.h>
int swap(int,int);
int swap(int a,int b){
int c;
c=a;
a=b;
b=c;
return a,b ;
}
void swapr(int *f,int *g){
int h;
h = *f;
*f = *g;
*g = h;
}

int main() {
int d,e,i,j;
printf("Enter the values of d and e:\n");
scanf("%d%d",&d,&e);
swap(d,e);
printf("The swaped values of d and e are %d and %d respectively by
call by value function",d,e);
printf("\n");
printf("Enter the values of i and j:\n");
scanf("%d%d",&i,&j);
swapr(&i,&j);
printf("The swaped values of i and j are %d and %d respectively by
call by reference function",i,j);
return 0;
}

OUTPUT
Enter the values of d and e:
3 4
PROGRAM 1
The swaped values of d and e are 3 and 4 respectively
by call by value function
Enter the values of i and j:
5 6
The swaped values of i and j are 6 and 5 respectively
by call by reference function
#include <stdio.h>

int main() {
float a,b,c,d;
printf("Enter the values of length and breath of the rectangle you
want to find area and perimeter of:\n");
scanf("%f%f",&a,&b);
c=a*b;
d=2*(a+b);
printf("The area of rectangle is %f",c);
printf("The perimeter of rectangle is %f",d);
return 0;
}

OUTPUT
Enter the values of length and breath of the
rectangle you want to find area and perimeter of:
4
5
The area of rectangle is 20.000000
The perimeter of rectangle is 18.000000
PROGRAM 1

#include <stdio.h>

int main() {
int a,b,c=0;
printf("Enter the value of n you want to sum to:\n");
scanf("%d",&a);
for(b=a;b>0;b--){
c=c+b;
}
printf("The sum of n natural number is given as %d",c);
return 0;
}

OUTPUT
Enter the value of n you want to sum to:
7
The sum of n natural number is given as 28
PROGRAM 1

#include <stdio.h>

int main() {
int a,b,c=0,d;
printf("Enter the number you want to find sum of digits of:\n");
scanf("%d",&a);
for(b=a;b>0;b/=10){
d=b%10;
c=c+d;
}
printf("The required sum is given as %d",c);
return 0;
}

OUTPUT
Enter the number you want to find sum of digits of:
85642
The required sum is given as 25
PROGRAM 1

#include <stdio.h>
#include <math.h>

int main() {
int a,b,c=0,d,e,f=0;
printf("Enter the number you want to find reverse of:\n");
scanf("%d",&a);
for(b=a;b>0;b/=10){
f++;
}
e=f-1;
for(b=a;b>0;b/=10){
d=b%10;
c=c+d*pow(10,e);
e--;
}
printf("The reverse is given as %d",c);
return 0;
}

OUTPUT
Enter the number you want to find reverse of:
1873
The reverse is given as 3781
PROGRAM 1

#include <stdio.h>
#include <math.h>

int main() {
int a,b,c=0,d,e,f=0;
printf("Enter the number you want to check if it is palindrome:\
n");
scanf("%d",&a);
for(b=a;b>0;b/=10){
f++;
}
e=f-1;
for(b=a;b>0;b/=10){
d=b%10;
c=c+d*pow(10,e);
e--;
}
if(a==c){
printf("The given number is palindrome.");
}
else{
printf("The given number is not a palindrome.");
}
return 0;
}

OUTPUT
Enter the number you want to check if it is
palindrome:
123676321
The given number is not a palindrome.
PROGRAM 1

#include <stdio.h>
#include <math.h>

int main() {
int a,b,c=0,d,e,f=0;
printf("Enter the number you want to check if it is armstrong
number:\n");
scanf("%d",&a);
for(b=a;b>0;b/=10){
f++;
}
e=f-1;
for(b=a;b>0;b/=10){
d=b%10;
c=c+d*d*d;
}
if(a==c){
printf("The given number is armstrong number.");
}
else{
printf("The given number is not a armstrong number.");
}
return 0;
}

OUTPUT
Enter the number you want to check if it is armstrong
number:
153
The given number is armstrong number.

Enter the number you want to check if it is armstrong


number:
156
The given number is not a armstrong number.
PROGRAM 1

#include <stdio.h>
#include <math.h>

int main() {
int a,b,c;
float d,e,f;
printf("Enter the coefficient of x^2:\n");
scanf("%d",&a);
printf("Enter the coefficient of x:\n");
scanf("%d",&b);
printf("Enter the constant:\n");
scanf("%d",&c);
d= sqrt((b*b)-4*a*c);
e= (-b + d)/2*a;
f= (-b - d)/2*a;
printf("The roots of the given equation is given as %f and
%f",e,f);
return 0;
}

OUTPUT
Enter the coefficient of x^2:
1
Enter the coefficient of x:
-3
Enter the constant:
2
The roots of the given equation is given as 2.000000
and 1.000000
PROGRAM 1

#include <stdio.h>

int main() {
int a;
printf("Enter the value of size of the array:\n");
scanf("%d",&a);
int b[a],c,d;
printf("Enter the elements of the array:\n");
for(c=0;c<a;c++){
scanf("%d",&b[c]);
}
printf("Enter the number you want to check:\n");
scanf("%d",&d);
for(c=0;c<a;c++){
if(b[c]==d){
printf("\nThe number you want to check is at location %d in
array ",c+1);
}
}
return 0;
}

OUTPUT
Enter the value of size of the array:
5
Enter the elements of the array:
1
4
6
8
5
Enter the number you want to check:
PROGRAM 1
8

The number you want to check is at location 4 in


array

#include <stdio.h>

int main() {
int a;
printf("Enter the value of size of the array:\n");
scanf("%d",&a);
int b[a],c,d,e;
printf("Enter the elements of the array:\n");
for(c=0;c<a;c++){
scanf("%d",&b[c]);
}
for(d=a;d>0;d--){
for(c=0;c<d;c++){
if(b[c]>b[c+1]){
e=b[c];
b[c]=b[c+1];
b[c+1]=e;
}
}
}
printf("The sorted array is given as:\n");
for(c=0;c<a;c++){
printf("%d \t",b[c]);
}
return 0;
}

OUTPUT
Enter the value of size of the array:
5
Enter the elements of the array:
2
6
9
PROGRAM 1
1
7
The sorted array is given as:
1 2 6 7 9
#include <stdio.h>
int fact(int);
int fact(int a){
int b,c=1;
for(b=a;b>0;b--){
c=c*b;
}
return c;
}
int main() {
int d,e;
printf("Enter the number you want to find factorial of:\n");
scanf("%d",&d);
e=fact(d);
printf("The factorial of %d is %d",d,e);
return 0;
}

OUTPUT
Enter the number you want to find factorial of:
7
The factorial of 7 is 5040
PROGRAM 1

#include <stdio.h>

int main() {
int a,b,c;
printf("Enter the number you want to check if it is prime:\n");
scanf("%d",&a);
for(b=2;b<a;b++){
if(a%b==0){
printf("The given number is not a prime number");
break;
}
}
if(b==a){
printf("The given number is prime number");
}

return 0;
}

OUTPUT
Enter the number you want to check if it is prime:
23
The given number is prime number

Enter the number you want to check if it is prime:


24
The given number is not a prime number
PROGRAM 1

#include <stdio.h>

int main() {
int a,b,c,d;
printf("Enter the numbers you want to find prime numbers between:\
n");
scanf("%d%d",&a,&c);
printf("The prime numbers between %d and %d are ",a,c);
for(d=a;d<=c;d++){
for(b=2;b<d;b++){
if(d%b==0){
break;
}
}
if(b==d){
printf("%d\t",d);
}
}
return 0;
}

OUTPUT
Enter the numbers you want to find prime numbers
between:
100
200
The prime numbers between 100 and 200 are 101 103
107 109 113 127 131 137 139
149 151 157 161 163 167 173
179 181 191 193 197 199
PROGRAM 1

You might also like