BSC - C Language Practical

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

B.Sc.

(Computer Science) || C language Practical

C language Practical
1. WAP to print the sum and product of digits of an integer.
2. WAP to reverse a number.
3. WAP to compute the sum of the first n terms of the following series S = 1+1/2+1/3+1/4+……
4. WAP to compute the sum of the first n terms of the following series S =1-2+3-4+5…………….
5. Write a function that checks whether a given string is Palindrome or not. Use this function to
find whether the string entered by user is Palindrome or not.
6. Write a function to find whether a given no. is prime or not. Use the same to generate the prime
numbers less than 100.
7. WAP to compute the factors of a given number.
8. Write a macro that swaps two numbers. WAP to use it.
9. WAP to print a triangle of stars as follows (take number of lines from user):
*
***
*****
*******
*********
10. WAP to perform following actions on an array entered by the user:
i) Print the even-valued elements
ii) Print the odd-valued elements
iii) Calculate and print the sum and average of the elements of array
iv) Print the maximum and minimum element of array
v) Remove the duplicates from the array
vi) Print the array in reverse order
The program should present a menu to the user and ask for one of the options. The menu should
also include options to re-enter array and to quit the program.
11. WAP that prints a table indicating the number of occurrences of each alphabet in the text
entered as command line arguments.
12. Write a program that swaps two numbers using pointers.
13. Write a program in which a function is passed address of two variables and then alter its
contents.
14. Write a program which takes the radius of a circle as input from the user, passes it to another
function that computes the area and the circumference of the circle and displays the value of area
and circumference from the main() function.
15. Write a program to find sum of n elements entered by the user. To write this program, allocate
memory dynamically using malloc() / calloc() functions or new operator.
16. Write a menu driven program to perform following operations on strings:
a) Show address of each character in string
b) Concatenate two strings without using strcat function.
c) Concatenate two strings using strcat function.
d) Compare two strings
e) Calculate length of the string (use pointers)
f) Convert all lowercase characters to uppercase
g) Convert all uppercase characters to lowercase
h) Calculate number of vowels
i) Reverse the string
17. Given two ordered arrays of integers, write a program to merge the two-arrays to get an
ordered array.
18. WAP to display Fibonacci series (i)using recursion, (ii) using iteration
19. WAP to calculate Factorial of a number (i)using recursion, (ii) using iteration
20. WAP to calculate GCD of two numbers (i) with recursion (ii) without recursion.

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

21. Create Matrix class using templates. Write a menu-driven program to perform following
Matrix operations (2-D array implementation).
a) Sum b) Difference c) Product d) Transpose
22. Write a menu-driven program, using user-defined functions to find the area of rectangle,
square, circle and triangle by accepting suitable input parameters from user.
23. WAP to display the first n terms of Fibonacci series.
24. WAP to find factorial of the given number.
25. WAP to find sum of the following series for n terms: 1 – 2/2! + 3/3! --------- n/n!
26. WAP to calculate the sum and product of two compatible matrices.

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

