C language loops | while loop 5

Example:21
C program to take input for 5 nos, check and print the largest entered number?
Sol:

#include <stdio.h>

int main()
{
    int i=1,n,m=0;
    while(i<=5)
    {
        printf("Enter any no ");
        scanf("%d",&n);
        if(n>m)
            m=n;
        i++;
    }
    printf("Max no = %d",m);
    return 0;
}

output:

Enter any no 25
Enter any no 96
Enter any no 45
Enter any no 32
Enter any no 4
Max no = 96

Note: The above program will give invalid result only when all the entered numbers are -ve. To make a perfect program we have to make program as given below.

#include <stdio.h>

int main()
{
    int i=1,n,m=0;
    while(i<=5)
    {
        printf("Enter any no ");
        scanf("%d",&n);
        if(i==1)
            m=n;
        if(n>m)
            m=n;
        i++;
    }
    printf("Max no = %d",m);
    return 0;
}

output:

Enter any no 25
Enter any no 96
Enter any no -89
Enter any no 36
Enter any no 4
Max no = 96

Example:22
C program to take input for 5 nos, check and print the lowest entered number?
Sol:

#include <stdio.h>

int main()
{
    int i=1,n,m=0;
    while(i<=5)
    {
        printf("Enter any no ");
        scanf("%d",&n);
        if(i==1)
            m=n;
        if(n<m)
            m=n;
        i++;
    }
    printf("Min no = %d",m);
    return 0;
}

output:

Enter any no 25
Enter any no 96
Enter any no 3
Enter any no 45
Enter any no 85
Min no = 3

Example:23
C program to take input for 10 numbers , check and print the following:
a. max no
b. min no
c. diff between max and min no
Sol:

#include <stdio.h>

int main()
{
    int i=1,n,ma,mi,d;
    while(i<=5)
    {
        printf("Enter any no ");
        scanf("%d",&n);
        if(i==1)
        {
            ma=n;
            mi=n;
        }
        /* max no */
        if(n>ma)
            ma=n;
        /* min no */
        if(n<mi)
            mi=n;
        i++;
    }
    /* diff */
    d=ma-mi;
    printf("Max no = %d Min no = %d diff = %d",ma,mi,d);
    return 0;
}

output:

Enter any no 25
Enter any no 63
Enter any no 3
Enter any no 48
Enter any no 5
Max no = 63 Min no = 3 diff = 60

Example:24
C program to take input for a number calculate and print its table?
Sol:

#include <stdio.h>

int main()
{
    int i=1,n,t;
    printf("Enter any no ");
    scanf("%d",&n);
    while(i<=10)
    {
        t=n*i;
        printf("%d\n",t);
        i++;
    }
    return 0;
}

output:

Enter any no 5
5
10
15
20
25
30
35
40
45
50

Method : 2

#include <stdio.h>

int main()
{
    int i=1,n,t;
    printf("Enter any no ");
    scanf("%d",&n);
    while(i<=10)
    {
        t=n*i;
        printf("%d * %d = %d\n",n,i,t);
        i++;
    }
    return 0;
}

output:

Enter any no 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Example:25
C program to take input for a number calculate and print its factorial?
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;
        i++;
    }
    printf("Fact = %d",f);
    return 0;
}

output:

Enter any no 5
fact = 120

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