You are on page 1of 14

Question1.

Write a program to find the factorial value of any number


entered through the keyboard by ‘for’ loop.
Ans.
#include <stdio.h>
#include <stdlib.h>

int main()
{
unsigned long int fact,n;
fact=1;
printf("Enter the number ");
scanf("%d",&n);
if(n==0)
{
printf("factorial is 1");

}
else{
for( ;n>0;n-=1)
{
fact=fact*n;

}
printf("factorial is %d",fact);
}
return 0;
}
Question 1.2. Question1. Write a program to find the factorial value
of any number entered through the keyboard by ‘do while’ loop.
Ans.
#include <stdio.h>
#include <stdlib.h>

int main()
{
unsigned long int fact,n;
fact=1;
printf("Enter the number ");
scanf("%d",&n);
if(n==0)
{
printf("factorial is 1");

}
else{
do
{
fact=fact*n;
n=n-1;

while(n>0);
printf("factorial is %d",fact);
}

return 0;
}
Question 1.3. Question1. Write a program to find the factorial value
of any number entered through the keyboard by ‘do while’ loop.
Ans.
#include <stdio.h>
#include <stdlib.h>

int main()
{
unsigned long int fact,n;
fact=1;
printf("Enter the number ");
scanf("%d",&n);
if(n==0)
{
printf("factorial is 1");

}
else{
while(n>0)
{
fact=fact*n;
n=n-1;

printf("factorial is %d",fact);
}

return 0;
}
Question 2. Write a program to calculate overtime pay of 10
employees. Overtime is paid at the rate of Rs. 12.00 per hour for
every hour worked above 40 hours. Assume that employees do not
work fractional part of an hour.
Ans.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int hrs;
float otime;
printf("Enter the number of hours worked: ");

scanf("%d" ,&hrs);

do
{
otime=(hrs-40)*12.00;

printf("%f ",otime);
break;
}while(hrs>40);

return 0;
}
Question 3. Two numbers are entered through the keyboard. Write a
program to find the value of one number raised to the power of
another.
Ans.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b,r=1;
scanf("%d %d ",&a,&b);
do
{
r=r*a;
b--;

}
while(b>0);
printf("%d",r);
return 0;
}
Question 4. Write a program to enter the numbers till the user wants
and at the end it should display the count of positive, negative and
zeros entered.
Ans.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int num,zero=0,negative=0,positive=0;
char choice;

do
{
printf("\nenter a number=");
scanf("%d",&num);
if(num==0)
zero+=1;
if(num<0)
negative+=1;
if(num>0)
positive+=1;
printf("zero= %d \n negative=%d \n positive=%d",zero,negative,positive);
printf("do you want to continue y /n =");
scanf("%c",&choice);
choice=getche();
}
while(choice=='y');
getch();
return 0;
}
Question 5. A five digit number is entered through the keyboard.
Write a program to obtain the reversed number and to determine
whether the original and reversed numbers are equal or not.
Ans.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,n,b,r=0;
printf("Enter the number= ");
scanf("%d",&n);
a=n;
while(n!=0){
b=n%10;
r=r*10+b;
n=n/10;
}
printf("The reversed number is %d\n",r);

if(r==a)
{
printf("The two numbers are equal");
}
else
{
printf("Numbers are not equal");
}
getch();
return 0;
}

You might also like