Q:

Write C program to reverse a number using while & for loop

0

Write C program to reverse a number using while & for loop

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Reverse a Number using while loop

#include < stdio.h >

    int main() {
    int num, rev = 0;

    //Reading a number
    printf("Please enter a number: ");
    scanf("%d", & num);
    
    //finding reverse number using while loop
     while (num > 0) {
      rev = rev * 10;
      rev = rev + num % 10;
      num = num / 10;
    }

    printf("Reversed number is = %d ", rev);
    return 0;
}

Reverse a Number using for loop

#include < stdio.h >
    int main() {
    int num, rev;

    //Reading a number
    printf("Please enter a number: ");
    scanf("%d", & num);

    //finding reverse number using for loop
    for (rev = 0; num > 0; num = num / 10) {
      rev = rev * 10;
      rev = rev + num % 10;
    }

    printf("Reversed number is = %d", rev);
    return 0;
  }

Result:

Please enter a number: 123

Reversed number is = 321

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write C program to calculate power using while & f... >>
<< Write C program to calculate product of digits of ...