+ 1

where shoud i place "i" to save "n" value in this code and can show the saved amount of "n" in the last line?

#include <iostream> using namespace std; int main() { int n, s=0,a,i; i=n; cin>>n; while(n>0) { a=n%10; s=s*10+a; n=n/10; } cout<<"enter n"<<i<<":"<<s; return 0; }

26th Aug 2020, 6:45 PM
Nariman Tajari
Nariman Tajari - avatar
3 Réponses
+ 2
The program is read and executed from top to bottom, therefore assigning 'n' to 'i' when neither of them has been initialized with a value is basically worthless. You first need to get the value of 'n' from the user before being able to copy it into 'i'.
26th Aug 2020, 6:51 PM
Shadow
Shadow - avatar
+ 1
Ok Shadow but what should i do if i need to save the amount of so called 'n' and then show it in the output.
26th Aug 2020, 7:03 PM
Nariman Tajari
Nariman Tajari - avatar
+ 1
Firstly ask the user to enter n value with cout<<"Enter n"<<endl; Then use cin>>n; Then assign n to i or i to n i=n; then do the while looping.And finally cout<<i<<s;
26th Aug 2020, 7:52 PM
HBhZ_C
HBhZ_C - avatar