Ask coding questions

← Back to all posts
How to take input again if the user inputs '2'
AaronPhilip (4)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=0,b=0,cases=0,count=0,i=0,j=0;
cin>>cases;
for(i=0;i<cases;i++)
{
cin>>a>>b;
for(i=a;i<=b;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
cout<<i<<endl;
}
cout<<endl;
}
getch();
}

in the above prgram if input is :
Input:
2
1 10
3 5

Output:
2
3
5
7

3
5

i have written the prime number logic but how to run it again if the user inputs 2

Thank you

Answered by DynamicSquid (4639) [earned 5 cycles]
View Answer
Comments
hotnewtop
DynamicSquid (4639)

Maybe a while loop?

int again = 2;
while (again == 2)
{
  // do stuff
  cin >> again;
}
AaronPhilip (4)

@DynamicSquid Thank you it helped, i finished the program :)

DynamicSquid (4639)

@AaronPhilip np! glad it worked out for you :)

CodeLongAndPros (1591)

Well for one conio is a ms-DOS header and repl is on UNIX

PattanAhmed (1397)

@AaronPhilip Hi,
Please attach your Repl for more clarification next time
Thanks!

AaronPhilip (4)

@PattanAhmed For Sure! Thank you for your time.

Coder100 (17018)

Send the repl instead of pasting the code next time, thanks!

AaronPhilip (4)

@Coder100 For sure! Thank you for your time.