In the below program we are going to take a number as input from the keyboard. Then we are going to find the sum of individual digits of that number and also find the reverse of that number. In this process we are also going to check whether that number is palindrome or not.
Example:
Input Number : 8954
Sum of digits of number : 26
Reverse of the number : 4598
The number is not Palindrome.
Reverse of the number : 4598
The number is not Palindrome.
Description:
n – Input number.
rem – Remainder.
s – Sum of individual digits of input number.
rev – Reverse of input number.
original – Copy of input number.
while loop is used to find the sum and reverse.
rem – Remainder.
s – Sum of individual digits of input number.
rev – Reverse of input number.
original – Copy of input number.
while loop is used to find the sum and reverse.
Input : Input a number from keyboard.
Output : Sum, Reverse and whether the number is palindrome or not.
Output : Sum, Reverse and whether the number is palindrome or not.
Algorithm to find the Sum Reverse and check for Palindrome of a number using functions.
STEP 1: START
STEP 2: READ a number
READ n
STEP 3: CALL the sum() function to find the sum of digits
PRINT the sum of digits
STEP 4: CALL the reverse() function to find the reverse of number
PRINT the reverse number
STEP 5: CALL the palindrome() function to check whether the number is palindrome or not
FIND the reverse of number
COMPARE original and reverse number
if original = reverse number
PRINT the number is palindrome
Otherwise
PRINT the number is not palindrome
STEP 6: END
STEP 2: READ a number
READ n
STEP 3: CALL the sum() function to find the sum of digits
PRINT the sum of digits
STEP 4: CALL the reverse() function to find the reverse of number
PRINT the reverse number
STEP 5: CALL the palindrome() function to check whether the number is palindrome or not
FIND the reverse of number
COMPARE original and reverse number
if original = reverse number
PRINT the number is palindrome
Otherwise
PRINT the number is not palindrome
STEP 6: END
Example C program to find the Sum Reverse and check for Palindrome of a number using functions.
#include<stdio.h>
#include<conio.h>
void sum(int n)
{
int rem,s=0;
while(n > 0)
{
rem=n%10;
s=s+rem;
n=n/10;
}
printf("\nSum of digits = %d",s);
}
void reverse(int n)
{
int rem,rev=0;
while(n > 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n =n/10;
}
printf("\nReversed number is = %d", rev);
}
void palindrome(int n)
{
int original,rem,rev=0;
original=n;
while (n > 0) {
rem = n % 10;
rev = rev * 10 + rem;
n =n/10;
}
if(original==rev)
printf("\nThe number is palindrome");
else
printf("\nThe number is not palindrome");
}
int main() {
int n;
printf("Enter an integer number: ");
scanf("%d", &n);
sum(n);
reverse(n);
palindrome(n);
getch();
return 0;
}
#include<conio.h>
void sum(int n)
{
int rem,s=0;
while(n > 0)
{
rem=n%10;
s=s+rem;
n=n/10;
}
printf("\nSum of digits = %d",s);
}
void reverse(int n)
{
int rem,rev=0;
while(n > 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n =n/10;
}
printf("\nReversed number is = %d", rev);
}
void palindrome(int n)
{
int original,rem,rev=0;
original=n;
while (n > 0) {
rem = n % 10;
rev = rev * 10 + rem;
n =n/10;
}
if(original==rev)
printf("\nThe number is palindrome");
else
printf("\nThe number is not palindrome");
}
int main() {
int n;
printf("Enter an integer number: ");
scanf("%d", &n);
sum(n);
reverse(n);
palindrome(n);
getch();
return 0;
}
Output 1:
Enter an integer number: 1234
Sum of digits = 10
Reversed number is = 4321
The number is not palindrome
Sum of digits = 10
Reversed number is = 4321
The number is not palindrome
Output 2:
Enter an integer number: 23432
Sum of digits = 14
Reversed number is = 23432
The number is palindrome
Sum of digits = 14
Reversed number is = 23432
The number is palindrome
QUIZ:
1] while loop is
- Exit control loop.
- Entry control loop.
- Both entry and exit control loop.
- None.
2] Which of the following is exit control loop?
- for loop.
- while loop.
- do while loop.
- all.
3] % * / are ________ type of operators.
- Logical operators.
- Conditional operator.
- Arithmetic operators.
- Relational operators.