21. Write a Program to print the sum of as many numbers as user want.
Answer :
#include<stdio.h>
#include<conio.h>
Void main()
{
int num, sum=0;
char reply='y';
while(reply == 'y')
{
printf("\n Enter The Number to Add:");
scanf("%d",&num);
sum= sum+num;
printf("\n Continue(y/n):");
scanf("%c", & reply);
}
printf("\n The sum of all the numbers is =%d", sum);
getch();
}
_________________________________________________________________________________
22. Accept a Number tell user want and check that is prime number or not.
Answer :
#include<stdio.h>
#include<conio.h>
Void main()
{
int num, i=2, flag=0;
char ch;
clrscr();
do
{
printf("Enter The Number, Which you want to check:");
scanf("%d", &num);
while(i<=num-1)
{
if(num%i ==0)
{
printf("Number is Not Prime");
flag =1;
break;
}
i++;
}
if(flag == 0)
printf("Number is Prime");
printf("\n You want to check more number, Enter Y");
fflush(stdin);
scanf("%c", & ch);
flag = 0;
}
while((ch == 'y')|| (ch == 'y'))
getch();
}
Answer :
#include<stdio.h>
#include<conio.h>
Void main()
{
int num, sum=0;
char reply='y';
while(reply == 'y')
{
printf("\n Enter The Number to Add:");
scanf("%d",&num);
sum= sum+num;
printf("\n Continue(y/n):");
scanf("%c", & reply);
}
printf("\n The sum of all the numbers is =%d", sum);
getch();
}
_________________________________________________________________________________
22. Accept a Number tell user want and check that is prime number or not.
Answer :
#include<stdio.h>
#include<conio.h>
Void main()
{
int num, i=2, flag=0;
char ch;
clrscr();
do
{
printf("Enter The Number, Which you want to check:");
scanf("%d", &num);
while(i<=num-1)
{
if(num%i ==0)
{
printf("Number is Not Prime");
flag =1;
break;
}
i++;
}
if(flag == 0)
printf("Number is Prime");
printf("\n You want to check more number, Enter Y");
fflush(stdin);
scanf("%c", & ch);
flag = 0;
}
while((ch == 'y')|| (ch == 'y'))
getch();
}
_________________________________________________________________________________
23. Write a Program to check the given number is prime or not.
Answer :
#include<stdio.h>
#include<conio.h>
Void main()
{
int num, i;
printf("\n Enter Any Number:");
scanf("%d",& num);
for(i=2; i<=num/2; ++i)
{
if(num%i == 0)
{
printf("\n Not a Prime Number");
exit(0);
}
}
printf("\n It is a Prime Number:");
getch();
}
_________________________________________________________________________________
24. Write a Program to check the given number is prime or not. Prime Numbers are the numbers
that are divisible by 1 and itself only.
Answer :
#include<stdio.h>
#include<conio.h>
Void main()
{
int num, f=1,i;
printf("\n Enter any Number:");
scanf("%d",&num);
for(i=2; i<num; ++i)
{
if((num%i)==0)
{
f=0;
break;
}
}
if(f==0)
printf("\n %d is not a Prime Number", num);
else
printf("\n %d is a Prime Number", num);
getch();
}
_________________________________________________________________________________
25. Write a Program that accept a number from user and find its factorial with if-else.
Answer :
#include<stdio.h>
#include<conio.h>
Void main()
{
int n,m=1, f=1;
printf("Enter Value:");
scanf("%d", &n);
f=f*m;
if(m == n)
{
printf(%d",f);
}
else
{
m = m+1;
}
getch();
}
*********************************************************************************
0 comments:
Post a Comment