In the previous blog we saw whether the student has passed or failed in the exam. Now, we will see whether the given number is palindrome or not .
Conditional Statements:-
If condition:-
The if statement is used to examine a given condition and perform actions based on whether or not that condition is true. It's most commonly utilized in scenarios where we need to perform many operations for various situations. The if statement's syntax is seen below.
if(expression)
{
}
If-Else Condition:-
For a single condition, the if-else statement is used to conduct two operations. It's an enhancement to the if statement that allows us to conduct two different operations, one for the condition's correctness and the other for the condition's incorrectness. It is important to note that the if and else blocks cannot be run at the same time. The syntax is seen below.
if(expression)
{
}
else
{
}
To check whether the given number is palindrome or not
Example:-
We, can clearly see from the image that the given number and its reversed number are equal.
Code:-
#include <stdio.h>
int main()
{
int number,reversednumber=0,value;
printf("Enter the number");
scanf("%d",&number);
value=number;
while(number>0)
{
reversednumber=reversednumber*10+number%10;
number=number/10;
}
if(value == reversednumber)
{
printf("The given number is pallindrome");
}
else
{
printf("The given number is not a pallindrome");
}
}
Algorithm:-
1.Start
2.Declare the variable number,reversednumber as int datatype and the reversednumber as 0 .
3.Use the format specifier "%d" to get the number
4.Use a while loop and run the loop until number is > 0 .
5.Inside the while loop we have to multiply the reversednumber * 10 and add it with the last digit of the number .
6.The remove the last digit in the number .
Explanation:-We do the steps 5 & 6 because if the number is 1000 the reversed number should be 1 .
1.Here last digit is 0
reversednumber=0*10+0=0
number=1000/10=100
2.reversednumber=0*10+0=0
number=1000/10=10
3.reversednumber=0*10+0=0
number=10/10=1
4.reversednumber=0*10+1=1
number=1/10=0
Loop stops.
7.We have initialized the value of the number to a variable called value.
8.If the value and reversed number are equal then the given number is palindrome and we can print the statement.
9.Else its not a palindrome and we can print that statement
10.Stop
Screenshot:-
1.Program Screenshot:-
2.Code Screenshot:-
In the next blog, we will see how to find the smallest number from the given 3 numbers.
0 Comments