0

I need help regarding C program to reverse a number

#include <stdio.h> int main() { long num, reverse = 0, temp, remainder; printf("Enter the number\n"); scanf("%ld", &num); temp = num; while (num > 0) { remainder = num % 10; reverse = reverse * 10 + remainder; num /= 10; } printf("Given number = %ld\n", temp); printf("Its reverse is = %ld\n", reverse); } Please explain to me in simple language and how reverse of an integer comes as output and how this program works

13th Oct 2018, 5:17 AM
Samadhan Patil
Samadhan Patil - avatar
2 Antworten
+ 3
Samadhan Patil let me try to explain it for input as 123... 1.123 > 0 and while condition is true... remainder is 3 and reverse is 0*10+3 = 3 and num i.e. 123 becomes 12 2. 12 > 0 and again while condition is true... remainder is 2 and reverse is 3*10 + 2 = 32 and num i.e. 12 becomes 1 3. 1> 0 and again while condition is true... remainder is 1 and reverse is 32*10 + 1 = 321 and num i.e. 1 becomes 0 this ends while loop and reverse which is output is 321
13th Oct 2018, 6:26 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
How to make a program for print every way of writing n as a sum of natural numbers, with repetitions, in non decreasing order. If we input 5 then its output 1+1+1+1+1 , 1+1+1+2 , 1+1+3 , 1+2+2 , 1+4, 2+3 , 5
10th Feb 2022, 4:51 PM
Abhinav dakshana scholar
Abhinav dakshana scholar - avatar