Skip to main content

for loop Programs In C

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();
}

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


Comments

Popular posts from this blog

C Programming Basics

C PROGRAMMING BASICS:
1. Introduction to C language: C language was invented in Bell Labs by Dennis Ritchie between 1972-73. C language is a case sensitive language i.e the input given to it varies according to upper and lower case letters. It is executed using binary form data such as (0,1). By using binary data the speed of executing a code increases.
1.1 Header Files:

Header files are those files which contain some predefined functions in it which we are going to use in our code. Every header file is accessed by appending an extension ".h".
Example: #include<stdio.h> ↓                   ⇡
         Tells computer to                  header file with 
         include the header                 extension .h
         file          
There are many header files for different functions such as the following: <conio.h><math.h><string.h>Etc.

1.2 Starting of code and basic Syntax:

void main() or int main()

This tells the computer from where the code is starting. It is wri…

How to start programming for beginners

HOW TO START PROGRAMMING:
1. What is Programming?
Programming is a way of communicating with the computer and giving it set of instructions to do the work as per our needs.
Example: While calculating something with a calculator what has to be done according to the input given to it is regulated by a program.Programming can also be used to build a software to determine whether a particular number is prime or not.2. Can anyone learn Programming ?The answer to this question is simple YES anyone with basic knowledge of maths and some common sense can master programming. The most amazing example of this is Sundar Pichai the CEO of Google he Studied metallurgical engineering at IIT Kharagpur. So if you love it you can do it. 3. Why Should I learn Programming?If you have that passion to solve a problem and enjoy twisting your mind then this is for you else you should leave programming right now.The other important Factor is Money if you become a master in programming then there is no limit on how…

Basic C Programming Questions

Basic Coding Questions:Exercise 1:Q1. Write a programme to calculate area and perimeter of rectangle by accepting its length and breadth.
Ans:
#include<stdio.h> #include<conio.h> void main() { float a,p,l,b; clrscr(); printf("\n enter the length and breadth of rectangle"); scanf("%f%f",&l,&b); a=l*b; p=2*(l+b); printf("\n the area of rectangle is %f and the perimeter is %f",a,p); getch(); }

The above image code is written in code blocks
Output:
enter the length and breadth of rectangle 20(press enter) 10(press enter) the area of rectangle is 200 and the perimeter is 60


Explanation of the above code: Declaring of variables in data type float since length and breadth cannot always be integers.Taking input of length and breadth together by using scanf function.Substituting the values of length and breadth stored in l and b to find out area and perimeter.Displaying the result.
Q2. Write a programme to calculate average of 5 numbers by taking input from user.
Ans: