Wednesday - September 13, 2023
#C Program #Algorithm #Blog #C, C++ and C#

Reverse numbers program in c, java, and flowchart

Reverse numbers program

I used the C programming language to create the programme. C is a general-purpose programming language that is frequently used to create different systems and applications. It was first created in the 1970s and has since grown to be among the most well-known and significant programming languages.

The program shows how to use a straightforward function to flip a given integer. It makes use of fundamental C programming ideas including variables, loops, arithmetic, and input/output processing.

A number reversal program takes an input number and returns its reversed version. For example, if the input number is 12345, the reversed number would be 54321.

Any number of programming languages, including C, Java, Python, and Fortran, can be used to create a number reversal programme. Depending on the programming language, the specific syntax and structure may change, but the fundamental logic does not.

When you need to reverse a number for mathematical operations, data processing, or to create numerical patterns, for example, this kind of programme comes in handy.

Algorithm to reverse a given number.

  • This is the algorithm to print the reverse of an accepted number
  • n- Accepted number
  • r- variable for remainder
  • rev- the reverse of a number

Diagram of flowchart reverse numbers program

Reverse numbers, Reverse numbers program
Image Source ~ Crafted With ©Ishwaranand – 2020 ~ Image by ©Ishwaranand

Flowchart of reversal number program?

The “Start” icon marks the start of the flowchart. After then, the user is asked to enter a number. The reversed number variable is set to 0 by the programme during runtime.

The “While” sign is used to denote a loop that the programme then enters. As long as the number is not equal to 0, the loop will keep going. The programme applies the modulo operator (number% 10) to determine the residual within the loop. It multiplies the reversed number by 10 and then adds the difference to update it. The rightmost digit is then removed from the number by dividing it by 10 to update it.

The programme ends the loop when the number reaches 0 and then outputs the reversed number. The “End” sign, which denotes the program’s conclusion, is the last symbol on the flowchart.

The successive actions and choices made by the number reversal programme are shown visually in this flowchart

Reversal number program paths

Start
↓
Input a number
↓
Initialize reversedNumber = 0
↓
While number ≠ 0
    ↓
    Calculate remainder = number % 10
    ↓
    Update reversedNumber = (reversedNumber * 10) + remainder
    ↓
    Update number = number / 10
    ↓
End While
↓
Output reversedNumber
↓
End

Reverses number program in Fortran steps:

step 1: start
step 2: read n
step 3: Set rev=0
step 4: repeat steps 5,6 & 7 unit n!=0
step 5: set r = n % 10
step 6: set rev = rev*10 + r
step 7: set n=n/10
step 8: print rev
step 9: stop

Example of Fortran steps program in reverses number:

program ReverseNumber
    implicit none

    integer :: number, reversedNumber, remainder

    ! Prompt the user to enter a number
    write(*,*) "Enter a number: "
    read(*,*) number

    reversedNumber = 0

    ! Reverse the number
    do while (number /= 0)
        remainder = mod(number, 10)
        reversedNumber = reversedNumber * 10 + remainder
        number = number / 10
    end do

    ! Print the reversed number
    write(*,*) "Reversed number: ", reversedNumber

end program ReverseNumber

C program of reverses numbers

This programme gives a simple C programming language example of how to reverse numbers.

Reverses numbers program in C?

The function reverse number is defined in this programme; it accepts an integer as input and outputs the number’s reversed form. Starting with the rightmost digit, it extracts each digit from the supplied number using a while loop. After moving the current digits to the left by multiplying them by 10, the extracted digit is added to the reversed number variable. The function then outputs the number that has been inverted.

The user is requested to input a number in the primary function. The reverse number function is called with the input number as an argument after the input has been read using scanf. The inverted number is then displayed using printf to the console.

Example of reverse number program in c

#include <stdio.h>

int reverseNumber(int number) {
    int reversedNumber = 0;
    
    while (number != 0) {
        int remainder = number % 10;
        reversedNumber = reversedNumber * 10 + remainder;
        number /= 10;
    }
   
    return reversedNumber;
}
int main() {
    int number;
    
    printf("Enter a number: ");
    scanf("%d", &number);
   
    int reversed = reverseNumber(number);

    printf("Reversed number: %d\n", reversed);
  
    return 0;
}

Java program reverses the number

ReverseNumber is a class that is defined in this programme, and it has two methods: reverseNumber and main.

Reverses number program in Java?

The reversed form of the number is returned by the reverse number method, which accepts an integer as input. Starting with the rightmost digit, it extracts each digit from the supplied number using a while loop. The extracted digit is added to the reversed number variable after the current digits have been multiplied by 10 to move them to the left. The method then outputs the inverted number.

Example of reverses number program in Java

import java.util.Scanner;

public class ReverseNumber {
    public static int reverseNumber(int number) {
        int reversedNumber = 0;

        while (number != 0) {
            int remainder = number % 10;
            reversedNumber = reversedNumber * 10 + remainder;
            number /= 10;
        }

        return reversedNumber;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int number = scanner.nextInt();

        int reversed = reverseNumber(number);

        System.out.println("Reversed number: " + reversed);
    }
}

An instance of the Scanner class is created in the main function in order to read user input. A number is requested from the user and then saved in the number variable. The input number is passed as a parameter to the reverse number method, and the resulting reversed number is saved in the reversed variable. Finally, System.out.println is used to print the inverted number to the console.

For the programme to run properly, make sure to import the Scanner class at the top of your Java code.

.