C language loops | while loop 6

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=1,n,f=1;
    printf("Enter any no ");
    scanf("%d",&n);
    while(i<=n)
    {
        f=f*i;
        printf("%d",i);
        if(i<n)
            printf(" * ");
        i++;
    }
    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: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=1,b,p,r=1;
    printf("Enter values of base and power ");
    scanf("%d %d",&b,&p);
    while(i<=p)
    {
        r=r*b;
        i++;
    }
    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:28
Print All upper case alphabet using while loop
Print all lower case alphabets using while loop
Print all digits using while loop
Sol:

#include <stdio.h>

int main()
{
    int i=65;
    printf("Upper case alphabets\n");
    while(i<=90)
    {
        printf("%c ",i);
        i++;
    }
    printf("\n");
    i='A';
    while(i<='Z')
    {
        printf("%c ",i);
        i++;
    }
    printf("\nLower case alphabets\n");
    i=97;
    while(i<=122)
    {
        printf("%c ",i);
        i++;
    }
    printf("\n");
    i='a';
    while(i<='z')
    {
        printf("%c ",i);
        i++;
    }
    printf("\nDigits\n");
    i=48;
    while(i<=57)
    {
        printf("%c ",i);
        i++;
    }
    printf("\n");
    i='0';
    while(i<='9')
    {
        printf("%c ",i);
        i++;
    }
    return 0;
    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

For Loop      While Loop      Do while Loop

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects