Semester: - II Subject: - Computer Programming A.Y.
:- 2022-2023
Q.1 A) Develop a C program for finding factorial of a number using recursion.
Algorithm:
Main Function()
Step1 : Start
Step2 : Declare variable n, i and declare function factorial (int)
Step 3 :print “Enter a Number”
Step 4: Read n
Step 5: call factorial function
Step 6: Stop
factorial function ()
Step 1: start factorial function
Step 2: is n= =0 ? , if yes then return 1 else return (n*factorial(n-1))
Program:
#include<stdio.h>
void main()
{
int number;
long fact;
int factorial(int); // function declaration
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number); //function call
printf("Factorial of %d is %ld\n", number, fact);
}
long factorial(int n) //function definition
{
if(n==0)return 1; else
return(n * factorial(n-1));
}
Output:
Enter a number: 3
Factorial of 3 is: 6
Explanation:
Suppose number : 3
factorial (3)
3 * factorial (2)
3 * 2 * factorial (1)
3*2*1
3*2*1= 6
Prof. Pranali Patil AY-2022-23
Q. 1 B) Develop a C program to input a number from the user and find the power of a given
number using recursion.
Algorithm:
Main Function()
Step1 : Start
Step2 : Declare variable n, i and declare function fibonacci(int)
Step 3 :print Enter the values of x and n
Step 4: Read x and n.
Step 7: call power function
Step 8: Stop
power function ()
Step 1: start power function
Step 2: is n= =1 ? , if yes then return x else return (x*power (x, n-1)
Program:
#include<stdio.h>
int main()
{
int x,n,y;
int power(int x, int n); //function declaration
printf("Enter the values of x and n\n");
scanf("%d%d",&x,&n);
y= power(x,n); //function call
printf("The value of %d raise to %d is %d \n ",n, x, y);
return 0;
}
int power(int x, int n) //function definition
{
if(n==1)
return x;
else
return (x * power (x,n-1));
}
Output:
Enter the values of x and n
2 3
The value of 2 raise to 3 is 8
Explanation:
Suppose x=2 and n=3
power (2,3)
2 * power (2,2)
2 * 2 * power (2,1)
2 * 2 * 2 * power(2,0)
2*2*2*1= 8
Prof. Pranali Patil AY-2022-23
Q.2 A1) Develop a C program to sort an array in ascending order
Program:
#include <stdio.h>
void main()
{
int a[100];
int i, j, n, temp;
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
/* Bubble sorting begins */
for (i = 0; i < n; i++)
{
for (j = 0; j < (n - i - 1); j++)
{
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf(“After Sorting…\n");
for (i = 0; i < n; i++)
{
printf("%d\n", a[i]);
Prof. Pranali Patil AY-2022-23
}
}
Output:
Enter the number of elements:5
Enter a value:56
Enter a value:34
Enter a value:76
Enter a value:23
Enter a value:11
After Sorting…
11
23
34
56
76
Q 2 A2) Develop a C program to find transpose of entered matrix
Program:
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], i, j, m, n;
printf("Enter rows and columns: ");
scanf("%d %d", &m, &n);
//to accept elements from user
printf("Enter the elements of first matrix\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf(“Enter a value:”)
scanf("%d", & a[i][j]);
}
}
// printing the matrix a[][]
printf("\nEntered matrix: \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
// computing the transpose
printf(“Transpose of matrix:\n”)
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
transpose[i][j] = a[j][i];
printf("%d ", transpose[i][j]);
}
Prof. Pranali Patil AY-2022-23
printf("\n");
}
return 0;
}
Output:
Enter rows and columns: 3 3
Enter a value:1
Enter a value:2
Enter a value:3
Enter a value:4
Enter a value:5
Enter a value:6
Enter a value:7
Enter a value:8
Enter a value:9
Entered matrix:
1 2 3
4 5 6
7 8 9
Transpose of the matrix:
1 4 7
2 5 8
3 6 9
Q.2 B1) Develop a C program to check whether the entered string is palindrome or not.
Program:
Algorithm:
Main Function()
Step1 : Start
Step2 : Declare variable n, i, c=0 and s[50]
Step 3 :print “Enter the string: ”
Step 4: Read string
Step 5: calculate size of string using string function stelen(s).
Step 6: initialize i=0
Step 7: check is i<n/2 if true then goto step 8 else goto step 9
Step 8: check is s[i]= =s[n-i-1] if yes then increment count of c and increment count of i then goto
step 7.
Step 9: check is c==I, if true then goto step 10 else goto step 11
Step 10:print “ string is palindrome”
Step 11:print “string is not palindrome”
Step 12:Stop
Program:
#include <string.h>
#include<stdio.h>
int main()
{
char s[50];
int i,n,c=0;
printf("Enter the string : ");
gets(s);
n=strlen(s);
Prof. Pranali Patil AY-2022-23
for(i=0;i<n/2;i++)
{
if(s[i]==s[n-i-1])
c++;
}
if(c==i)
printf("string is palindrome");
else
printf("string is not palindrome");
return 0;
}
Output:
Enter the string : NITIN
string is a palindrome
Explanation :
Enter the string : NITIN
In memory: s[0] s[1] s[2] s[3] s[4] s[5]
N I T I N /0
When i=0,
is 0<2 , True
Then is s[0]==s[4], True, then increment c , c=1
When i=1
is 1<2 , True
Then is s[1]==s[3], True , then increment c , c=2
When i=2
is 2<2 , False
Now check is c==i , 2 ==2, True
Then print “string is palindrome”
Q. 2 B2) Develop a C program to Count no. of vowels, consonant, digit and whitespaces in
entered string
Program:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int vowel=0, consonant=0, digit=0, whitespace=0;
printf("Enter the String: ");
gets(str);
printf("\n%s",str);
for(int i=0;i<strlen(str);i++)
{
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
Prof. Pranali Patil AY-2022-23
if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u'||s
tr[i]=='U’)
{
vowel++;
}
else
{
consonant++;
}
else if (str[i]>='0' && str[i]<='9')
{
digit++;
}
else if(str[i] == ‘ ‘ )
{
whitespace++;
}
}
}
printf("Number of vowel = %d", vowel);
printf("\nNumber of consonant = %d", consonant);
printf("\nNumber of digit = %d", digit);
printf("\nNumber of whitespace = %d", whitespace);
return 0;
}
Output:
Enter the String:I am Alex 123
Number of vowel=4
Number of consonant =3
Number of digit =3
Number of whitespace=3
Q.3 A) Make use of structure which has following elements,
1. Employee ID
2. Name
3. Salary
and write a program to read records of 5 employees and display them in tabular format.
Program:
#include<stdio.h>
struct employee
{
int id;
char name[100];
float salary;
};
void main()
{
struct employee e[100];
int n, i;
printf("Enter the no. of employees:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
Prof. Pranali Patil AY-2022-23
printf("Enter employee id: ");
scanf("%d",&e[i].id);
printf("Enter employee name: ");
scanf(" %s",&e[i].name);
printf("Enter employee salary: ");
scanf("%f", &e[i].salary);
}
for(i=0;i<n;i++)
{
printf("\n\nEmployee details\n");
printf(“Employee ID\tName\tSalary”);
printf("%d\t%d\s%f\n", e[i].id, e[i].name, e[i].salary);
}
}
Output:
Enter the no. of employees:5
Enter employee id: 1001
Enter employee name:Alex
Enter employee salary: 50000
Enter employee id: 1002
Enter employee name:Willam
Enter employee salary:60000
Enter employee id: 1003
Enter employee name:Bob
Enter employee salary:65000
Enter employee id: 1004
Enter employee name:Rose
Enter employee salary:65000
Enter employee id: 1005
Enter employee name:Nile
Enter employee salary:60000
Employee details
Employee Id Name Salary
1001 Alex 50000
1002 Willam 60000
1003 Bob 65000
1004 Rose 65000
1005 Nile 60000
Q.3 B) Develop a C program using nested structure to create an array of
structure to store the details of N students using following elements, 1. Roll
No. 2. Name 3. Date of Birth Display the data in tabular format
Program:
#include <stdio.h>
struct student
{
int roll_no;
Prof. Pranali Patil AY-2022-23
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}dob;
};
int main()
{
struct student s[10];
int n;
printf("Enter No.of students: ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter Roll No.:");
scanf("%d",&s[i].roll_no);
printf("Enter Name: ");
scanf("%s",s[i].name);
printf("Enter Date of Birth: ");
scanf("%d%d%d",&s[i].dob.dd , &s[i].dob.mm , &s[i].dob.yyyy);
}
printf("Roll no.\tName\tDate of Birth\n");
printf("_________________________________________\n");
for(int i=0;i<n;i++)
{
printf("%d\t\t%s\t%d-%d-%d\n",s[i].roll_no,s[i].name,s[i].dob.dd , s[i].dob.mm ,
s[i].dob.yyyy);
}
return 0;
}
Output:
Enter No.of students: 2
Enter Roll No.:11
Enter Name: Alex
Enter Date of Birth: 12 6 2000
Enter Roll No.:12
Enter Name: Bob
Enter Date of Birth: 23 7 2001
Roll no. Name Date of Birth
_________________________________________
11 Alex 12-6-2000
12 Bob 23-7-2001
Prof. Pranali Patil AY-2022-23