Find elements

C program to reverse a number using loops

C program to reverse a number using loops

In this article, we will discuss the concept of the C program to reverse a number using loops

In this post, we are going to learn how to find reverse number of the given number in C programming language

C  code to reverse a number using for loop

Program 1

The program allows  the user to enter a number and it displays the reverse pattern of the given number using for loop in C language

when you are entered 45678,
The output will be displayed as 87654

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,rem,reverse=0;
    printf("Enter a number for find reverse\n");
    scanf("%d",&num);
    printf("You entered %d\n",num);
    for(;num!=0; num=num/10){
        rem=num%10;
        reverse=reverse*10+rem;
    }
    printf("Reverse of the given number %d",reverse);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Explanation

  • Declare and initialize three variables as follows  int num,rem,reversed_Num=0;
  • The user is asked to enter a number and it is stored in the integer variable of ‘num
  • The for loop is used to find the reversed number of the given number
  • The for loop is functioning until ‘ num’ is not equal to zero
  • Finally, the output is displayed as the reversed number

 

C  code to reverse a number using while loop

Program 2

The program allows  the user to enter a number and it displays the reverse pattern of the given number using while loop in C language

when you are entered 13579,
The output will be displayed as 97531

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,reverse=0,rem;
    printf("Enter a number for find reverse\n");
    scanf("%d",&num);
    printf("You entered %d\n",num);
    while(num!=0){
        rem=num%10;
        reverse=reverse*10+rem;
        num/=10;
    }

    printf("Reverse number %d\n",reverse);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

 

Explanation

  • Declare and initialize three variables as follows int num,rem,reversed_Num=0;
  • The user is asked to enter a number and it is stored in the integer variable of ‘num
  • The while loop is used to find the reversed number of the given number
  • The while loop is functioning until ‘ num‘ is not equal to zero
  • Finally, the output is displayed as the reversed number

 

C  code to reverse a number using do-while loop

Program 3

The program allows  the user to enter a number and it displays the reverse pattern of the given number using do-while loop in C language

when you are entered 54321
The output will be displayed as 12345

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,reverse=0,rem;
    printf("Enter a number for find reverse\n");
    scanf("%d",&num);
    printf("You entered %d\n",num);
    do{
        rem=num%10;
        reverse=reverse*10+rem;
        num/=10;
    }while(num!=0);

    printf("Reverse number %d\n",reverse);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

 

Explanation

  • Declare and initialize three variables as follows int num,rem,reversed_Num=0;
  • The user is asked to enter a number and it is stored in the integer variable of ‘num
  • The do-while loop is used to find the reversed number of the given number
  • The do-while loop is functioning until ‘ num‘ is not equal to zero
  • Finally, the output is displayed as the reversed number

Suggested for you

For loop in C language

While loop in C language

Do while loop in C language

 

Similar post

Reversed string in Java language

Reversed string in C language

Reversed string in C++ language

C++ program to reverse a number using loops

Java program to reverse a number using loops

 

C++ program to reverse a number using loops
Program to Check whether an Alphabet is vowel or consonant in C++
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

19 hours ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago