BACK TO 10+2 STEM LAB
10+2 MATHEMATICS & DRAWINGS•2016-07-22•4 MIN READ

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.

Key Architecture Takeaways

  • ›
    Point 1: Core computer science and engineering computing concept: Example Program Using 'for' Loop.
  • ›
    Point 2: Detailed trace of memory representation, algorithmic complexity, and control structures.
  • ›
    Point 3: Includes portable ISO C (C17/C23) code examples verified with modern GCC and Clang.
Notice

Compiler & Toolchain Modernization

While legacy educational environments used 16-bit DOSBox Turbo C++, all code presented here adheres to standard ANSI/ISO C standards. You can compile these programs cleanly on modern Linux, Windows (MSYS2/WSL), or macOS using:

bash
> gcc -Wall -Wextra -O2 program.c -o program
> ./program
> ```


---

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, ...}

c #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); } }

SHELL
- 

## ** 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.

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

SHELL
Output

[![prime output](http://technhit.in/wp-content/uploads/2016/07/prime-output.png)](http://technhit.in/wp-content/uploads/2016/07/prime-output.png)

 	
- 

## ** 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.

c #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; }

SHELL
Output

[![divisible](http://technhit.in/wp-content/uploads/2016/07/divisible.png)](http://technhit.in/wp-content/uploads/2016/07/divisible.png)

 	
- 

## **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.

c #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; }

SHELL
** Output**

[![fibonacci](http://technhit.in/wp-content/uploads/2016/07/fibonacci.png)](http://technhit.in/wp-content/uploads/2016/07/fibonacci.png)

 	
- **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*.

c #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

Example Program factorialExample Program factorial

π
TechNHIT STEM Faculty
Department of Applied Mathematics & Physics
Example Program Using 'for' Loop | TechNHIT 10+2 STEM Lab | TechNHIT