For loop Program in C language

For loop Program  in C language :

For loop Program in C language, This is one of the most frequently used loops in C programming. Syntax of for loop: for (initialization; condition test; increment or decrement.


For loop Program in C language
For loop Program  in C language

For loop Program  in C language :


⇒Print table of 1:

#include<stdio.h>
#include<conio.h>
  void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d\n",i);
}
getch();
}

➤OUTPUT :
1
2
3
4
5
6
7
8
9
10


⇒Table of any number :


#include<stdio.h>
#include<conio.h>
  void main()
{
int i,no;
clrscr();
printf("Enter any number=\n");
scanf("%d",&no);
for(i=1;i<=10;i++)
{
printf("%d",i*no);
}
getch();
}
➤OUTPUT :
Enter any number=
4
4 8 12 16 20 24 28 32 36 40


⇒Print natural number from 1 to n :

#include<stdio.h>
#include<conio.h>
  void main()
{
int i,no;
clrscr();
printf("Enter the any number =\n");
scanf("%d",&no);
printf(" The Natural number from 1 to %d is given below\n",no);
for(i=1;i<=no;i++)
{
printf("%d",i);
}
getch();
}

➤OUTPUT :
Enter any number=
7
The natural number from 1 to 8 is given below
1 2 3 4 5 6 7


⇒Print natural number from 1 to n in reverse order :

#include<stdio.h>
#include<conio.h>
  void main()
{
int i,no;
printf("Enter the any number =\n");
scanf("%d",&no);
printf(" The Natural number from 1 to %d  in reverse order is given below\n",no);
for(i=no;i>=1;i--)
{
printf("%d",i);
}
getch();
}

➤OUTPUT :

Enter any number=
7
The natural number from 1 to %d  in reverse order is given below
7 6 5 4 3 2 1





⇒PRINT SUM OF NATURAL NUMBER FROM 1 TO N :

#include<stdio.h>
#include<conio.h>
  void main()
{
int i,no,sum=0;
printf("Enter the any number =\n");
scanf("%d",&no);
printf(" The Natural number from 1 to %d is given below\n",no);
for(i=1;i<=no;i++)
{
printf("%d",i);
sum=sum+i;
}
printf("\n Total sum=%d",sum);
getch();
}
➤OUTPUT :
Enter any number=
8
The natural number from 1 to 8 is given below
1 2 3 4 5 6 7 8
Total sum=36

⇒PRINT A B C D......Z METHOD 1 :

#include<stdio.h>
#include<conio.h>
  void main()
{

char ch;
for(ch='A';ch<='Z';ch++)
{
printf("%c",ch);
}
getch();
}

➤OUTPUT :
A  B C D E F G H I J K L M N O P  
Q R S T U V W Y  Z


⇒PRINT A B C D .....Z METHODE 2 :
#include<stdio.h>
#include<conio.h>
  void main()
{

int i;
for(i=65;i<=90;i++)
{
printf("%d",i);
}
getch();
}
➤OUTPUT :
A  B C D E F G H I J K L M N O P  
Q R S T U V W Y  Z


⇒PRINT a b c d.....z methode 1:

#include<stdio.h>
#include<conio.h>
  void main()
{

char ch;
for(ch='a';ch<='z';ch++)
{
printf("%c",ch);
}
getch();
}

➤OUTPUT :
a b c d e f g h i j k l m n o p 
q r s t u v w s y z



For loop Program  in C language:


⇒Print a b c d e f.....z methode 2 :

#include<stdio.h>
#include<conio.h>
  void main()
{
int i;
for(i=65;i<=90;i++)
{
printf("%d",i);
}
getch();
}

➤OUTPUT :
a b c d e f g h i j k l m n o p 
q r s t u v w s y z

⇒PRINT ALL EVEN NUMBER BETWEEN 1 AND 20 METHODE 1 :


#include<stdio.h>
#include<conio.h>
  void main()
{
int i;
printf("Enter number B/W 1 and 20 is given below\n");
for(i=2;i<=20;i+2)
{
printf("%d",i);
}
getch();
}

➤OUTPUT :
Enter number B/W 1 and 20 is given below
2 4 6 8 10 12 14 16 18 20

⇒PRINT ALL EVEN NUMBER BETWEEN 1 AND 20 METHODE 2 :


