Skip to main content

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 23


Write a C program to find the sum of individual digits of a positive integer and test given number is palindrome.

(Palindrome number is such number which when reversed is equal to the original number. For example: 121, 12321, 1001 etc.)

#include <stdio.h>

void main()

{

int n, temp, r=0,sum=0, rev = 0; 

clrscr();

printf("\n Enter an integer: \n"); 

scanf("%d", &n);

/* original number is stored at temp */

temp = n; 

while (n > 0)

{

r = n % 10;

sum=sum+r; // sum of Individual Numbers 

rev = rev * 10 + r;

n = n/10;

}

printf("\n Given number is = %d\n", temp);

printf(ā€œ\n Sum of individual digits of a given number is:%dā€, sum); 

printf("\n Its reverse is = %d\n", rev);

if (temp == rev)

printf("\n %d Number is a palindrome \n", temp); 

else

printf("\n %d is Number is not a palindrome \n", temp); 

getch();

}


OUTPUT:

Enter an integer: 
545
Given number is = 545

 Sum of individual digits of a given number is:14
 Its reverse is = 545

 545 Number is a palindrome 

Comments

Popular posts from this blog

Learning and Development Interview Questions and answers for Mathematics-1.

  1). What is Mean, Mode and Median? Solution Answer: The mean is the average of a  collection of numbers or terms in a sequence. To calculate the mean use a formula is sum of total terms divided  by number of terms. The mode  is the most f requent number or term in a sequence. It means the number that occurred  highest number of times  in a sequence. To find the mode arrange the numbers in ascending or descending order and verify which number repeated most number of times in a sorted sequence. The median is the middle number/term where the sequence is arranged in ascending or descending order. If the sorted sequence have odd number of terms then  divide by 2 and round up to get the position of the median number.  If the  sorted sequence  have  even  number of terms then  divide by 2  to get the position of the median number.  2 ). What is the Difference between Fractional and Rational number? Solu...