C Language: break and continue statement examples

Question:1
Write a c program to take input for numbers, calculate and print their sum, the program should get terminated when 0(zero) is entered?
Sol:

#include <stdio.h>
int main()
{
    int n,s=0;
    while(1) //for(;;)
    {
        printf("Enter number to add, to end enter 0 (zero) ");
        scanf("%d",&n);
        if(n==0)
            break;
        s=s+n;
    }
    printf("Sum = %d\n",s);
    return 0;
}

Output:

Enter number to add, to end enter 0 (zero) 2
Enter number to add, to end enter 0 (zero) 3
Enter number to add, to end enter 0 (zero) 6
Enter number to add, to end enter 0 (zero) 5
Enter number to add, to end enter 0 (zero) 0
Sum = 16

Question:2
Write a C program to take input for numbers, calculate and print their product. The program should get terminated when zero(0) is entered.
Sol:

#include <stdio.h>
int main()
{
    int n,p=1;
    while(1) //for(;;)
    {
        printf("Enter number , to end enter 0 (zero) ");
        scanf("%d",&n);
        if(n==0)
            break;
        p=p*n;
    }
    printf("Product = %d\n",p);
    return 0;
}

Output:

Enter number , to end enter 0 (zero) 2
Enter number , to end enter 0 (zero) 3
Enter number , to end enter 0 (zero) 6
Enter number , to end enter 0 (zero) 0
Product = 36

Question:3
Write a C program to take input for numbers, count and print total +ve entered numbers. The program should get terminated when zero(0) is entered. (Program can be terminated using any value)
Sol:

#include <stdio.h>
int main()
{
    int n,p=0;
    while(1) //for(;;)
    {
        printf("Enter number , to end enter 0 (zero) ");
        scanf("%d",&n);
        if(n==0)
            break;
        if(n>0)
            p++;
    }
    printf("Total +ve elements  = %d\n",p);
    return 0;
}

Output:

Enter number , to end enter 0 (zero) 2
Enter number , to end enter 0 (zero) 3
Enter number , to end enter 0 (zero) 6
Enter number , to end enter 0 (zero) -9
Enter number , to end enter 0 (zero) 6
Enter number , to end enter 0 (zero) 0
Total +ve elements = 4

Question:4
Write a C program to take input for numbers, count and print total -ve entered numbers. The program should get terminated when zero(0) is entered. (Program can be terminated using any value)
Sol:

#include <stdio.h>
int main()
{
    int n,p=0;
    while(1) //for(;;)
    {
        printf("Enter number , to end enter 0 (zero) ");
        scanf("%d",&n);
        if(n==0)
            break;
        if(n<0)
            p++;
    }
    printf("Total -ve elements  = %d\n",p);
    return 0;
}

Output:

Enter number , to end enter 0 (zero) 2
Enter number , to end enter 0 (zero) 3
Enter number , to end enter 0 (zero) 9
Enter number , to end enter 0 (zero) -8
Enter number , to end enter 0 (zero) -7
Enter number , to end enter 0 (zero) -36
Enter number , to end enter 0 (zero) -5
Enter number , to end enter 0 (zero) 0
Total -ve elements = 4

Question:5
Write a C program to take input for numbers, calculate and print their sum of all +ve entered numbers. The program should get terminated when zero(0) is entered.
Sol:

#include <stdio.h>
int main()
{
    int n,s=0;
    while(1) //for(;;)
    {
        printf("Enter number , to end enter 0 (zero) ");
        scanf("%d",&n);
        if(n==0)
            break;
        if(n>0)
            s=s+n;
    }
    printf("Sum of all -ve elements  = %d\n",s);
    return 0;
}

Output:

Enter number , to end enter 0 (zero) 2
Enter number , to end enter 0 (zero) 3
Enter number , to end enter 0 (zero) 6
Enter number , to end enter 0 (zero) -9
Enter number , to end enter 0 (zero) 0
Sum of all -ve elements = 11

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