C language goto label name examples Set 2

Set 1:Q1-Q4    Set 2:Q5-Q8

Question:5

Write a C program to print numbers from 1 to 10 and also print the sum of all numbers(using goto labelname)?

Sol:

#include <stdio.h>
int main()
{
    int i=1,s=0;
    abc:
        s=s+i;
        printf("%d\n",i);
        i=i+1;
        if(i<=10)
            goto abc;
        printf("Sum = %d",s);

    return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10
Sum = 55

Question:6

Write a C program to print all even numbers upto 20 and also print sum of even numbers(using goto labelname)?

Sol:

#include <stdio.h>
int main()
{
    int i=2,s=0;
    abc:
        s=s+i;
        printf("%d\n",i);
        i=i+2;
        if(i<=20)
            goto abc;
        printf("Sum = %d",s);

    return 0;
}

Output:

2
4
6
8
10
12
14
16
18
20
Sum = 110

Question:7

Write a C program to print all odd numbers upto 25 and also print sum of odd numbers(using goto labelname)?

Sol:

#include <stdio.h>
int main()
{
    int i=1,s=0;
    abc:
        s=s+i;
        printf("%d\n",i);
        i=i+2;
        if(i<=25)
            goto abc;
        printf("Sum = %d",s);

    return 0;
}

Output:

1
3
5
7
9
11
13
15
17
19
21
23
25
Sum = 169

Question:8

Write a C program to print numbers from 1 to 5 and also print product of all numbers(using goto labelname)?

Sol:

#include <stdio.h>
int main()
{
    int i=1,p=1;
    abc:
        p=p*i;
        printf("%d\n",i);
        i=i+1;
        if(i<=5)
            goto abc;
        printf("Product = %d",p);

    return 0;
}

Output:

1
2
3
4
5
Product = 120

Set 1:Q1-Q4    Set 2:Q5-Q8

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