Prime Number:
A prime
number that doesn’t have factors. Like 2
3 5 7 11 13 17……
Just see the
logic how to check a given number is prime or not
// number is
a variable to be checked
// count is
a integer variable
// flag is
used to check condition initially should be 0
for(count=2; count<=number/2; count++)
{
if( number % count = = 0)
{
flag = 1;
break;
}
}
if (flag = = 0)
printf("prime number");
else
printf("not a prime number");
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Factorial of
a given number
A. Using loop
int main()
{
int count , fact = 1,num;
printf("Enter Number:
");
scanf("%d",&num);
for(count = 1; count < = num; count++)
{
fact = fact * count;
}
printf("Factorial of %d is:
%d",num,fact);
return 0;
}
B. Using Recursion -
Recursion is function which calls itself ,
Calling itself
stops at a particular condition.
int
fact(int n); // Function Declaration
int main()
{
int num, result;
printf("Enter num: ");
scanf("%d", &num);
result
= fact(num); // Function Calling
printf("Factorial of %d is %d", num,
result);
return 0;
}
int fact(int n) // Function Definition
{
if (n <= 0)
{
return 1;
}
else
{
return (n * fact(n - 1));
}
}