UT-II Paper Solution
UT-II Paper Solution
:- 2022-2023
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);
}
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
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;
}
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
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]);
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 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
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'))
{
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
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