C Language loops:for loop 3

Example:11
Write a C program to Print all multiples of 7 up to 70 and print sum of all the nos?
Sol:

#include <stdio.h>
int main()
{
    int i,s=0;
    for(i=7;i<=70;i=i+7 )
    {
        printf("%d\n",i);
        s=s+i;
    }
    printf("Sum = %d\n",s);
    return 0;
}

Output:

7
14
21
28
35
42
49
56
63
70
Sum = 385

Example:12
Write a C program to to take input for 5 numbers calculate and print their sum?
Sol:

#include <stdio.h>
int main()
{
    int i,n,s=0;
    for(i=1;i<=5;i=i+1 )
    {
        printf("Enter any no ");
        scanf("%d",&n);
        s=s+n;
    }
    printf("Sum = %d\n",s);
    return 0;
}

Output:

Enter any no 2
Enter any no 3
Enter any no 6
Enter any no 5
Enter any no 4
Sum = 20

Example:13
Write a C program to take input for 3 numbers calculate and print their product?
Sol:

#include <stdio.h>

int main()
{
    int i,n,p=1;
    for(i=1;i<=3;i=i+1 )
    {
        printf("Enter any no ");
        scanf("%d",&n);
        p=p*n;
    }
    printf("Prod = %d\n",p);
    return 0;
}

Output:

Enter any no 2
Enter any no 3
Enter any no 5
Prod = 30

Example:14
Write a C program to take input for 5 nos , check and print total +ve elements entered by the user.
Sol:

#include <stdio.h>

int main()
{
    int i,n,p=0;
    for(i=1;i<=5;i=i+1 )
    {
        printf("Enter any no ");
        scanf("%d",&n);
        if(n>0)
            p++;
    }
    printf("Total +ve elements = %d\n",p);
    return 0;
}

Output:

Enter any no 25
Enter any no -96
Enter any no 35
Enter any no -85
Enter any no 4
Total +ve elements = 3

Example:15
Write a C program to take input for 5 nos , check and print total -ve elements entered by the user.
Sol:

#include <stdio.h>

int main()
{
    int i,n,p=0;
    for(i=1;i<=5;i=i+1 )
    {
        printf("Enter any no ");
        scanf("%d",&n);
        if(n<0)
            p++;
    }
    printf("Total -ve elements = %d\n",p);
    return 0;
}

Output:

Enter any no 25
Enter any no -5
Enter any no 2
Enter any no -6
Enter any no -7
Total -ve elements = 3

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