C Language loops-for 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,n,m=0;
    for(i=1;i<=5;i++)
    {
        printf("Enter any no ");
        scanf("%d",&n);
        if(n>m)
            m=n;
    }
    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 results only when all the entered numbers are -ve. To make a perfect program we have to make the program as given below.

#include <stdio.h>

int main()
{
    int i,n,m=0;
    for(i=1;i<=5;i++)
    {
        printf("Enter any no ");
        scanf("%d",&n);
        if(i==1)
            m=n;
        if(n>m)
            m=n;
    }
    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:

Output:

#include <stdio.h>

int main()
{
    int i,n,m=0;
    for(i=1;i<=5;i++)
    {
        printf("Enter any no ");
        scanf("%d",&n);
        if(i==1)
            m=n;
        if(n<m)
            m=n;
    }
    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,n,ma,mi,d;
    for(i=1;i<=5;i++)
    {
        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;
    }
    /* 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,n,t;
    printf("Enter any no ");
    scanf("%d",&n);
    for(i=1;i<=10;i++)
    {
        t=n*i;
        printf("%d\n",t);
    }

    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,n,t;
    printf("Enter any no ");
    scanf("%d",&n);
    for(i=1;i<=10;i++)
    {
        t=n*i;
        printf("%d * %d = %d\n",n,i,t);
    }

    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,n,f=1;
    printf("Enter any no ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        f=f*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