c program to check whether a number is palindrome or not using function

Palindrome number:

palindrome number can also be definded as a number which is equal to number after reversing its digits.
Example: for e.g. input is 12121, and after reversal of its digits, input number will be 12121.

Logic:

1.Input a number
2.Reverse its digits
3.match reversed number against input number

#include<stdio.h>
#include<conio.h>
int ispalindrome(int num);
int reverse(int n);
int rev=0,rem=0; 
void main()
{
  int num;
  printf("n Enter a number: ");
  scanf("%d",&num);
  if(ispalindrome(num))
  {
    printf("n %d is palindrome number",num); 
  }
  else
  {
    printf("n %d is not palindrome number,"num); 
  } 
 getch();
}

int ispalindrome(int num)
{
  if(num==reverse(num))
  {
     return 1;
  }
  else
  {
    return 0; 
  }
}

int reverse(int n)
{
  if(n)
  {
    rem=n%10;
    rev=rev*10+rem;
    reverse(n/10);
  } 
  return rev;
}

Ouput:

Enter a number: 10201
10201 is palindrome number

c program to find reverse of a number using recursion

#include<stdio.h>
#include<conio.h>
int reverse(int num); 
void main()
{
   int num, r=0;
   printf("n Enter a number: ");
   scanf("%d",&num);
   r = reverse(num);
   printf("n Reverse number = %d",r);
   getch();

} 
int rev = 0, rem = 0;  
int reverse(int num)
{
   if(num)   {
     rem=num%10;
     rev=rev*10+rem;
     reverse(num/10);
   }
   return rev;
} 

Ouput:

Enter a number: 371
Reverse number = 173

 

c program to reverse a number using for loop

Program

C Program to reverse digits of a number using for loop

#include
#include
void main()
{
  int val;
  int rever=0, remain=0;
  printf("\n Enter a number: ");
  scanf("%d",&val);
  for(;val>0;val=val/10)
  {
    remain = val%10;
    rever = rever*10+remain;
  }

  printf("\n reverse of number is = %d",rever); 
  getch();
}  

Output

Enter a number: 132
reverse of number is = 231

 

c program to reverse a number using while loop

Program

Program to reverse digits of a number using while loop in c

#include<stdio.h>
#include<conio.h>
void main()
{
  int num;
  int rev=0, rem=0;
  printf("n Enter a number: ");
  scanf("%d",&num);
  while(num>0)
  {
    rem = num%10;
    rev = rev*10+rem;
    num = num/10;
  }
  printf("n reverse number = %d",rev);
  getch();
} 

Output

Enter a number: 123
Reverse number = 321

c program to reverse a number using do while loop

Program

Program in C for reversal of digits of a number using do while loop

#include
#include
void main()
{
  int val;
  int rever=0, remain=0;
  printf("\nEnter a number: ");
  scanf("\n%d",&val);
  do
  {
    remain = val%10;
    rever = rever*10+remain;
    val = val/10;
}while(val>0);

printf("\nreverse of the number = %d",rever);
getch();
}

Output

Enter a number: 254
reverse of the number = 452

c program to find sum of squares of even numbers

Program

C Program to find and print sum of square of even numbers

#include<stdio.h>
#include<conio.h>
void main()
{
   int num, sum=0;
   for(num=1; num<=10;num++)
   {
     if(num%2==0)
        sum=sum+num*num; 
   }
   printf("n Sum of square of first 10 even numbers: %d",sum);
   
   getch();
}

Output

sum of first 10 even numbers: 220

Explanation

  • This program calculates sum of those numbers which produces no remainder after dividing by 2 i.e.
    even numbers

  • Above program performs addition of square of first 10 even numbers i.e. sum of squares of 2,4,6,8,10

c program to check whether given number is armstrong number or not using for loop

#include<stdio.h>
#include<conio.h>
void main()
{
  int num, n, rem;
  int cube=0, sum=0;
  printf("n Enter a number: ");
  scanf("%d",&num);
  n = num;
  for(;n>0;n=n/10)
  {
    rem = n%10;
    cube = rem*rem*rem;
    sum = sum + cube;
  }
  if(num==sum)
  {
    printf("n %d is armstrong number",num);
  }
  else
  {
    printf("n %d not armstrong number",num);
  }
  getch();
}

Ouput:

Enter a number: 407
407 is armstrong number

 

C program to check armstrong number using recursive function or recursion

Armstrong number is an integer which has sum of cubes of its individual digits equal to the number itself.

Example: 0,1,153,370,..

logic

  • Seperate each digit of input integer and then calculate its cube.
  • Add the cube of all digits
  • check final sum against input integer

Program

program to check armstrong number using recursive function or recursion

#include<stdio.h>
#include<conio.h>
int armstrong(int num);
void main()
{
   int num, n;
   printf("n Enter a number: ");
   scanf("%d", &num);
   n = armstrong(num);
   printf("n %d is armstrong number",n);
   getch();
}
int armstrong(int num)
{ 
    int s = 0;
    if(num)
    {
      s = ((num%10)*(num%10)*(num%10)) + armstrong(num/10);
    }
    return s;
}

Output


input = 407
output = (4*4*4) + (0*0*0) + (7*7*7)
input  number equals sum of cube of all its digits.
So 407 is armstrong number

C program to find and print armstrong numbers between 1 and 1000

Armstrong numbers:

Input : 153.
Calculating armstrong number: 153 =(1*1*1)+(5*5*5)+(3*3*3)= 1+125+27 =153 

If input equals output, 153==153, 153 is armstrong number.

#include<stdio.h>
#include<conio.h>
void main()
{
  int num, n, rem;
  int cube=0, sum; 
  printf("n Armstrong numbers between 1 and 1000 are: n");
  for(num=1;num<=1000;num++)
  {
    sum=0;
    n=num;
    while(n>0)
    {
      rem=n%10;
      cube=rem*rem*rem;
      sum=sum+cube;
      n=n/10;
    }
    if(num==sum)
    {
      printf("n%d is armstrong number",num);    
    }
  getch();
}

Ouput:

Armstrong numbers between 1 and 1000 are:
1 is armstrong number
153 is armstrong number
370 is armstrong number
371 is armstrong number
407 is armstrong number

 

error: Content is protected !!