Computer Science PRATICAL ONE
Computer Science PRATICAL ONE
( C programming questions )
Grade :11
1. Write a program in C to calculate the factorial of a given
number.
#include <stdio.h>
int main()
{
int i, fact, value;
fact =1;
printf("Enter a number:");
scanf("%d", &value);
for(i=1;i<=value;i++)
{
fact=fact*i;
}
printf("The factorial of %d is %d", value, fact);
return 0;
}
Output:
Enter a number: 4
The factorial of 4 is 24
2. Write a program to calculate and print the average of three
numbers.
#include <stdio.h>
int main()
{
int i,j,k,sum,average;
printf("Enter any three numbers");
scanf("%d,%d,%d",&i,&j,&k);
sum=i+j+k;
printf("Sum of these number is %d", sum);
average=sum/3;
printf("\n The average of three number is:%d",average);
return 0;
}
Output:
Enter any three numbers1,2,3
Sum of these number is 6
The average of three number is:2
3. Write a program in C to find greatest among three numbers.
#include <stdio.h>
int main()
{
int i,j,k;
printf("Enter any three numbers:");
scanf("%d,%d,%d",&i,&j,&k);
if(i>j&&i>k)
{
printf("%d is the greatest among these three
numbers",i);
}
else if (j>i&&j>k)
{
printf("%d is the greatest among these three
numbers",j);
}
else
{
printf("%d is the greatest among these three
numbers",k);
}
return 0;
}
Output:
Enter any three numbers:23,45,12
45 is the greatest among these three number
4. Write a program to print the factorial of numbers from 1 to
n.
#include <stdio.h>
int main()
{
int n,i,fact,value;
fact=1;
value=1;
n=1;
printf("Enter the terms");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
printf("\n The factorial of number of %d is
%d",value,fact);
value++;
}
return 0;
}
Output:
Enter the terms 3
#include <stdio.h>
int main()
{
int i,a,b;
i=1;
printf("Enter a number");
scanf("%d",&a);
do
{
b=a*i;
printf("\n %d*%d=%d",a,i,b);
i++;
}
while(i<=10);
return 0;
}
Output:
Enter a number 5
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
6. Write a program to calculate the power of a number
#include <math.h>
#include <stdio.h>
int main()
{
int base, exp, result;
printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter an exponent: ");
scanf("%d", &exp);
result = pow(base, exp);
printf("%d^%d = %d", base, exp, result);
return 0;
}
Output:
Enter a base number: 2
Enter an exponent: 3
2^3 = 8
7. Write a program to swap the values of two variables without
using a temporary variable.
#include <stdio.h>
int main()
{
int i,j;
printf("Enter the value of i:");
scanf("%d",&i);
printf("Enter the value of j:");
scanf("%d",&j);
//code to swap 'i' and 'j'
i=i+j;
j=i-j;
i=i-j;
printf("After swaping: i=%d, j=%d",i,j);
return 0;
Output:
Enter the value of i:2
Enter the value of j:3
After swaping: i=3, j=2
8. Write a program to calculate simple interest given principle,
rate, and time.
#include <stdio.h>
int main()
{
int p,t,r,i;
printf("Enter the value of principle:");
scanf("%d",&p);
printf("Enter the value of time:");
scanf("%d",&t);
printf("Enter the value of rate:");
scanf("%d",&r);
i=p*t*r;
i=i/100;
printf("simple interest=%d",i);
return 0;
}
Output:
Enter the value of principle:300
Enter the value of time:2
Enter the value of rate:3
simple interest=18
9. Write a program to calculate the sum of all elements in an
array.
#include <stdio.h>
int main()
{
int a[100];
int i, n, sum = 0;
Output:
#include <stdio.h>
int main()
{
int n, i, j, a[100], b[100];
Output:
Enter the number of elements in array
5
Enter the array elements
12345
Reverse array is
5 4 3 2 1
11. Write a program to count the number of characters in a
string.
#include <stdio.h>
#include<string.h>
int main()
{
char str[50];
int len;
printf("Enter a string:");
scanf("%s",str);
len=strlen(str);
printf("The length of string is %d",len);
return 0;
}
Output:
Enter a string: apple
The length of string is 5
12. Write a program to calculate the length of the string
without using the built-in function.
#include <stdio.h>
int main()
{
char str[100];
int i,length=0;
Output:
Enter a string: apple
Length of input string: 5
13. Implement a program to find the sum of digits of a
number.
#include <stdio.h>
int main()
{
int n, sum = 0;
printf("Enter the number ");
scanf("%d", &n);
printf("The number is = %d\n", n);
while (n!= 0)
{
sum += n % 10;
n= n / 10;
}
printf("Sum: %d\n", sum);
return 0;
}
Output:
Enter the number 122344
The number is = 122344
Sum: 16
14. Implement a program to find the Fibonacci
series.(1,1,2,3,5,…….up to 10 terms)
#include <stdio.h>
int main()
{
int a=1,b=1,c;
int count=1;
while(count<=10)
{
printf("%d\t",a);
c=a+b;
a=b;
b=c;
count++;
}
return 0;
}
Output:
1 1 2 3 5 8 13 21 34 55
15.Write a C program to check whether a number is prime or
not.
#include <stdio.h>
int main()
{
int n,c=2;
printf("Enter a number");
scanf("%d",&n);
for(c=2;c<=n-1;c++)
{
if(n%c==0)
{
printf("%d is not prime number\n",n);
break;
}
}
if (c==n)
{
printf("%d is prime number\n",n);
}
return 0;
}
Output:
Enter a number 23
23 is prime number
16. Write a C program to check whether a given string is
palindrome or not.
#include <stdio.h>
int main()
{
int n, rev = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
while (n != 0)
{
remainder = n % 10;
rev= rev* 10 + remainder;
n /= 10;
}
if (original == rev)
{
printf("%d is a palindrome.", original);
}
else
{
printf("%d is not a palindrome.", original);
}
return 0;
}
Output:
Enter an integer: 1221
1221 is a palindrome.
17. Write a C program to count the number of vowels and
consonants in a given string.
#include <stdio.h>
int main()
{
char sentence[80];
int i, vowels = 0, consonants = 0;
printf("Enter a sentence:");
scanf("%s",sentence);
for (i = 0; sentence[i] != '\0'; i++)
{
if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i]
== 'i' || sentence[i] == 'o' || sentence[i] == 'u') ||
(sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] ==
'I' || sentence[i] == 'O' || sentence[i] == 'U'))
{
vowels = vowels + 1;
}
else
{
consonants = consonants + 1;
}
}
printf("No. of vowels in %s = %d\n", sentence, vowels);
printf("No. of consonants in %s = %d\n", sentence,
consonants);
}
Output:
Enter a sentence: computer
No. of vowels in computer = 3
No. of consonants in computer = 5
18. Write a C program to sort an array of integers using bubble
sort.
#include <stdio.h>
int main()
{
int i,j,n,num[100],temp;
printf("\nEnter the size of an array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter array elements:");
scanf("%d",&num[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf(" An array of integers sorted by using bubble
sort are:");
for(i=0;i<n;i++)
{
printf("\n %d",num[i]);
}
return 0;
}
Output:
Enter the size of an array:5
Enter array elements:21
Enter array elements:45
Enter array elements:23
Enter array elements:67
Enter array elements:12
An array of integers sorted by using bubble sort are:
12
21
23
45
67
19. Write a C program to transpose a matrix.
#include <stdio.h>
int main()
{
int a[10][10],transpose[10][10],r,c,i,j;
printf("Enter rows and columns: ");
scanf("%d %d",&r,&c);
printf("\nEnter matrix elements:\n");
for(i=0;i<r;++i)
{
for(j=0;j<c;++j)
{
printf("Enter element a%d%d:",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nEntered matrix:\n");
for(i=0;i<r;++i)
{
for(j=0;j<c;++j)
{
printf("%d\t",a[i][j]);
if(j==c-1)
{
printf("\n");
}
}
}
for(i=0;i<r;++i)
{
for(j=0;j<c;++j)
{
transpose[j][i]=a[i][j];
}
}
printf("\n Transpose of the matrix;\n");
for (i=0;i<c;++i)
{
for(j=0;j<r;++j)
{
printf("%d\t",transpose[i][j]);
if(j==r-1)
{
printf("\n");
}
}
}
return 0;
}
Output:
Enter rows and columns: 2 3
Entered matrix:
1 4 0
-5 2 7
#include <stdio.h>
int main()
{
int r1,c1,r2,c2;
printf("Enter rows and columns of array 1:");
scanf("%d %d",&r1,&c1);
printf("Enter the rows and columns of array 2:");
scanf("%d %d",&r2,&c2);
if(c1!=r2)
{
printf("Matrix multiplication is not possible");
}
else
{
int a[r1][c1],b[r2][c2],i,j,k,product[r1][c2];
printf("Enter the details of an array 1:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("Enter value in %d %d index:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Enter the details of an array 2:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("Enter the value in %d %d index:",i,j);
scanf("%d",&b[i][j]);
}
}
//array multiplication
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
product[i][j]=0;
for(k=0;k<c1;k++)
{
product[i][j]+=a[i][k]*b[k][j];
}
printf("%d\t",product[i][j]);
}
printf("\n");
}
}
return 0;
}
Output:
Enter rows and columns of array 1:2 3
Enter the rows and columns of array 2:3 2
Enter the details of an array 1:
Enter value in 0 0 index:2
Enter value in 0 1 index:2
Enter value in 0 2 index:2
Enter value in 1 0 index:2
Enter value in 1 1 index:2
Enter value in 1 2 index:2
Enter the details of an array 2:
Enter the value in 0 0 index:2
Enter the value in 0 1 index:2
Enter the value in 1 0 index:2
Enter the value in 1 1 index:2
Enter the value in 2 0 index:2
Enter the value in 2 1 index:2
12 12
12 12
21. Implement a program to convert a decimal number to
binary.
#include <stdio.h>
int main()
{
int d,n,sum=0,i=1;
printf("\nEnter a decimal number:");
scanf("%d",&n);
while(n!=0)
{
d=n%2;
sum=sum+d*i;
n=n/2;
i*=10;
}
printf("The binary number is %d",sum);
return 0;
}
Output:
Enter a decimal number:234
The binary number is 11101010
22. Implement a program to convert a binary number to
decimal.
#include <stdio.h>
#include <math.h>
int main()
{
int d,n,n1,p=0,sum=0;
printf("\nEnter a binary number:");
scanf("%d",&n);
n1=n;
while(n!=0)
{
d=n%10;
sum=sum+pow(2,p)*d;
n=n/10;
p++;
}
printf("Decimal equivalent of %d=%d",n1,sum);
return 0;
}
Output:
Enter a binary number:10101
Decimal equivalent of 10101=21
23. Write a C program to calculate the area and perimeter of
rectangle.
#include <stdio.h>
int main()
{
int l,b,a,p;
printf("Enter the length of the rectangle:");
scanf("%d",&l);
printf("Enter the breadth of the reactangle:");
scanf("%d",&b);
a=l*b;
printf("Area of rectangle is:%d",a);
p=2*(l+b);
printf("\nperimeter of rectangle is:%d",p);
return 0;
}
Output:
Enter the length of the rectangle:5
Enter the breadth of the reactangle:3
Area of rectangle is:15
perimeter of rectangle is:16
24. Write a C program to find the LCM (Least Common
Multiple) of two numbers.
#include <stdio.h>
int main()
{
int n1,n2,max;
printf("Enter two positive numbers:");
scanf("%d %d",&n1,&n2);
max=(n1>n2) ? n1:n2;
while(1)
{
if ((max % n1==0)&&(max % n2 ==0))
{
printf("The LCM of %d and %d is %d",n1,n2,max);
break;
}
++max;
}
return 0;
}
Output:
Enter two positive numbers:2 4
The LCM of 2 and 4 is 4
25. Write a C program to check whether a given number is
Armstrong or not.
#include <stdio.h>
int main()
{
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0)
{
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
if (result == num)
{
printf("%d is an Armstrong number.", num);
}
else
{
printf("%d is not an Armstrong number.", num);
}
return 0;
}
Output:
Enter a three-digit integer: 371
371 is an Armstrong number.
26. Implement a program to calculate the sum of the series:
1+1/2+1/3+…+1/n.
#include<stdio.h>
int main() {
int num, i, sum = 0;
printf("Input any number: ");
scanf("%d", &num);
printf("1 + ");
for(i = 2; i <= num - 1; i++)
{
printf(" 1/%d +", i);
}
for(i = 1; i <= num; i++)
{
sum = sum + i;
}
printf(" 1/%d", num);
printf("\nSum = 1/%d", sum + 1/num);
return 0;
}
Output:
Input any number: 3
1 + 1/2 + 1/3
Sum = 1/6
27. Convert number of days into year , month , day , in C
programming.
#include<stdio.h>
int main()
{
int day,years,months,days,d1;
printf("Enter days:");
scanf("%d",&day);
years=day/365;
d1=day%365;
months=d1/30;
days=d1%30;
printf("Years=%d Months=%d
Days=%d",years,months,days);
return 0;
}
Output:
Enter days:400
Years=1 Months=1 Days=5
28. Check whether a year is leap year or not in C
programming.
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0)
{
printf("%d is a leap year.", year);
}
else if (year % 100 == 0)
{
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0)
{
printf("%d is a leap year.", year);
}
else
{
printf("%d is not a leap year.", year);
}
return 0;
}
Output:
Enter a year: 2008
2008 is a leap year.
29. Check whether a character is an alphabet or not using C
programming.
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
printf("%c is an alphabet.", c);
}
else
{
printf("%c is not an alphabet.", c);
}
return 0;
}
Output:
Enter a character: apple
a is an alphabet.
30. Write a program to input 100 students marks and arrange
them in ascending order and print them.
#include <stdio.h>
int main()
{
int i,j,n,num[100],t;
printf("Enter the number of the students:");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Enter the marks of the students:");
scanf("%d",&num[i]);
}
for(i=0;i<n-1;i++)
{
for (j=i+1;j<n;j++)
{
if (num[i]>num[j])
{
t=num[i];
num[i]=num[j];
num[j]=t;
}
}
}
printf("Entered marks of the students are in ascending
order:");
for(i=0;i<n;i++)
{
printf("%d\t",num[i]);
}
return 0;
}
Output:
Enter the number of the students:10
Enter the marks of the students:78
Enter the marks of the students:98
Enter the marks of the students:67
Enter the marks of the students:88
Enter the marks of the students:82
Enter the marks of the students:74
Enter the marks of the students:93
Enter the marks of the students:77
Enter the marks of the students:86
Enter the marks of the students:91
Entered marks of the students are in ascending order: 67
74 77 78 82 86 88 91 93 98
31. WAP to input 100 student’s marks and display highest
marks.
#include <stdio.h>
int main()
{
int i,n,num[100],max;
printf("Enter the number of the students:");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Enter the marks of the students:");
scanf("%d",&num[i]);
}
max=num[0];
for(i=1;i<n;i++)
{
if(num[i]>max)
{
max=num[i];
}
}
printf("\nHighest mark among these students
is:%d",max);
return 0;
}
Output:
Enter the number of the students:10
Enter the marks of the students:78
Enter the marks of the students:88
Enter the marks of the students:86
Enter the marks of the students:74
Enter the marks of the students:98
Enter the marks of the students:88
Enter the marks of the students:72
Enter the marks of the students:84
Enter the marks of the students:93
Enter the marks of the students:87
{
int a1[3][2],a2[2][5],i,j,k,sum[3][5];
printf("Enter the details of an array 1:\n");
for (i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("Enter the value in %d%d
position:",i,j);
scanf("%d",&a1[i][j]);
}
}
printf("enter the details of an array 2:\n");
for (i=0;i<2;i++)
{
for(j=0;j<5;j++)
{
printf("Enter the value in %d%d
position:",i,j);
scanf("%d",&a2[i][j]);
}
}
printf("The sum of two array is:\n");
for (i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
sum[i][k]=0;
for(k=0;k<2;k++)
{
sum[i][j]=a1[i][k]+a2[k][j];
}
printf("%d\t",sum[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter the details of an array 1:
Enter the value in 00 position:2
Enter the value in 01 position:2
Enter the value in 10 position:2
Enter the value in 11 position:2
Enter the value in 20 position:2
Enter the value in 21 position:2
enter the details of an array 2:
Enter the value in 00 position:2
Enter the value in 01 position:2
Enter the value in 02 position:2
Enter the value in 03 position:2
Enter the value in 04 position:2
Enter the value in 10 position:2
Enter the value in 11 position:2
Enter the value in 12 position:2
Enter the value in 13 position:2
Enter the value in 14 position:2
The sum of two array is:
4 4 4 4 4
4 4 4 4 4
4 4 4 4 4
2. 2D Array Multiplication:
Implement a program to multiply two 2D Arrays and
display the resulting matrix.
[A1]3*2*[A2]2*4
#include<stdio.h>
int main()
{
int a1[3][2],a2[2][4],i,j,k,product[3][4];
printf("Enter the details of an array 1:\n");
for (i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("Enter the value in %d%d
position:",i,j);
scanf("%d",&a1[i][j]);
}
}
printf("Enter the details of an array 2:\n");
for (i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
printf("Enter the value in %d%d
position:",i,j);
scanf("%d",&a2[i][j]);
}
}
printf("The resultant matrix multiplication is:\n");
for (i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
product[i][k]=0;
for(k=0;k<2;k++)
{
product[i][j]=a1[i][k]*a2[k][j];
}
printf("%d\t",product[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter the details of an array 1:
Enter the value in 00 position:3
Enter the value in 01 position:3
Enter the value in 10 position:3
Enter the value in 11 position:3
Enter the value in 20 position:3
Enter the value in 21 position:3
Enter the details of an array 2:
Enter the value in 00 position:3
Enter the value in 01 position:3
Enter the value in 02 position:3
Enter the value in 03 position:3
Enter the value in 10 position:3
Enter the value in 11 position:3
Enter the value in 12 position:3
Enter the value in 13 position:3
The resultant matrix multiplication is:
9 9 9 9
9 9 9 9
9 9 9 9
3. 2D Array Subtraction:
Write a program to subtract one 2D array from another
and print the resultant matrix.
[A1]4*6-[A2]6*3
#include<stdio.h>
int main()
{
int a1[4][6],a2[6][3],i,j,k,diff[4][3];
printf("Enter the details of an array 1:\n");
for (i=0;i<4;i++)
{
for(j=0;j<6;j++)
{
printf("Enter the value in %d%d
position:",i,j);
scanf("%d",&a1[i][j]);
}
}
printf("Enter the details of an array 2:\n");
for (i=0;i<6;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the value in %d%d
position:",i,j);
scanf("%d",&a2[i][j]);
}
}
printf("The resultant matrix multiplication is:\n");
for (i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
diff[i][k]=0;
for(k=0;k<6;k++)
{
diff[i][j]=a1[i][k]-a2[k][j];
}
printf("%d\t",diff[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter the details of an array 1:
Enter the value in 00 position:3
Enter the value in 01 position:3
Enter the value in 02 position:3
Enter the value in 03 position:3
Enter the value in 04 position:3
Enter the value in 05 position:3
Enter the value in 10 position:3
Enter the value in 11 position:3
Enter the value in 12 position:3
Enter the value in 13 position:3
Enter the value in 14 position:3
Enter the value in 15 position:3
Enter the value in 20 position:3
Enter the value in 21 position:3
Enter the value in 22 position:3
Enter the value in 23 position:3
Enter the value in 24 position:3
Enter the value in 25 position:3
Enter the value in 30 position:3
Enter the value in 31 position:3
Enter the value in 32 position:3
Enter the value in 33 position:3
Enter the value in 34 position:3
Enter the value in 35 position:3
Enter the details of an array 2:
Enter the value in 00 position:1
Enter the value in 01 position:1
Enter the value in 02 position:1
Enter the value in 10 position:1
Enter the value in 11 position:1
Enter the value in 12 position:1
Enter the value in 20 position:1
Enter the value in 21 position:1
Enter the value in 22 position:1
Enter the value in 30 position:1
Enter the value in 31 position:1
Enter the value in 32 position:1
Enter the value in 40 position:1
Enter the value in 41 position:1
Enter the value in 42 position:1
Enter the value in 50 position:1
Enter the value in 51 position:1
Enter the value in 52 position:1
The resultant matrix multiplication is:
2 2 2
2 2 2
2 2 2
2 2 2
4. Bubble Sort Algorithm:
Implement the bubble sort algorithm to sort an array in
ascending order.
#include<stdio.h>
int main()
{
int i,j,n,num[100],t;
printf("Enter the size of array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter array elements:");
scanf("%d",&num[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]>num[j])
{
t=num[i];
num[i]=num[j];
num[j]=t;
}
}
}
printf("Numbers in ascending order are:\n");
for(i=0;i<n;i++)
{
printf("%d\n",num[i]);
}
return 0;
}
Output:
Enter the size of array:5
Enter array elements:78
Enter array elements:84
Enter array elements:66
Enter array elements:75
Enter array elements:82
Numbers in ascending order are:
66
75
78
82
84