Solution
1. WAP to print the sum and product of digits of an integer.
/* WAP to print the sum and product of digits of an integer. */
#include<stdio.h>
int main(){
int n;
int s,p;
printf("\nEnter a number:");
scanf("%d",&n);
/* Calculate sum and product*/
s=0;
p=1;
while(n>0){
s=s+n%10;
p=p*(n%10);
n=n/10;
}
printf("\nSum=%d and Product=%d",s,p);
}
2. WAP to reverse a number.
/* WAP to reverse a number. */
#include<stdio.h>
int main(){
int n;
int s;
printf("\nEnter a number:");
scanf("%d",&n);
/* Calculate Reverse */
s=0;
while(n>0){
s=s*10+n%10;
n=n/10;
}
printf("\nReverse=%d",s);
}
3. WAP to compute the sum of the first n terms of the following series S =
1+1/2+1/3+1/4+1/n……
/* 3. WAP to compute the sum of the first n terms of the following series S =
1+1/2+1/3+1/4+……+1/n */
#include<stdio.h>
int main(){
double n,i,s;
printf("\nEnter a number:");
scanf("%lf",&n);
s=0;
i=1;
while(i<=n){
s=s+(1/i);
i++;
}
printf("\nSum of the series = %.2lf",s);

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

}
4. WAP to compute the sum of the first n terms of the following series S =1-2+3-4+5…………….
/* 4. WAP to compute the sum of the first n terms of the following series S=1-2+3-
4+5……………. */
#include<stdio.h>
int main(){
int n,i,odd,even,s;
printf("\nEnter a number:");
scanf("%d",&n);
s=0;
odd=1;
even=2;
while(n>0){
s=s+(odd-even);
odd+=2;
even+=2;
n--;
}
printf("\nSum of the series = %d",s);
}
5. Write a function that checks whether a given string is Palindrome or not. Use this function to
find whether the string entered by user is Palindrome or not.
/* 5. Write a function that checks whether a given string is Palindrome or not.
Use this function to find whether the string entered by user is Palindrome or not.
*/
#include<stdio.h>
int findpalindrome(char []);
int main(){
char str[20];
int f;
printf("\nEnter a string:");
scanf("%s",str);
f=findpalindrome(str);
if(f==0)
printf("\n%s is not Palindrome",str);
else
printf("\n%s is Palindrome",str);
}
int findpalindrome(char s[]){
int f; /* f=1: string is palindrome otherwise not */
int i,j;
/*move to the end of string */
for(i=0;s[i]!='\0';i++);
i--; /*move one position backward to avoid null */
/*compare first with last */
for(f=1,j=0;f==1 && s[j]!='\0';i--,j++){
if(s[i]!=s[j])
f=0;
}
return(f);
}

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

6. Write a function to find whether a given no. is prime or not. Use the same to generate the prime
numbers less than 100.
/* 6. Write a function to find whether a given no. is prime or not.
Use the same to generate the prime numbers less than 100.
*/
#include<stdio.h>
int findpalindrome(int);
int main(){
int n,i;
printf("\nEnter a number:");
scanf("%d",&n);
printf("\nDisplaying all prime between 1 to %d\n",n);
for(i=1;i<=n;i++){
if(findpalindrome(i))
printf("%d,",i);
}
}
int findpalindrome(int n){
int s,m;
m=n;
s=0;
/*reverse the number */
while(m>0){
s=s*10+m%10;
m=m/10;
}
if(n==s)
return(1);
else
return(0);
}
7. WAP to compute the factors of a given number.
/*
7. WAP to compute the factors of a given number.
*/

#include<stdio.h>
int main(){
int n,i;
printf("\nEnter a number:");
scanf("%d",&n);
printf("\nDisplaying Factors of %d\n",n);
for(i=1;i<=n;i++){
if(n%i==0)
printf("%d,",i);
}
}
8. Write a macro that swaps two numbers. WAP to use it.
/*
8. Write a macro that swaps two numbers. WAP to use it.
*/

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

#include<stdio.h>
#define SWAP(x,y) int t;t=x;x=y;y=t;
int main(){
int a,b;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
printf("\nBefore swap A=%d, B=%d",a,b);
SWAP(a,b);
printf("\nAfter swap A=%d, B=%d",a,b);
return 0;
}
9. WAP to print a triangle of stars as follows (take number of lines from user):
*
***
*****
*******
*********
/*
9. WAP to print a triangle of stars as follows (take number of lines from user):
*
***
*****
*******
*********

*/

