Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Sunday, July 10, 2022

C program to input four digits and display them in reverse order


C program to input four digits and display them in reverse order




Introduction:

This article will look at a C program to input four digits and display them in reverse order. In this program, you will learn the fundamentals of user input, data manipulation, and output in the C programming language.

Recognizing the Problem:

Before we get into the logic of the program, let's define the problem statement. We must write a C program that prompts the user to enter four digits before displaying them in reverse order. If the user enters 1234, the program should return 4321.

Steps to reverse the number:


  • The first step is to define the variables that will be required. We'll need a variable to hold the input number and another to hold the reversed number.
  • The next step is to obtain the user's input number. We can accomplish this with the scanf() function.
  • The following step is to reverse the number. We can accomplish this with a loop. We will divide the number by ten in the loop and store the remainder in the reversedNumber variable.
  • The final step is to print the flipped number. We can accomplish this with the printf() function.


Complete Code:


#include <stdio.h>
int main() {
int number, reversedNumber;
printf("Enter a four-digit number: ");
scanf("%d", &number);
while (number > 0) {
reversedNumber = reversedNumber * 10 + number % 10;
number = number / 10;
}
printf("The reversed number is: %d\n", reversedNumber);
return 0;
}


OR


#include<stdio.h>
int main()
{
int num,first,second,third,fourth,reverse;
printf("\nInput 4 Digited Number:- ");
scanf("%d",&num);
first=num%10;
num=num/10;
second=num%10;
num=num/10;
third=num%10;
num=num/10;
fourth=num%10;
num=num/10;
reverse=first*1000+second*100+third*10+fourth*1;
printf("\n Reversed Number = %d", reverse);
}


Output:





Conclusion:

In this article, we looked at a C program to input four digits and display them in reverse order. We were able to achieve the desired result more efficiently by breaking the problem down into smaller steps. Remember to experiment with various inputs to further solidify your understanding of the program.


No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages