C Language Tutorial Home

# C Language loops:for loop 6

### Example:26Factorial of a number , get the following outputExample:n=31*2*3=6n=51*2*3*4*5=120Sol:

```#include <stdio.h>

int main()
{
int i,n,f=1;
printf("Enter any no ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
printf("%d ",i);
if(i<n)
printf(" * ");
}
printf(" = %d",f);

return 0;
}
```

### Output:

Enter any no 5
1 * 2 * 3 * 4 * 5 = 120

Enter any no 3
1 * 2 * 3 = 6

### Example:27C program to calculate the value of x to the power y by taking input for the values of base and power?Example :(i). b=2 p=3 : 2*2*2 =8(ii). b=2 p=5 : 2*2*2*2*2 =32Sol:

```#include <stdio.h>

int main()
{
int i,b,p,r=1;
printf("Enter values of base and power ");
scanf("%d %d",&b,&p);
for(i=1;i<=p;i++)
{
r=r*b;
}
printf("Result  = %d",r);

return 0;
}
```

### Output:

Enter values of base and power 2 5
Result = 32

Enter values of base and power 2 3
Result = 8

### Example:28Print All upper case alphabet using for loopPrint all lower case alphabets using for loopPrint all digits using for loopSol:

```#include <stdio.h>

int main()
{
int i;
printf("Upper case alphabets\n");
for(i=65;i<=90;i++)
{
printf("%c ",i);
}
printf("\n");
for(i='A';i<='Z';i++)
{
printf("%c ",i);
}
printf("\nLower case alphabets\n");
for(i=97;i<=122;i++)
{
printf("%c ",i);
}
printf("\n");
for(i='a';i<='z';i++)
{
printf("%c ",i);
}
printf("\nDigits \n");
for(i=48;i<=57;i++)
{
printf("%c ",i);
}
printf("\n");
for(i='0';i<='9';i++)
{
printf("%c ",i);
}

return 0;
}
```

### Output:

Upper case alphabets
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Lower case alphabets
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
Digits
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9

1
2
3
4
5
6
7
8
9
10

### Method: 1.for(a;b;c){}

```/*
Method: 1.
for(a;b;c)
{
}
*/
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
printf("%d\n",i);
}
return 0;
}
```

### Method:2.for(;b;c){}

```/*
Method:2.
for(;b;c)
{
}
*/
#include <stdio.h>
int main()
{
int i=1;
for(;i<=10;i++)
{
printf("%d\n",i);
}
return 0;
}
```

### for(a;;c){}

```/*
Method:3.

for(a;;c)
{
}
*/
#include <stdio.h>
int main()
{
int i;
for(i=1;;i++)
{
printf("%d\n",i);
if(i>=10)
break;
}
return 0;
}
```

### for(a;b;){}

```/*
Method:4.

for(a;b;)
{
}
*/
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=10;)
{
printf("%d\n",i);
i++;
}
return 0;
}
```

### for(a;;){}

```/*
Method:5.

for(a;;)
{
}
*/
#include <stdio.h>
int main()
{
int i;
for(i=1;;)
{
printf("%d\n",i);
if(i>=10)
break;
i++;
}
return 0;
}
```

### for(;b;){}

```/*
Method:6.

for(;b;)
{
}
*/
#include <stdio.h>
int main()
{
int i=1;
for(;i<=10;)
{
printf("%d\n",i);
i++;
}
return 0;
}
```

### for(;;c){}

```/*
Method:7.

for(;;c)
{
}
*/
#include <stdio.h>
int main()
{
int i=1;
for(;;i++)
{
printf("%d\n",i);
if(i>=10)
break;
}
return 0;
}
```

### for(;;){}

```/*
Method:8.

for(;;)
{
}
*/
#include <stdio.h>
int main()
{
int i=1;
for(;;)
{
printf("%d\n",i);
if(i>=10)
break;
i++;
}
return 0;
}
```

Important Pages