Hello Guys in this post we will be solving a program in c to interchange the unit digits of two numbers.
234 456
output:
236 454
Understanding the Program Structure:
In this program we need to get two input from the console, which is of type int seperated by Space.
The output should be the same format with the interchanged unit digits of two numbers with space in between.
Key Concept of the Program:
Unit Digit: Unit digit is the last digit of any number, It is the digit on the One's Place of a number. In program we obtain the unit digit by using modulo operator.
Procedure:
First we are getting two inputs from the user which are a and b, which stores the two integer values using "scanf" function.
scanf("%d %d",&a,&b);
Next there may be two conditions for our program, the first one is what if the numbers are less than 10?
So we check them by the if condition,
if(a<10 && b<10){
printf("%d %d",b,a);}
Here we are checking whether the numbers are less then 10,if they are less then we are just printing the numbers in the other order.
Next logic will be for the numbers greater than 10,so if they are greater than 10,we will be finding their unit digits by the following ways,
n%10- gives the last number
n%100 - gives the last before number and so on.
a%10-unit digit of a
b%10-unit digit of b
Then we are printing the number except the unit digit. This can be done by dividing the number by 10.
n/10 - gives value except last number
a/10 - gives value except last number of a
b/10 - gives value except last number of b
else{
printf("%d%d %d%d",a/10,b%10,b/10,a%10);
}
Program:
int main(){
int a,b;
scanf("%d %d",&a,&b);
if(a<10 && b<10){
printf("%d %d",b,a);}
else{
printf("%d%d %d%d",a/10,b%10,b/10,a%10);}
}
Hope you understood the program. If any queries please reach out to me or just comment in this post .
No comments:
Post a Comment