#include<stdio.h>
int main(){
int n,i,j;
printf("Enter number of lines:");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i*2-1;j++){
printf("* ");
}
printf("\n");
}
}
10. WAP to perform following actions on an array entered by the user:
i) Print the even-valued elements
ii) Print the odd-valued elements
iii) Calculate and print the sum and average of the elements of array
iv) Print the maximum and minimum element of array
v) Remove the duplicates from the array
vi) Print the array in reverse order
The program should present a menu to the user and ask for one of the options. The menu should
also include options to re-enter array and to quit the program.
/*
10. WAP to perform following actions on an array entered by the user:
i) Print the even-valued elements

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

ii) Print the odd-valued elements


iii) Calculate and print the sum and average of the elements of array
iv) Print the maximum and minimum element of array
v) Remove the duplicates from the array
vi) Print the array in reverse order
The program should present a menu to the user and ask for one of the options. The menu should
also include options to re-enter array and to quit the program.
*/
#define MAX 5
#include<stdio.h>
void printarray(int[]);
int main(){
int ar[MAX],ar1[MAX],i,j,k,s,av,min,max,ch;
for(i=0;i<MAX;i++){
printf("\nEnter Data:");
scanf("%d",&ar[i]);
}
do{
printf("\n1. Print the even-valued elements");
printf("\n2. Print the odd-valued elements");
printf("\n3. Calculate and print the sum and average of the elements of array");
printf("\n4. Print the maximum and minimum element of array");
printf("\n5. Remove the duplicates from the array");
printf("\n6. Print the array in reverse order");
printf("\n7. Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
if(ch==1){
printarray(ar);
printf("\nEven Valued:");
for(i=0;i<MAX;i++){
if(ar[i]%2==0)
printf("%d,",ar[i]);
}
}
else if(ch==2){
printarray(ar);
printf("\nOdd Valued:");
for(i=0;i<MAX;i++){
if((ar[i]%2)!=0)
printf("%d,",ar[i]);
}
}
else if(ch==3){
printarray(ar);
for(i=0,s=0;i<MAX;i++){
s=s+ar[i];
}
av=s/MAX;
printf("\nSum = %d, Average = %d",s,av);
}

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

else if(ch==4){
printarray(ar);
max=min=ar[0];
for(i=0;i<MAX;i++){
if(ar[i]>max)
max=ar[i];
if(ar[i]<min)
min=ar[i];
}
printf("\nMax = %d, Min = %d",max,min);
}
else if(ch==5){
printarray(ar);
for(i=0,k=0;i<MAX;i++){
for(j=0;j<k;j++){
if(ar[i]==ar1[j]){
break;
}
}
if(j==k){
ar1[k]=ar[i];
k++;
}
}
printf("\nContent of array after removing duplicate elements:");
printarray(ar1);
}
else if(ch==6){
printarray(ar);
printf("\nPrint array in reverse order:");
for(i=MAX-1;i>=0;i--)
printf("%d,",ar[i]);
}
else{
printf("\nEntered wrong choice");
}
}while(ch!=7);
}
void printarray(int a[]){
int i;
printf("\nContent of Array:");
for(i=0;i<MAX;i++)
printf("%d,",a[i]);
}
11. WAP that prints a table indicating the number of occurrences of each alphabet in the text
entered as command line arguments.
/*
11. WAP that prints a table indicating the number of occurrences of each alphabet in the text
entered as command line arguments.
*/
#include <stdio.h>

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
int alphabetCount[26] = {0}; /* Array to store the count of each alphabet */
int i, j;

/* Loop through each argument passed */


for (i = 1; i < argc; i++) {
char *word = argv[i];
/* Loop through each character in the argument */
for (j = 0; word[j] != '\0'; j++) {
/* Convert to lowercase and increment the count if it's an alphabet */
if (isalpha(word[j])) {
alphabetCount[tolower(word[j]) - 'a']++;
}
}
}

/* Print the table header */


printf("Alphabet | Count\n");
printf("---------|------\n");

/* Print the count of each alphabet */


for (i = 0; i < 26; i++) {
if (alphabetCount[i] > 0) { /* Print only if the count is non-zero */
printf(" %c | %d\n", i + 'a', alphabetCount[i]);
}
}

return 0;
}
12. Write a program that swaps two numbers using pointers.
/*
12. Write a program that swaps two numbers using pointers.
*/
#include<stdio.h>
int main(){
int a,b,t,*x,*y;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
printf("\nBefore swap A=%d, B=%d",a,b);
x=&a;
y=&b;
t=*x;
*x=*y;
*y=t;
printf("\nAfter swap A=%d, B=%d",a,b);
return 0;
}
13. Write a program in which a function is passed address of two variables and then alter its
contents.

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

