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

Introduction

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

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

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

Leave a Comment