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


Introduction

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


techstudy.org

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

Reverse a Number using while loop

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#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

C language

     





Related Topics


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.