On this article, you are going to learn how to write a C program to opposite a host. Reversing a host in C programming way converting all of the digits of a host to convey the digit on the closing place to the primary place and vice-versa. Assume you’re taking the quantity 1234; the opposite is 4321. There are a couple of techniques to put in writing a C program to opposite a host, and we will be able to be taking a look at they all on this article. However sooner than delving deep into writing this system itself, let’s first perceive the set of rules and good judgment at the back of it.
Figuring out the Set of rules of a C Program to Opposite a Quantity
In actual existence, we will be able to merely turn the quantity to search out its opposite. However how can we construct the good judgment to make that occur via a program? Let’s in finding out.
- We will be able to claim two int variables: ‘Quantity’ retailer the quantity to be reversed, and ‘Reversed_Number,’ initialized to be 0, will cling the ensuing reversed quantity.
- Now, we will be able to use the components Reversed_Number = Reversed_Number*10 + Numberp.c10.
- This components divides the quantity by way of 10 and retail outlets the rest multiplied by way of 10 as the price of the ‘Reversed_Number.’
- After making use of the components, we will be able to divide the quantity by way of 10 and replace its worth to get rid of the closing digit this is already reversed.
- We will be able to repeat this till the price of the quantity turns into not up to 0.
- When the quantity is going underneath 0, the ‘Reversed_Number’ variable may have the end result.
Flowchart of C Program to Opposite a Quantity
The set of rules and good judgment discussed within the earlier phase may also be demonstrated the use of the next flowchart:
The use of the above flowchart, we will be able to create a C program to opposite a host the use of more than a few strategies. Now we will be able to take a look at the ones a couple of strategies and opposite a host.
Our Unfastened Classes with Certificates
C Program to Opposite a Quantity The use of Whilst Loop
The primary manner that we will be able to use to opposite a host in C is thru some time loop. We will be able to create and use the whilst loop to proceed processing the components till the price of the quantity turns into 0. Right here’s the implementation of this system the use of the whilst loop.
Instance
#come with <stdio.h>
int primary(){
int Num, rev_Num = 0, the rest;
printf(“Input the quantity to opposite: “);
scanf(“%d”, &Num);
whilst (Num != 0){
the rest = Num % 10;
rev_Num = rev_Num * 10 + the rest;
Num = Num/10;
}
printf(“The reversed quantity is: %d”, rev_Num);
go back 0;
}
Output:
C Program to Opposite a Quantity The use of Recursion
You’ll additionally write a C program to opposite a host the use of recursion. The flowchart and set of rules stay the similar. On the other hand, because of recursion, the serve as will name itself till the situation turns into false. Therefore, we gained’t have to make use of any loop on this instance.
Instance
#come with<stdio.h>
int primary(){
int Num,rev_Num;
printf(“Input the quantity to opposite: “);
scanf(“%d”,&Num);
//Calling our serve as that may repeat itself
rev_Num=rev_Func(Num);
printf(“nThe reversed quantity is :%d”,rev_Num);
go back 0;
}
int sum=0,the rest;
rev_Func(int Num){
if(Num){
the rest=Nump.c10;
sum=sum*10+the rest;
rev_Func(Num/10);
}
else
go back sum;
go back sum;
}
Output:
C Program to Opposite a Quantity The use of For Loop
For this case, as a substitute of the use of some time loop, we will be able to use a for loop in C to execute the components till the situation turns into false. The next code demonstrates a C program to opposite a host the use of a for loop.
Instance
#come with<stdio.h>
void primary(){
int Num,rev_Num=0,the rest,a;
printf(“Input the quantity to opposite: “);
scanf(“%d”,&Num);
a=Num;
for(;Num>0;){
the rest=Nump.c10;
rev_Num=rev_Num*10+the rest;
Num=Num/10;
}
printf(“Opposite of %d is %d”,a,rev_Num);
}
Output:
C Program to Opposite a Quantity The use of Serve as
Right here, we will be able to create a serve as in the principle manner and use the set of rules within it to calculate the opposite of the quantity entered by way of the consumer.
Instance
#come with <stdio.h>
int rev_Int(int);
int primary(){
int Num, Rev = 0;
printf(“nEnter the quantity to opposite: “);
scanf(“%d”, &Num);
supply: www.simplilearn.com