Examples Using Loop Statements.
Today in our C programming tutorial for beginners lesson, we will see some examples using for loops. Also, we will learn about how to find the factorial of any number in c.
The Solution To Our Last Problem:
In our last tutorial, I have given you a task. Have you solved the problem? If you did then it’s fine. If you couldn’t then try to solve it with me. If you still haven’t read my previous article you can read it here.
However, the problem from the last tutorial was like that:
1^2 + 2^2 + 3^2 + …..+ n^2 is a section of number where n is a positive integer (less than 1000). Our task is to write a code that will print the summation of this section.
Here, as we don’t know the exact value of n. So, we have to take the value of n from the user as input. Then we have to square each element from 1 to n and then add each of them to a variable consider sum. Then we have to print the answer. See the following code.
Code:
#include <stdio.h>
int main ()
{
int i,j,sum=0,n;
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
sum+=(i*i);
}
printf(“%dn”,sum);
return 0;
}
Input: 8
Output: 140
Input: 1000
Output: 332833500
I have used for loop to solve this problem. But you can use while or do…while loop as you want.
Finding Factorial Of Any Number In C:
To find the factorial of any number in c, at first, you have to learn what is factorial?
Factorial of any integer means multiplying all the integers from 1 to itself. For example factorial of 6=1*2*3*4*5*6. So, the factorial of n=1*2*3*……*n-1*n. Factorial of n is represent as n!
Now, to write a code in c we have to use two-variable for this problem. One is to store the factorial and the other is to use the values from 1 to n. Follow the following code for this problem.
Code using while loop:
#include <stdio.h>
int main ()
{
int fact=1,i,j,n;
scanf(“%d”,&n);
i=1;
while(i<=n)
{
fact*=i;
i++;
}
printf(“%dn”,fact);
return 0;
}
Code using for loop:
#include <stdio.h>
int main ()
{
int fact=1,i,j,n;
scanf(“%d”,&n);
for(i=1; i<=n; i++)
fact*=i;
printf(“%dn”,fact);
return 0;
}
Input: 5
Output: 120
Input: 10
Output: 3628800
Here, we have used the fact variable to store the factorial. As factorial can’t be 0 (factorial of 0 is also 1) that so why we initialize fact as 1. i variable is used for the values from 1 to n. We are multiplying each i with the current value of fact. When multiplication of 1 to n is completed then we got the factorial then we print it. When n is 5. The code works in the following manner:
When i=1, fact=1*1=1.
When i=2, fact=1*2=2.
When i=3, fact=2*3=6.
When i=4, fact=6*4=24.
When i=5, fact=24*5=120.
When i=6, condition i<=5 become false so the loop was terminated and then answer was printed.
I think you have learned how the code works. Now, you have seen that I do not use {} at the for statement. That is because when for statement has only one statement there is not necessary to use the second bracket. But if you use the second bracket then it’s still no problem. But when 2 or more statements are placed into the for a loop. Then you must use the second bracket to declare the scope of these statements under the for a loop.
2 More Problems Using Loop Statements:
Now, consider the following problem.
Problem: You are given n numbers (n<10000). Write a code that will take at first n as input. Then it will take n line of inputs. Each line will take an integer number as input and print is that number even or odd number.
For this, at first, we have to take n as input. Then we will use a loop to take n numbers as input. Then we will print after taking each number whether it is an even or odd number.
Code:
#include <stdio.h>
int main ()
{
int n,k,i;
scanf(“%d”,&n);
for(i=1; i<=n; i++)
{
scanf(“%d”,&k);
if(k%2==0)
{
printf(“%d is an even numbern”,k);
}
else
{
printf(“%d is an odd numbern”,k);
}
}
return 0;
}
Give some input and watch the output by yourself.
Now, look at our today’s last problem.
Problem: Write a code in c that will print the multiplication table from 1 to n. Here, n is less than 1000.
It seems pretty hard. But don’t worry it is very easy. See my code then you will automatically understand it.
Code:
#include <stdio.h>
int main ()
{
int n,k,i,j;
scanf(“%d”,&n);
for(i=1; i<=n; i++)
{
for(j=1; j<=10; j++)
{
printf(“%d*%d=%dn”,i,j,i*j);
}
printf(“n”);
}
return 0;
}
For n=2 output will be like:
1*1=1
1*2=2
1*3=3
1*4=4
1*5=5
1*6=6
1*7=7
1*8=8
1*9=9
1*10=10
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
Here we have used nested for loop. And we have used two nested for loops. The first one is for the which multiplication table we are going to show. And the second one is for the values 1 to 10. By which we normally multiply the number which multiplication table is going to show. I think you got this.
Now, try to write code for these 2 problems using a while loop and paste your code into the comment box.
That’s it for today. Wear a mask and wash your hands. Don’t go outside without very necessary.
Stay home. Stay safe. Happy coding.