C program to reverse a number, to print chess board, to find prime number between 1 to n and to print Multiplication table of any numbers up to any terms using for loop | C programming Language
A
for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Syntax
The syntax of a
for loop in C programming language is −
for ( init; condition; increment ) {
statement(s);
}
Here is the flow of control in a 'for' loop −
- The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
- Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the 'for' loop.
- After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
- The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the 'for' loop terminates.
C program to reverse a number using for loop
#include <stdio.h> int main () { int a; /* for loop execution */ for( a = 10; a < 20; a = a + 1 ){ printf("value of a: %d\n", a); } return 0; }
C program to Print Chess
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,num;printf("Enter the number : ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
for(j=0;j<=num;j++)
{
printf("%c",219);//219 is the ASCII cose for whiteprintf(" ");
}printf("\n");
if(i%2==0)
{
printf("%c",255);//255 is the ASCII cose for white} } return 0; }
C program to Find Prime Number between 1 to n
#include<stdio.h> int main() { int n,i,j,count; printf("Enter the Number:"); scanf("%d",&n); printf("Prime number between %d to %d are\n",1,n); for(i=2;i<=n;i++) { count=1; for (j=2;j<=i/2;j++) {if (i%j==0) {count=0; break; } } if(count==1) {printf("%d ",i);} } return 0;
}
C Program to print Multiplication table of any numbers up to any terms
#include<stdio.h>
int main()
{
int n1,n2,i;
printf("Enter the number:");
scanf("%d",&n1);
printf("Enter the term up to which you want to multiply:");
scanf("%d",&n2);
for(i=1;i<=n2;i++)
{
printf("%d X %d = %d\n",n1,i,n1*i);
}
return 0;
}
OUTPUT