Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

which one is better and why ?

+1 vote
asked Sep 17, 2019 by anonymous
In the given below code which loop(other loop on comment block) is better to use and why ?

main()
{
int i,x=0,r,n;
printf("enter the digits");
scanf("%d",&n);
while(n!=0){                                                   //for(i=0;i<n;i++)

r=n%10;
x=x*10+r;
n=n/10;

}
printf("%d",x);
getch();

}

4 Answers

0 votes
answered Sep 19, 2019 by suraj

both  loops are approaximately same in terms of space complexity.

so, in my view both are same but, you can use that one those are easier for you.

0 votes
answered Sep 19, 2019 by Nandu
Those two conditions n!=0 and n=n/10 can be given at the same time if you had gone for 'for' loop

it can be written as for(;n!=0;n=n/10)
0 votes
answered Sep 20, 2019 by Raja Sekhar (260 points)

In this case use while loop

storage space is very important but in the given for loop 'i' is an avoidable variable used

0 votes
answered Sep 22, 2019 by Kankana

while(n!=0) would be more appropriate since the digit can be positive as well as negative.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...