Example Program Using ‘for’ Loop

Unlike any programming, language loops play a very important role in C or C++ . Here is some example program using for loop.
-
C Program to print n natural number using for loop:
A natural number is a number that occurs commonly and obviously in nature. As such, it is a whole, non-negative number. The set of natural numbers, denoted N, can be defined in either of two ways: N = {0, 1, 2, 3, …}
#include<studio.h>
void main()
{
int i;
printf("Enter any value");
scanf("%d",&i);
for(j=1;j<=i;j++)
{
printf("Value of j is now %d\n",j);
}
}
-
C program to find prime no between n value given by user:
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are 1,2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.
#include<stdio.h>
void main()
{
int i,j,n,count=0;
clrscr();
printf("Enter n term value to search prime");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
count=0;
for ( j =1 ; j <= i ; j++ )
{
if ( i % j == 0 )
count=count+1;
}
if ( count<=2 )
printf("Prime number.=%d\n",i);
}
getch();
}
Output
-
C Program to check how many number are divisible by 5 and 3 between given range by user:
When one number can be divided by another and the result is an exact whole number. Example: 15 is divisible by 3, because 15 ÷ 3 = 5 exactly. In this case, we use logical “&&” for checking two conditions, if here any condition returns false the whole expression is return false.
#include <stdio.h>
int main()
{
int range1,range2, result,i;
printf("Enter Start value of range:");
scanf("%d",&range1);
printf("Enter End value of range:");
scanf("%d",&range2);
printf("##################################################");
printf(" The following No is divisible by 5 and 3 both");
printf("##################################################");
for(i=range1; i<=range2; i++)
{
if (i%5==0 && i%3==0)
{
printf("%d \t",i); //\t is escape sequence used to put a tab
}
}
getch();
return 0;
}
Output
-
C Program to print Fibonacci series:
Fibonacci series is sequence of number like that 0,1,1,2,3,5,8,13…….n . The basic algorithm is {3rd number = 1st number + 2nd number} and 1st and 2nd number can start with any natural number. for example, you can start series with 4,5,9,14,23…….n . In mathematics and computer science, this sequence has many application.
#include<stdio.h>
int main()
{
int n, fn = 0, sn = 1, tmp, k;
clrscr();
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( k = 0 ; k < n ; k++ )
{
printf("%d\t",fn);
tmp = fn + sn;
fn = sn;
sn = tmp;
}
getch();
return 0;
}
Output
- C Program to calculate factorial of a given number:
In mathematics, the factorial of a non-negative integer n, denoted by “n!”, is the product of all positive integers less than or equal to n. n! = n*(n-1)*(n-2)*(n-3)…3.2.1 and zero factorial is defined as one i.e. 0! = 1.
#include <stdio.h>
int main()
{
int i, v, fact = 1;
clrscr();
printf("Enter a number to calculate it's factorial\n");
scanf("%d", &v);
if (v==0)
{
fact=1;
printf("Factorial of %d = %d\n", v, fact);
}
else
{
for (i = 1; i <= v; i++)
{
fact = fact * i;
}
printf("Factorial of %d = %d\n", v, fact);
}
getch();
return 0;
}
Output
Subroto Mondal
Latest posts by Subroto Mondal (see all)
- Installing and Configuring OpenShift: A Step-by-Step Guide for Linux Experts with Terminal Code - February 19, 2023
- Sed Command in Linux with Practical Examples - February 19, 2023
- Rsync Command uses with examples - February 19, 2023




