0% found this document useful (0 votes)
20 views7 pages

A 2

notes

Uploaded by

bharati
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)
20 views7 pages

A 2

notes

Uploaded by

bharati
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
You are on page 1/ 7

1.

Write a menu driven program to perform the following operations on


an integer. Write separate functions.
1. Check if is even or odd
2. Check if it is prime
3. Exit
Ans:- #include<stdio.h>
int main()
{
int c=0, num, res, n, flag=0, i;
while(c!=4)
{
//display menu
printf("\n1. Factorial of a number\n2. Prime or not\n3. Odd or even\n4.
Exit\n");

//display choice option to the user


printf("\nEnter your choice:");
scanf("%d", &c);

//write case statement for Four options

switch(c)
{
//For factorial block
case 1:

//code for factorial functionality


printf("Enter an integer: ");
scanf("%d", &num);
n=num;
res=num;
while(num>1)
{
res = res*(num-1);
num = num-1;
}
printf("\nFactorial of %d is %d. \n\n",n, res);
break;

//For prime block


case 2:

//functionality of Prime or not

printf("Enter an integer: ");


scanf("%d", &num);
n=num;

for(i=2;i<=n/2;i++)
{
if(num%i==0)
{
flag=1;
break;
}
}

//for number "1" it's neither prime nor composite


if(num==1)
printf("\n1 is neither prime nor composite");
else
{
if(flag==0)
printf("\n%d is Prime Number.\n\n", n);
else
printf("\n%d is not a Prime Number.\n\n", n);
}
break;

//For Odd-even block


case 3:

//functionality for Odd-even


printf("Enter an integer: ");
scanf("%d", &num);
n=num;
if(num%2==0)
printf("\n%d is Even Number.\n\n",n);
else
printf("\n%d is Odd Number.\n\n",n);
break;

//For Exit block


case 4:
printf("\nExit");
break;
}
}
}

2. Write a recursive function in ‘C’ to calculate factorial of a number. Use


this function in main.
Ans:- #include<stdio.h>

int fact(int);
int main()
{
int x,n;
printf(" Enter the Number to Find Factorial :");
scanf("%d",&n);
x=fact(n);
printf(" Factorial of %d is %d",n,x);

return 0;
}
int fact(int n)
{
if(n==0)
return(1);
return(n*fact(n-1));
}

3. Write a function in ‘C’ to calculate sum of digits of an integer. Use


this function in main.
Ans:- #include <stdio.h>

int sum_of_digits(int n)

int sum = 0,rem;

while (n > 0)

rem= n%10;

sum = sum+rem;

n=n/10;

}
return sum;

int main()

int n;

printf("Enter a number: ");

scanf("%d", &n);

printf("Sum of digits of the number is %d", sum_of_digits(n));

return 0;

4. Write a function in ‘C’ to reverse an integer. Use this in main.

Ans:- #include<stdio.h>

int findReverse(int n)

int sum=0 ,rem;

while (n!=0)

Rem= n%10;

sum = sum*10 + rem;

n =n/ 10;

return sum;

}
int main()

int number, reverse;

printf("Enter a positive interger: ");

scanf("%d", &number);

reverse = findReverse(number);

printf("The reverse of %d is: %d", number, reverse);

return 0;

You might also like