/*
13. Write a program in which a function is passed address of two variables and then alter its
contents.
*/
#include<stdio.h>
void swap(int*, int *);
int main(){
int a,b;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
printf("\nBefore swap A=%d, B=%d",a,b);
swap(&a,&b);
printf("\nAfter swap A=%d, B=%d",a,b);
return 0;
}
void swap(int *x, int *y){
int t;
t=*x;
*x=*y;
*y=t;
}
14. Write a program which takes the radius of a circle as input from the user, passes it to another
function that computes the area and the circumference of the circle and displays the value of area
and circumference from the main() function.
/*
14. Write a program which takes the radius of a circle as input from the user, passes it to another
function
that computes the area and the circumference of the circle and displays the value of area and
circumference from the main() function.
*/
/*
Formula:
c=2*pi*r
a=pi*r*r
*/
#include<stdio.h>
#define PI 3.14189
double circumference(double);
double area(double);
int main(){
double r;
printf("Enter the radius of the circle:");
scanf("%lf",&r);
printf("\nCircumference = %.2lf",circumference(r));
printf("\nArea = %.2lf",area(r));
return 0;
}
double circumference(double r){
return(2*PI*r);
}
double area(double r){

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

return(PI*r*r);
}
15. Write a program to find sum of n elements entered by the user. To write this program, allocate
memory dynamically using malloc() / calloc() functions or new operator.
/*
15. Write a program to find sum of n elements entered by the user.
To write this program, allocate memory dynamically using malloc() / calloc() functions or new
operator.
*/
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,*p,sum=0,i;
printf("\nEnter number of elements:");
scanf("%d",&n);
p=(int *)malloc(sizeof(int)*n);
for(i=0;i<n;i++){
printf("\nEnter data:");
scanf("%d",(p+i));
sum=sum+*(p+i);
}
printf("\nElements are:");
for(i=0;i<n;i++){
printf("%d,",*(p+i));
}
printf("\nSum=%d",sum);
return 0;
}
16. Write a menu driven program to perform following operations on strings:
a) Show address of each character in string
b) Concatenate two strings without using strcat function.
c) Concatenate two strings using strcat function.
d) Compare two strings
e) Calculate length of the string (use pointers)
f) Convert all lowercase characters to uppercase
g) Convert all uppercase characters to lowercase
h) Calculate number of vowels
i) Reverse the string
/*
16. Write a menu driven program to perform following operations on strings:
a) Show address of each character in string
b) Concatenate two strings without using strcat function.
c) Concatenate two strings using strcat function.
d) Compare two strings
e) Calculate length of the string (use pointers)
f) Convert all lowercase characters to uppercase
g) Convert all uppercase characters to lowercase
h) Calculate number of vowels
i) Reverse the string
*/
#define MAX 10

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
void printstring(char[]);
void inputstring(char[]);
int main(){
char str[MAX],str1[MAX],str3[MAX+MAX],*p,t;
int i,j,v,ch;
do{
system("cls");
printf("\n1. Show address of each character in string");
printf("\n2. Concatenate two strings without using strcat function");
printf("\n3. Concatenate two strings using strcat function");
printf("\n4. Compare two strings");
printf("\n5. Calculate length of the string (use pointers)");
printf("\n6. Convert all lowercase characters to uppercase");
printf("\n7. Convert all uppercase characters to lowercase");
printf("\n8. Calculate number of vowels");
printf("\n9. Reverse the string");
printf("\n10. Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
if(ch==1){
printf("\nShow address of each character in string:");
inputstring(str);
printstring(str);
for(i=0;str[i]!='\0';i++){
printf("\n%u is the address of %c",&str[i],str[i]);
}
}
else if(ch==2){
printf("\nConcatenate two strings without using strcat function...\n");
inputstring(str);
printf("\nEnter another string:");
inputstring(str1);
printstring(str);
printstring(str1);
for(i=0,j=0;str[i]!='\0';i++,j++){
str3[j]=str[i];
}
for(i=0;str1[i]!='\0';i++,j++){
str3[j]=str1[i];
}
printf("\nConcatinated String is :");
printstring(str3);
}
else if(ch==3){
printf("\nConcatenate two strings using strcat function...\n");
inputstring(str);
printf("\nEnter another string:");

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

inputstring(str1);
strcat(str,str1);
printstring(str);
}
else if(ch==4){
printf("\nCompare two strings...\n");
inputstring(str);
printf("\nEnter another string:");
inputstring(str1);
printstring(str);
printstring(str1);
if(strcmpi(str,str1)==0)
printf("\nStrings are equal");
else
printf("\nStrings are not equal");

}
else if(ch==5){
printf("\nCalculate length of the string (use pointers):...\n");
inputstring(str);
for(p=str,i=0;*(p+i)!='\0';i++);
printf("\nThe length of the string is %d",i);
}
else if(ch==6){
printf("\nConvert all lowercase characters to uppercase....\n");
inputstring(str);
printstring(str);
for(i=0;str[i]!='\0';i++){
if(str[i]>=97 && str[i]<=122){
str[i]=str[i]-32;
}
}
printstring(str);
}
else if(ch==7){
printf("\nConvert all uppercase characters to lowercase....\n");
inputstring(str);
printstring(str);
for(i=0;str[i]!='\0';i++){
if(str[i]>=65 && str[i]<=90){
str[i]=str[i]+32;
}
}
printstring(str);
}
else if(ch==8){
printf("\nCalculate number of vowels....\n");
inputstring(str);
printstring(str);
for(i=0,v=0;str[i]!='\0';i++){
switch(str[i]){

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

case 'A':
case 'a':
case 'E':
case 'e':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u':
v++;
break;
}
}
printf("\nNumber of vowels:%d",v);
}
else if(ch==9){
printf("\nReverse the string....\n");
inputstring(str);
printstring(str);
for(i=0;str[i]!='\0';i++);
i--;
for(j=0;j<i;j++,i--){
t=str[i];
str[i]=str[j];
str[j]=t;
}
printstring(str);
}
else{
printf("\nEntered wrong choice");
}
printf("\nPress any key to continue....");
getch();
}while(ch!=10);
}
void printstring(char s[]){
int i;
printf("\nContent of String:");
for(i=0;s[i]!='\0';i++)
printf("%c,",s[i]);
}
void inputstring(char s[]){
printf("\nEnter a string:");
scanf("%s",s);
}
17. Given two ordered arrays of integers, write a program to merge the two-arrays to get an
ordered array.
/*
17. Given two ordered arrays of integers, write a program to merge the two-arrays to get an
ordered array.

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

*/
#include<stdio.h>
#define MAX 5
void inputarray(int[]);
void orderarray(int[]);
void printarray(int[]);
void mergearray(int[], int[], int[]);
int main(){
int ar1[MAX],ar2[MAX],ar3[MAX+MAX],i,j,t;
inputarray(ar1);
printarray(ar1);
inputarray(ar2);
printarray(ar2);
orderarray(ar1);
printarray(ar1);
orderarray(ar2);
printarray(ar2);
printf("\nBefore Merge content of arrays will be....\n");
printarray(ar1);
printarray(ar2);
mergearray(ar3, ar1, ar2); /*ar3 = ar1 + ar2 */
printf("\nOrdering the Array...\n");
for(i=0;i<MAX+MAX;i++){
for(j=i+1;j<MAX+MAX;j++){
if(ar3[i]>ar3[j]){
t=ar3[i];
ar3[i]=ar3[j];
ar3[j]=t;
}
}
}
printf("\nAfter Merge, the ordered array will be....\n");
for(i=0;i<MAX+MAX;i++){
printf("%d,",ar3[i]);
}
return 0;
}
void inputarray(int a[]){
int i;
printf("\nEnter the elements of Array...\n");
for(i=0;i<MAX;i++){
printf("\nEnter data:");
scanf("%d",&a[i]);
}
}
void orderarray(int a[]){
int i,j,t;
printf("\nOrdering the Array...\n");
for(i=0;i<MAX;i++){
for(j=i+1;j<MAX;j++){
if(a[i]>a[j]){

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
void printarray(int a[]){
int i;
printf("\nThe elements of the Array...\n");
for(i=0;i<MAX;i++){
printf("%d,",a[i]);
}
}
void mergearray(int c[], int a[], int b[]){
/* Merging */
int i,j;
for(i=0,j=0;i<MAX;i++,j++){
c[j]=a[i];
}
for(i=0;i<MAX;i++,j++){
c[j]=b[i];
}
}
18. WAP to display Fibonacci series (i)using recursion, (ii) using iteration
/*
18. WAP to display Fibonacci series (i)using recursion, (ii) using iteration
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void fibIteration(int);
void fibRecursion(int);
int main(){
int n,ch;
printf("\nEnter how many terms:");
scanf("%d",&n);
do{
system("cls");
printf("\n1. Fibonacci Series using Recursion");
printf("\n2. Fibonacci Series using Iteration");
printf("\n3. Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
if(ch==1){
printf("%d,%d",0,1);
fibRecursion(n-2);
}
else if(ch==2){
fibIteration(n);
}

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

printf("\nPress any key to continue....");


getch();
}while(ch!=3);
}
void fibIteration(int n){
int f1=0,f2=1,f3;
printf("\nFibonacci Series :");
printf("%d,%d",f1,f2);
while((n-2)>0){
f3=f1+f2;
printf(",%d",f3);
f1=f2;
f2=f3;
n--;
}
}
void fibRecursion(int n){
static int f1=0,f2=1,f3;
if(n>0){
f3=f1+f2;
f1=f2;
f2=f3;
printf(",%d",f3);
fibRecursion(n-1);
}
}
19. WAP to calculate Factorial of a number (i)using recursion, (ii) using iteration
/*
19. WAP to calculate Factorial of a number (i)using recursion, (ii) using iteration
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int factIteration(int);
int factRecursion(int);
int main(){
int n,ch,f=0;
printf("\nEnter a number:");
scanf("%d",&n);
do{
system("cls");
printf("\n1. Factorial using Recursion");
printf("\n2. Factorial using Iteration");
printf("\n3. Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
if(ch==1){
f=factRecursion(n);
}
else if(ch==2){
f=factIteration(n);

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

}
printf("\nFactorial = %d",f);
printf("\nPress any key to continue....");
getch();
}while(ch!=3);
}
int factIteration(int n){
int f=1;
while(n>0){
f=f*n;
n--;
}
return(f);
}
int factRecursion(int n){
if(n==1)
return(1);
else
return(n*factRecursion(n-1));
}
20. WAP to calculate GCD of two numbers (i) with recursion (ii) without recursion.
/*
20. WAP to calculate GCD of two numbers (i) with recursion (ii) without recursion.
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int gcdIteration(int,int);
int gcdRecursion(int,int);
int main(){
int a,b,ch,r;
printf("\nEnter two numbers:");
scanf("%d%d",&a,&b);
do{
system("cls");
printf("\n1. GCD using Recursion");
printf("\n2. GCD using Iteration");
printf("\n3. Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
if(ch==1){
r=gcdRecursion(a,b);
printf("\nGCD = %d",r);
}
else if(ch==2){
r=gcdIteration(a,b);
printf("\nGCD = %d",r);
}
printf("\nPress any key to continue....");
getch();
}while(ch!=3);

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

}
int gcdIteration(int a, int b){
int temp;
while(b!=0){
temp=b;
b=a%b;
a=temp;
}
return a;
}
int gcdRecursion(int a, int b){
if(b!=0)
return gcdRecursion(b,a%b);
else
return a;
}
21. Create Matrix class using templates. Write a menu-driven program to perform following
Matrix operations (2-D array implementation):
a) Sum b) Difference c) Product d) Transpose
/*
21. Create Matrix. Write a menu-driven program to perform following Matrix operations (2-D
array implementation):
a) Sum b) Difference c) Product d) Transpose
*/
#define R 2
#define C 2
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void sumMatrix(int [][C],int [][C],int [][C]);
void differenceMatrix(int[][C],int[][C],int[][C]);
void productMatrix(int[][C],int[][C],int[][C]);
void transposeMatrix(int[][C], int[][C]);
void inputMatrix(int[][C]);
void printMatrix(int[][C]);
int main(){
int a[R][C],b[R][C],c[R][C],ch,r;
printf("\nEnter Value of Matrix:");
inputMatrix(a);
printf("\nEnter Value of Matrix:");
inputMatrix(b);
do{
system("cls");
printf("\n1. Sum of two Matrix");
printf("\n2. Difference of two Matrix");
printf("\n3. Product of two Matrix");
printf("\n4. Transpose of a Matrix");
printf("\n5. Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
if(ch==1){

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

sumMatrix(a,b,c);
printMatrix(c);
}
else if(ch==2){
differenceMatrix(a,b,c);
printMatrix(c);
}
else if(ch==3){
productMatrix(a,b,c);
printMatrix(c);
}
else if(ch==4){
transposeMatrix(a,c);
printMatrix(c);
}
printf("\nPress any key to continue....");
getch();
}while(ch!=5);
}
void sumMatrix(int a[][C],int b[][C],int c[][C]){
int i,j;
for(i=0;i<R;i++){
for(j=0;j<C;j++){
c[i][j]=a[i][j]+b[i][j];
}
}
}
void differenceMatrix(int a[][C],int b[][C],int c[][C]){
int i,j;
for(i=0;i<R;i++){
for(j=0;j<C;j++){
c[i][j]=a[i][j]-b[i][j];
}
}
}
void productMatrix(int a[][C],int b[][C],int c[][C]){
int i,j,k;
for(i=0;i<R;i++){
for(j=0;j<C;j++){
c[i][j]=0;
for(k=0;k<C;k++){
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
}
void transposeMatrix(int a[][C],int c[][C]){
int i,j;
printf("\nTransposing the matrix....\n");
for(i=0;i<R;i++){
for(j=0;j<C;j++){

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

c[j][i]=a[i][j];
}
}
}
void inputMatrix(int a[][C]){
int i,j;
printf("\nEnter element of the matrix....\n");
for(i=0;i<R;i++){
for(j=0;j<C;j++){
printf("\nEnter Data:");
scanf("%d",&a[i][j]);
}
}
}
void printMatrix(int a[][C]){
int i,j;
printf("\nDisplay element of the matrix....\n");
for(i=0;i<R;i++){
for(j=0;j<C;j++){
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
22. Write a menu-driven program, using user-defined functions to find the area of rectangle,
square, circle and triangle by accepting suitable input parameters from user.
/*
22. Write a menu-driven program, using user-defined functions
to find the area of rectangle, square, circle and triangle by accepting suitable
input parameters from user.
*/
#define PI 3.14189
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int areaRectangle(int,int); /*length x width*/
int areaSquare(int); /*side * side */
int areaCircle(int); /* pi*r*r */
int areaTriangle(int,int); /* (base*height)/2 */
int main(){
int length, width, side, radius, base, height, result,ch;
do{
system("cls");
printf("\n1. Area of Rectangle");
printf("\n2. Area of Square");
printf("\n3. Area of Circle");
printf("\n4. Area of Triangle");
printf("\n5. Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
if(ch==1){

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

printf("\nEnter length and width:");


scanf("%d%d",&length,&width);
result=areaRectangle(length,width);
printf("\nArea of Rectangle = %d",result);
}
else if(ch==2){
printf("\nEnter side of a square:");
scanf("%d",&side);
result=areaSquare(side);
printf("\nArea of Square = %d",result);
}
else if(ch==3){
printf("\nEnter Radius:");
scanf("%d",&radius);
result=areaCircle(radius);
printf("\nArea of Circle = %d",result);
}
else if(ch==4){
printf("\nEnter Base and Height:");
scanf("%d%d",&base,&height);
result=areaTriangle(base,height);
printf("\nArea of Triangle = %d",result);
}
printf("\nPress any key to continue....");
getch();
}while(ch!=5);
}
int areaRectangle(int l,int w){
/*length x width*/
return(l*w);
}
int areaSquare(int s){
/*side * side */
return(s*s);
}
int areaCircle(int r){
/* pi*r*r */
return(PI*r*r);
}
int areaTriangle(int b,int h){
/* (base*height)/2*/
return((b*h)/2);
}
23. WAP to display the first nth terms of Fibonacci series. /* See Solution 18 */
24. WAP to find factorial of the given number. /*See Solution 19*/
25. WAP to find sum of the following series for n terms: 1 – 2/2! + 3/3! --------- n/n!
/*
25. WAP to find sum of the following series for n terms: 1 – 2/2! + 3/3! --------- n/n!
*/
#include<Stdio.h>
int factorial(int n) {

©Dr. Sanjay Banerjee, 2024


B.Sc. (Computer Science) || C language Practical

int fact=1, i;
while(n>0) {
fact*=n;
n--;
}
return fact;
}

/* Function to calculate the sum of the series */


double sum_of_series(int n) {
double sum = 0.0;
int i;
for(i=1;i<=n;i++){
sum+=(i%2==0?-1:1)*((double)i/factorial(i));
}
return sum;
}

int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("The sum of the series is: %.5f\n", sum_of_series(n));
return 0;
}
26. WAP to calculate the sum and product of two compatible matrices. /* See Solution 21 */

©Dr. Sanjay Banerjee, 2024

You might also like