Example:26
Factorial of a number , get the following output
Example:
n=3
1*2*3=6
n=5
1*2*3*4*5=120
Sol:
#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 = 120Enter any no 3
1 * 2 * 3 = 6
Example:27
C 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 =32
Sol:
#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 = 32Enter values of base and power 2 3
Result = 8
Example:28
Print All upper case alphabet using for loop
Print all lower case alphabets using for loop
Print all digits using for loop
Sol:
#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
Example:29
Different forms of for loop
Note: all the argument passed in for loops are optional.
For loop can take following forms
1. for(a;b;c)
2. for(;b;c)
3. for(a;;c)
4. for(a;b;)
5. for(a;;)
6. for(;b;)
7. for(;;c)
8. for(;;)
Sol:
The output is same for all the methods:
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; }
Method:3.
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; }
Method:4.
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; }
Method:5.
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; }
Method:6.
for(;b;)
{
}
/* Method:6. for(;b;) { } */ #include <stdio.h> int main() { int i=1; for(;i<=10;) { printf("%d\n",i); i++; } return 0; }
Method:7.
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; }
Method:8.
for(;;)
{
}
/* Method:8. for(;;) { } */ #include <stdio.h> int main() { int i=1; for(;;) { printf("%d\n",i); if(i>=10) break; i++; } return 0; }