0% found this document useful (0 votes)
406 views4 pages

Goto Statement in C

The document discusses the goto statement in C programming. It provides an example of using goto to unconditionally jump to labels within an if/else statement to check if variable a is greater than b. The program first prompts the user to input two numbers, compares them, and uses goto to jump to labels that print which number is greater before terminating.

Uploaded by

Prerna Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
406 views4 pages

Goto Statement in C

The document discusses the goto statement in C programming. It provides an example of using goto to unconditionally jump to labels within an if/else statement to check if variable a is greater than b. The program first prompts the user to input two numbers, compares them, and uses goto to jump to labels that print which number is greater before terminating.

Uploaded by

Prerna Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

goto statement in C :

goto is a unconditional jump statement.

Syntax :

goto label;

so we have to use the goto carefully inside a conditional statement.

goto.c

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter 2 nos A and B one by one : ");
scanf("%d%d",&a,&b);
if(a>b)
{
goto first;
}
else
{
goto second;
}

first:
printf("\n A is greater..");
goto g;

second:
printf("\n B is greater..");

g:
getch();
}

Program Algorithm / Explanation :


1. #include<stdio.h> header file is included because, the C in-built
statement printf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch()comes under
conio.h header files.
3. main () function is the place where C program execution begins.
4. Two variables a & b of type int are declared.
5. User Inputs are received for a & b using scanf.
6. if condition is used to check whether a is greater than b. if(a>b).
7. if it is true goto statement is used to jump to the label first : and the statement
under first : is executed and then again jump is performed to get to the end of
the program. goto g:
8. if the condition is false a statement is used to jump to label second. goto
second : and the statement under second : is executed.

Output :

Factorial program in c using for loop


Here we find factorial using for loop.

#include <stdio.h>

int main()
{
int c, n, fact = 1;

printf("Enter a number to calculate it's factorial\n");


scanf("%d", &n);

for (c = 1; c <= n; c++)


fact = fact * c;

printf("Factorial of %d = %d\n", n, fact);


return 0;
}
Download Factorial program.

Output of code:

Factorial program in c using function


#include <stdio.h>

long factorial(int);

int main()
{
int number;
long fact = 1;

printf("Enter a number to calculate it's factorial\n");


scanf("%d", &number);

printf("%d! = %ld\n", number, factorial(number));

return 0;
}

long factorial(int n)
{
int c;
long result = 1;

for (c = 1; c <= n; c++)


result = result * c;

return result;
}

Factorial program in c using recursion


#include<stdio.h>

long factorial(int);

int main()
{
int n;
long f;

printf("Enter an integer to find factorial\n");


scanf("%d", &n);

if (n < 0)
printf("Negative integers are not allowed.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}

return 0;
}

long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
Recursion is a technique in which a function calls itself, for example in above code factorial
function is calling itself. To solve a problem using recursion you must first express its solution in
recursive form.

You might also like