how can i make this program repeat until user enters 1? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

how can i make this program repeat until user enters 1?

/*Write a C++ program that takes a positive integer from user and store it in variable posNumber. Follow these conditions; If the number is less than 1, print wrong input. If it is 1, Print its value. If value is greate than 1, check the value is it Even or Odd. If it is Even, half it and print. If it is Odd, multiply it by 3 and print result. Repeat the whole process until user enter 1.*/ #include <stdio.h> int main (){ int pnum,p,m; printf("Enter a Positive Integer:"); scanf("%d",&pnum); if(pnum<1){ printf("wrong input !!!"); } else if(pnum>1){ if(pnum%2==0){ p=pnum/2; printf("%d is posivite",p); } else { m=pnum*3; printf("%d",m); } } else if(pnum=1) { printf("you entred %d",pnum); } return 0; }

7th Jan 2019, 9:28 PM
Abdul Bari Abbasi
Abdul Bari Abbasi - avatar
2 Answers
+ 9
You can do that with a while loop, which takes pnum as it's statement. So just place all the code in main in this: while(pnum! =1){enter code here} I'd also check all my if statements for misplaced assignment operators(=) instead of equality operators(==). 😉😊 Good luck and stay hungry!
7th Jan 2019, 9:55 PM
Denis Cvetanov
Denis Cvetanov - avatar
+ 2
Thanks Denis :)
8th Jan 2019, 10:02 AM
Abdul Bari Abbasi
Abdul Bari Abbasi - avatar