Wednesday, 2 June 2021
Write C program to display the first five odd numbers using goto statement
goto syntax in C
Syntax:- goto label_name;
A statement label is defined in exactly the same way as a variable name, which is a sequence of letters and digits, the first of which must be a letter. The statement liable must be followed by a colon (:). Like other statements, the goto statement ends with a semicolon.
The goto is an unconditional branching statement and its use is discouraged in structured programming. In a language that provides the various loop statements like ‘for’, ‘while’, and ‘do-while’ as well as the switch statement, there is normally no need to use the ‘goto’ statement. Often, a break statement or a continue statement can eliminate the need for a goto statement.
Because the goto statement can interfere with the normal sequence of processing, it makes a program more difficult to read and maintain.
Examples of C goto
Program1:- Print first N natural numbers in C programming using the goto statement.
#include
int main()
{
int n, i=1;
printf("Enter a number: ");
scanf("%d",&n);
start:
printf("%d\t",i);
i++;
if(i
int main()
{
int n, sum=0;
start:
printf("Enter a number: ");
scanf("%d",&n);
if(n<0) goto end;
sum = sum + n;
goto start;
end:
printf("Sum = %d",sum);
return 0;
}
Output:-
Enter a number: 10
Enter a number: 15
Enter a number: -5
Sum = 25
In this program, two goto statements are used. The first goto statement (goto end and label end) works as a break statement and the second goto statement (goto start and label start) works as a loop.
Use of goto statement
The goto statement is used:-
To execute a group of statements repeatedly for a particular number of times.
To come out of several nested loops at one stroke.
Program3:- Write a program to find factorial of a given number in C programming using the goto statement.
#include
int main()
{
int num;
long fact = 1;
printf("Enter a number: ");
scanf("%d",&num);
if(num<0) goto end;
for(int i=1; i<=num; i++)
fact = fact * i;
printf("Factorial of %d is = %ld", num, fact);
end:
return 0;
}
Output:-
Enter a number: 5
Factorial of 5 is = 120
In the above program for calculating the factorial, we used a goto statement and for loop. We can do the same without using for loop also. The below program is without for loop.
#include
int main()
{
int num;
long fact = 1;
printf("Enter a number: ");
scanf("%d",&num);
if(num<0) goto end;
int i=1;
loop:
fact = fact * i;
i++;
if(i<=num) goto loop;
printf("Factorial of %d is = %ld", num, fact);
end:
return 0;
}
Output:-
Enter a number: 5
Factorial of 5 is = 120
Enter a number: 6
Factorial of 6 is = 720
Enter a number: 7
Factorial of 7 is = 5040
Note:- Generally, we don’t use the goto statement in C programming. It is recommended to use loops instead of using goto statement. Use goto statement, only if loops can’t do that operation.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!
Subscribe to:
Post Comments (Atom)
C Programming
What is DBMS in brief?
A Database Management System (DBMS) is a software suite designed to efficiently manage, organize, store, manipulate, and retrieve data. It a...
-
Jainism (/ˈdʒeɪnɪzəm/),[1] frequently accustomed as Jain Dharma, is an age-old Indian religion. Jain dharma traces its airy account and hist...
-
A calibration adjudicator is a apparatus for barometer lengths and appointment abstracts at a anchored arrangement of length; two accepted e...
No comments:
Post a Comment