###
__for loop Programs In C:__

Q1. Write a C program to find factorial of a number by accepting the number from user.

__Ans.__

#include<stdio.h>

#include<conio.h>

void main()

{

int n, i, fact=1;

clrscr();

printf("\n enter the number for finding factorial");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

fact=fact*i;

}

printf("\n the factorial of %d is %d",n,fact);

getch();

}

__Output:__

enter the number for finding factorial

5

the factorial of 5 is 120

###
__Explanation of the above Code:__

First of all

What is factorial?

__Ans.__

The symbol of factorial is !

here are example which will give you idea what is factorial

ex.

5!=5*4*3*2*1

3!=3*2*1

Now you have the idea what is factorial.

- In the above code we have declared 3 variables of datatype integer i for the loop, fact=1 for calculating the factorial, n for input.
- When the loop gets started with i=1 it checks the condition first i.e whether i is less than n (1<=5) which in this case is true since the initial value of i is 1 and the value of n is 5.
- After entering the loop we have the code fact=fact*i ; it happens like this fact=1*1 since the initial values of both fact and i is 1. Now the value of 1 is assigned to fact.
- Now increment takes place and the value of i becomes 2. As the condition is true it enters the loop again and executes the code like this fact=1*2; now the value of fact becomes 2.
- Now increment takes place and the value of i becomes 3. As the condition is true it enters the loop again and executes the code like this fact=2*3; now the value of fact becomes 6.
- This goes on till the value of i becomes equal to n that is 5 in this case.
- After reaching 5 the value of fact becomes equal to 5 factorial and the value is finally printed.

###
__NOTE:__

###
- We have given initial value to fact as 1 because if there is no value given to it wrong output will be shown.
- We can also start the loop from i=2 because the initial value of fact is 1 and the first iteration of the loop that is 1*1 is not necessary.

###
__Same Program using while loop:__

#include<stdio.h>

#include<conio.h>

void main()

{

int n, i=1, fact=1;

clrscr();

printf("\n enter the number for finding factorial");

scanf("%d",&n);

while(i<=n)

{

fact=fact*i;

i++;

}

printf("\n the factorial of %d is %d",n,fact);

getch();

}

###
Q2. Write a C program to find whether a number is prime or not by accepting number from user.
__Ans.__
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, r,c=0;
clrscr();
printf("\n enter the number ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
r=n%i;
if(r==0)
{
c++;
}
}
if(c==2)
{
printf("\n the number is prime number");
}
else
{
printf("\n the number is not prime number");
}
getch();
}

__Ans.__

###
__Output:__

enter the number

5

the number is prime number

###
__Explanation of the above Code:__

First of all

What is prime number?

__Ans.__

A number which is divisible by 1 and only itself is called a prime number. Also a number which is divisible has remainder 0.

So in all a number which is divisible twice that is it has remainder 0 twice only is a prime number.

- In the above code we have declared 4 variables of datatype integer n for input, i for the loop, r for calculating remainder and c for counting how many times the remainder is 0 i.e how many times the number is divisible.
- In the for loop we one by one check, how many times the number was divisible by dividing the number n by the numbers from 1 to n and increasing the value of c if it is divisible.
- So the first value which enters the loop is i=1 then the remainder is calculated by r i.e r=5%1 which is 0 since 5 is divisible by 1 and now the value of loop increases from 0 to 1.
- Now the value of i becomes 2 again the remainder is calculated by r=5%2 which is 1 in this case so the value of c remains the same.
- Similarly the value of c remains same for 3 and 4 because their remainders are 2 and 1 respectively.
- Now 5 is divisible by 5 and the remainder becomes 0 so the value of c increases from 1 to 2.
- After coming out of for loop we compare the values of c. If the value of C is equal to 2 i.e the number is divisible twice and its a prime number.
- Else its not a prime number

###
__Same Program using while loop:__

###
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i=1, r,c=0;
clrscr();
printf("\n enter the number ");
scanf("%d",&n);
while(i<=n)
{
r=n%i;
if(r==0)
{
c++;
}
i++;
}
if(c==2)
{
printf("\n the number is prime number");
}
else
{
printf("\n the number is not prime number");
}
getch();
}

Please subscribe to get notified whenever i upload a new post.
Also share the content with your friends who wants to learn programming.
If you have any doubts please comment down below.

If you have not studied the previous topic of while and do while loop click on the link given below

}

Please subscribe to get notified whenever i upload a new post.

Also share the content with your friends who wants to learn programming.

If you have any doubts please comment down below.

If you have not studied the previous topic of while and do while loop click on the link given below

## Comments

## Post a comment

If you have any doubts pls let me know