A Learning Platform

SUCCESS

Success = Discipline + Consistency + Sacrifice.
Discipline - in what you do.
Consistency - in what you do.
Sacrifice - for what you do.

                   -- Manjunatha C. P. --

Sum, Reverse and Palindrome of a number - C program

    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.

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.

Input : Input a number from keyboard.
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

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;
}

Output 1:

Enter an integer number: 1234
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


QUIZ:

1] while loop is

  1. Exit control loop.
  2. Entry control loop.
  3. Both entry and exit control loop.
  4. None.

2] Which of the following is exit control loop?

  1. for loop.
  2. while loop.
  3. do while loop.
  4. all.

3] % * / are ________ type of operators.

  1. Logical operators.
  2. Conditional operator.
  3. Arithmetic operators.
  4. Relational operators.