Knowee
Questions
Features
Study Tools

algorithm for a program to reverse a number in c programming

Question

algorithm for a program to reverse a number in c programming

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple algorithm to reverse a number in C programming:

  1. Start
  2. Declare variables num, rev and rem.
  3. Read the number from the user and store it in num.
  4. Set rev to 0.
  5. While num is not equal to 0, repeat steps 6 and 7.
  6. Set rem to num modulo 10.
  7. Multiply rev by 10 and add rem to rev.
  8. Divide num by 10.
  9. Once the loop is finished, rev will contain the reversed number.
  10. Print rev.
  11. 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.

This problem has been solved

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

1/3

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.