While 2

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

---------------------------------WHILE 2------------------------------------

Q.Write a C program that prompts the user to enter a number and then prints
the multiplication table for that number using a while loop.

ANS:
#include<stdio.h>
int main(){
int n,i=1;
printf("enter a no : ");
scanf("%d",&n);
printf("table is :\n");
while(i<=10){
printf("%d*%d=%d\n",n,i,n*i);
i++;
}

return 0;
}
---------------------------------------------------------------------------
Q.Write a C program to generate the Fibonacci sequence up to a given times using a
while loop.
The Fibonacci sequence is a series of numbers in which each number is the sum of
the two preceding ones,
usually starting with 0 and 1. The program should prompt the user to enter a
positive integer and
then print the Fibonacci sequence up to that number.
Input as:
Enter how many Fibonacci series you want : 9
Output as:
0 1 1 2 3 5 8 13 21

ANS:
#include<stdio.h>
int main(){

int n1=0,n2=1,n3,i,n;
printf("enter a no : ");
scanf("%d",&n);
while(i<n){
n3=n1+n2;
printf("%d ",n1);
n1=n2;
n2=n3;
i++;

return 0;
}
----------------------------------------------------------------------------------
Q.Write a C program to check if a given number is a palindrome. A palindrome is a
number that remains
the same when its digits are reversed. For example, 121, 454 and 767 are
palindromic numbers.
The program should prompt the user to enter a positive integer and determine
whether it is a palindrome or not.
Input as:
Enter a number : 121
Output as:
121 is a Palindrome number.

ANS:
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}
-----------------------------------------------------------------------------------
--
Q.Write a C program to check the given number is a perfect number or not?
If a sum of the multiples of the number excluding the given number is same as the
original
number then we can say that number is a perfect number.
6 multiples are 1,2,3,6 : sum of multiples = 1+2+3 = 6
6 is a perfect number.

ANS:
#include<stdio.h>

int main()
{
int i=1,n,Sum=0;
printf(" Enter any number to check Perfect Number \n");
scanf("%d",&n);

while(i<n)
{
if(n%i==0)
Sum=Sum+i;
i++;
}
if(Sum == n)
printf("\n %d is Perfect Number", n);
else
printf("\n %d is not a Perfect Number", n);
return 0;
}
-----------------------------------------------------------------------------------
------
Q.Write a C program to check if a given number is a strong number. A strong number
is
a positive integer in which the sum of the factorial of its digits is equal to the
number itself.
For example, 145 is a strong number since 1! + 4! + 5! = 145. The program should
prompt the user
to enter a positive integer and determine whether it is a strong number or not.
Input as:
Enter a number : 145
Output as:
145 is a strong number.

ANS:
#include<stdio.h>
int main(){
int n,t,f,last_digit,sum=0;
printf("enter a positive integer :");
scanf("%d",&n);
t=n;
while(n){
last_digit=n%10;
f=1;
while (last_digit>1)
{
f*=last_digit;
last_digit--;
}
sum+=f;
n=n/10;
}
if(t==sum){
printf("%d is a strong number",t);
}else{
printf("%d is not a strong number",t);
}
return 0;
}
-----------------------------------------------------------------------------------
------

You might also like