Sir,I want the answer for a program.
1. A program to read values until the user enters -1 without using infinite loop?
int n = 0;
do {
// Do rest of the code you want to execute.
printf(“To Exit: Enter -1\n”);
scanf("%d", &n);
} while(n == -1);
/* while.c – entry condition loop */
#include <stdio.h>
int main(void)
{
const int exit_value = -1;
int value_entered;
scanf("%d", &value_entered)
while (value_entered != exit_value)
{
printf("\nEnter a value : ");
printf("\nEnter the exit value : ");
scanf("%d", &value_entered);
}
return 0;
}
int n = 0;
do { // Do rest of the code you want to execute.
printf(“To Exit: Enter -1\n”);
scanf("%d", &n);
} while(!(n == -1));
simplest way to do this without using loop is recursion
very similar to life,universe and everything
int main(){
cin>>n;
if(n==-1)exit(0);
else main();
return 0;
}
3 Likes
sorry… condn needs care.
int n = 0;
do {
// Do rest of the code you want to execute.
printf(“To Exit: Enter -1\n”);
scanf("%d", &n);
} while(!(n == -1));
He wanted it in C brother, anyways…
doesn’t recursion works in c?
ofcourse you can do same in c as well