0% found this document useful (0 votes)
24 views4 pages

C Programs

Uploaded by

agnes joshy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

C Programs

Uploaded by

agnes joshy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Reverse an Integer

#include <stdio.h>
int main()
{
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}

Output

Enter an integer: 2345


Reversed number = 5432
Check whether a number is palindrome

#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;

// reversed integer is stored in reversed variable


while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}

// palindrome if original and reversed are equal


if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);

return 0;
}
Program to Count the Number of Digits

#include <stdio.h>
int main() {
long long n;
int count = 0;
printf("Enter an integer: ");
scanf("%lld", &n);
// iterate at least once, then until n becomes 0
// remove last digit from n in each iteration
// increase count by 1 in each iteration
do {
n /= 10;
++count;
} while (n != 0);

printf("Number of digits: %d", count);


}

Output

Enter an integer: 3452


Number of digits: 4
The integer entered by the user is stored in variable n. Then the do...while loop is iterated

until the test expression n! = 0 is evaluated to 0 (false).

 After the first iteration, the value of n will be 345 and the count is incremented to 1.

 After the second iteration, the value of n will be 34 and the count is incremented to 2.

 After the third iteration, the value of n will be 3 and the count is incremented to 3.

 After the fourth iteration, the value of n will be 0 and the count is incremented to 4.

 Then the test expression of the loop is evaluated to false and the loop terminates.

You might also like