#include<stdio.h>
#include<conio.h>
  void main()
{

int i;
printf("Even number B/W 1 and 20 is given below\n");
for(i=2;i<=20;i+1)
{
if("i%d==0)
printf("%d",1);
}
getch();

}
➤OUTPUT :
Even number B/W 1 and 20 is given below
2 4 6 8 10 12 14 16 18 20




⇒PRINT ALL ODD NUMBER BETWEEN 1 AND 20 METHODE 1 :


#include<stdio.h>
#include<conio.h>
  void main()
{
int i;
printf("Odd number B/W 1 and 20 is given below\n");
for(i=1;i<=20;i+2)
{
printf("%d",i);
}
getch();


}
➤OUTPUT :
Odd number B/W 1 and 20 is given below
1 3 5 7 9 11 13 15 17 19


For loop Program  in C language:


⇒PRINT ALL ODD NUMBER BETWEEN 1 AND 20 METHODE 2 :

#include<stdio.h>
#include<conio.h>
  void main()
{

int i;
printf("Odd number B/W 1 and 20 is given below\n");
for(i=2;i<=20;i+1)
{
if("i%2!==0)
printf("%d",i);
}
getch();


}

➤OUTPUT :
Odd number B/W 1 and 20 is given below
1 3 5 7 9 11 13 15 17 19


PRINT ASCII VALUE OF ALPHABET FROM A TO Z


#include<stdio.h>
#include<conio.h>
  void main()
{
char ch;
for(char ch='A'; ch<='Z';ch++)
{
printf("%c=%d\n",ch,ch);
}
getch();
}
➤OUTPUT :
A=65
B=66
C=67
D=68
E=69
F=70
G=71
H=72

FACTORIAL OF ANY NUMBER :

#include<stdio.h>
#include<conio.h>
  void main()
{
int i,no f=1;
printf("Enter any number=\n");
scanf("%d",&no);
for(i=1;i<=n0;i++)
{
f=f*i;
}
printf("Factorial=%d",f);
getch();
}

➤OUTPUT :
Enter any number=
6
Factorial=720

⇒FACTOR OF ANY NUMBER :

#include<stdio.h>
#include<conio.h>
  void main()
{

int i,no,f=1;
printf("Enter any number=\n");
scanf("%d",&no);
printf("Factor is given below\n");
for(i=1;i<=no;i++)
{
if(no%i==0)
printf("%d",i);
}
getch();
}
➤OUTPUT : 
Enter any number=
8
The factor is given below
1 2 4 8


⇒POWER OF ANY NUMBER :

#include<stdio.h>
#include<conio.h>
  void main()
{
int i,b,p,f=1;
printf("Enter base=\n");
scanf("%d",&b);
printf("Enter power=\n");
scanf("%d",&p);
for(int i=1;i<=p;i++)
{
f=f*b;
}
printf("Result=%d",f);
getch();
}

➤OUTPUT : 
Enter base=
2
Enter power=
5
Result=32


⇒LCM OF TWO NUMBER :

#include<stdio.h>
#include<conio.h>
  void main()
{
int no1,no2,i;
printf("Enter first number=\n");
scanf("%d",&no1);
printf("Enter second number=\n");
scanf("%d",&no2);
for(i=1;;i=i+1)
{
if(i%no1==0&&i%no2==0)
{
printf("LCM of %d and %d is %d",no1,no2,i);
break;
}
 getch();
}
➤OUTPUT :
Enter the first number=
4
Enter the second number=
6
LCM of 4 and 6 is 12


⇒HCF OF TO NUMBER :

#include<stdio.h>
#include<conio.h>
  void main()
{
int no1,no2,i,m=1;
printf("Enter first number=\n");
scanf("%d",&no1);
printf("Enter second number=\n");
scanf("%d",&no2);
for(i=1;;i=i+1)
{
if(i%no1==0&&i%no2==0)
{
m=i;
}
}
printf("LCM of %d and %d is %d",no1,no2,i);
 getch();
}

➤OUTPUT :
Enter the first number=
16
Enter the second number=
24
HCF of 16 and 24 is 8


⇒CHECK GIVEN NUMBER IS PRIME OR NOT :

#include<stdio.h>
#include<conio.h>
  void main()
{
int no,m=0,i;
printf("Enter any number=\n");
scanf("%d",&no);
for(i=2;i<=no-1;i++)
{
if(no%i==0)
{
printf("Number is not Prime");
m=1;
break;
}
}
if(m==0)
printf("Number is prime");
getch();
}

➤OUTPUT :
Enter any number=
43
Number is prime


⇒PRINT PRIME NUMBER BETWEEN 1 TO N :


#include<stdio.h>
#include<conio.h>
  void main()
{
int n,m,num,i,j;
printf("Enter any number upto you want to print prime number=\n");
scanf("%d",&num);
printf("Prime number between 1 and %d is given below\n",num);
for(i=1;i<=num;i++)
{
n=i,m=0;
for(j=2;j<=n-1;j++)
{
if(n%j==0)
{
m=1;
break;
}
}
if(m==0)
printf("%d",n);
}
getch();
}

➤OUTPUT :
Enter any number up to you want to print prime number=
18
A prime number between 1 and %d is given below
12357111317



⇒PRINT FABONIC SERIES UPTO N TERMS :

#include<stdio.h>
#include<conio.h>
 void main()
{
int i,no,a,b=1,c=0;
printf("Enter any number upto you want to print fabonacci series=\n");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
printf("%d",c);
a=b;
b=c;
c=a+b;
}
getch();
}
➤OUTPUT :
Enter any number up to you want to print Fibonacci series=
8
011235813 





Post a Comment

0 Comments