How to take input from user multiple times using while loop?? | Sololearn: Learn to code for FREE!
+ 3

How to take input from user multiple times using while loop??

I want to take number from user Bhai koi madat kr do hum user se bar bar input lena chahte he https://code.sololearn.com/cHC09zCXblKk/?ref=app

4th Jan 2022, 11:15 PM
Raisun Lakra
10 Answers
+ 1
/* it will not work at Sololearn, because Sololearn takes all the inputs at the beginning. to make it works at Sololearn, you can make an array to store the tables you want, then get the multiplications. https://code.sololearn.com/cCp6o92r4o02/?ref=app example on inputs: 3 5 10 20 you will get 3 tablets (tablets of 5, 10 and 20) */ #include <stdio.h> int main() { int num, i; while(1) // True (non-zero is true) { printf("\n\nEnter a num (0 to stop): "); scanf("%d",&num); if (num == 0) {break;} for(i = 1; i <= 10; i++) { printf("\n%d × %d = %d", num, i, num * i); } } return 0; }
6th Jan 2022, 6:40 AM
Mafdi
Mafdi - avatar
+ 5
As Romeo Cojocaru said, the code playground is different to other environments. I see that another user @lpang added a better way to do this as a comment to your code. Here is my attempt. https://code.sololearn.com/coIOMWzwkQWd/?ref=app
5th Jan 2022, 12:04 AM
HungryTradie
HungryTradie - avatar
+ 3
The corrected code! Also, you need to understand how SoloLearn input / submit a value works. It differs from a classic IDE. Use forum search to learn more on that matter. #1. Why did you used free() ? #2. Why did you used a while loop with a nested for loop? #3. What do you try to accomplish: to input 10 values or just one and print the multiplication table for that value? #include <stdio.h> #include <stdbool.h> int main() { int num, i; //while(true){ for(i=0;i<=10;i++) { printf("Enter a no. :\n"); scanf("%d",&num); //for(i=1;i<=10;i++) printf("%d × %d = %d\n",num,i,num*i); //free(num); } return 0; }
4th Jan 2022, 11:50 PM
Romeo Cojocaru
Romeo Cojocaru - avatar
+ 2
I'm the updated code, change scanf to: scanf(" %d[^\n]", &num); this will keep \n out of the stdin buffer.
5th Jan 2022, 12:05 AM
William Owens
William Owens - avatar
+ 1
Jeff.C I want to take input again and again Program should return result and ask user for input a no one more time
5th Jan 2022, 2:59 AM
Raisun Lakra
0
Romeo Cojocaru I want to make a program which take input from user and after giving the output it will ask for input one more time. This should continue until the user has left the program
5th Jan 2022, 2:53 AM
Raisun Lakra
0
Romeo Cojocaru Eg. Enter no :5 5×1=5 . . . 5×10=50 Enter no :7 7×1=7 . . .7×10=70 Enter no. . . .
5th Jan 2022, 2:55 AM
Raisun Lakra
0
William Owens it's not working
5th Jan 2022, 3:16 AM
Raisun Lakra
0
William Owens can you please run this code on your pc after removing free() May be because of sololearn it's not asking user for input one more time and taking previous input as the current
5th Jan 2022, 3:23 AM
Raisun Lakra
0
#include <stdio.h> int main(void) { while(1==1) { puts("Please enter a number to multiply by 10: \n"); long long int user_number = 0; scanf(" %lli", &user_number); printf("%lli x 10 = %iil\n", user_number, user_number * 10); } } This is a forever loop that will ask the user for a number and multiply it by 10. Hopefully it will get you started.
5th Jan 2022, 12:37 PM
William Owens
William Owens - avatar