algorithm for a program to reverse a number in c programming
Question
algorithm for a program to reverse a number in c programming
Solution
Sure, here is a simple algorithm to reverse a number in C programming:
- Start
- Declare variables
num,revandrem. - Read the number from the user and store it in
num. - Set
revto 0. - While
numis not equal to 0, repeat steps 6 and 7. - Set
remtonummodulo 10. - Multiply
revby 10 and addremtorev. - Divide
numby 10. - Once the loop is finished,
revwill contain the reversed number. - Print
rev. - End
Here is the C code that implements this algorithm:
#include <stdio.h>
int main() {
int num, rev = 0, rem;
printf("Enter an integer: ");
scanf("%d", &num);
while(num != 0) {
rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
}
printf("Reversed number: %d", rev);
return 0;
}
This program reads an integer from the user, reverses that number, and prints the reversed number.
Similar Questions
Write a C program to find reverse of a string using pointers
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in reverse binary. For an integer x, the algorithm is:As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is:6the output is:0116 in binary is 110; the algorithm outputs the bits in reverse.
Write a program in C to print a string in reverse using a character pointer.Also print the uppercase of the first letter of the reversed word.
#include <stdio.h>int main(){ int num; int sum=0, rev=0; printf("Enter a number"); scanf("%d", &num); while(num>0) { sum=sum + num%10; rev=rev*10 + num%10; num=num/10; } printf("sum=%d\n", sum); printf("reverse=%d\n", rev); return 0;}
Write a C++ program to find Reverse of a Number using class.inputEnter a number: 12345output54321
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.