Showing posts with label whether. Show all posts
Showing posts with label whether. Show all posts

C and C++ Programs to Check Whether a Number is Strong Number or Not

This post contains C and C++ program to check whether a given number is Strong number or not. A strong number is a number for which the sum of factorials of its digits is equal to the number itself. The first one is C program to check whether the input number is a strong number or not. An example is 145. 1!+4!+5!=1+24+120=145 Therefore 145 is a strong number.

C Program:

#include<stdio.h>
long int factorial(int n)

{

C and C++ Program to Check Whether a Given Number is Even or Odd

C and C++ programs to check whether a given number is Odd or even

C Program

#include<stdio.h>
void main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( n%2 == 0 )
printf("Even");
else
printf("Odd");
}

C++ Program

#include<iostream.h>
void main()
{
int n;
cout<<"Enter an integer\n";
cin>>n;
if ( n%2 == 0 )
cout<<"\nEven";
else
cout <<"\nOdd";
}


C and C++ Program to Check Whether a Given Year is Leap Year or Not

This is post contains a C program and a C++ program to check whether an input year is leap year or not. A year is a leap year if:

  1. The year is divisible by 400
  2. The year is divisible by 4 but not divisible by 100.
Any other year is not a leap year.

C program
#include <stdio.h>
void main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if(year%400==0 || ((year%4=0) && (year%100!=0)))
printf("%d is a leap year.\n", year); else
printf("%d is not a leap year.\n", year);
}


C++ program
#include <iostream.h>
void main()
{
int year;
cout<<"Enter a year to check if it is a leap year or not";
cin>>year;
if(year%400==0 || ((year%4=0) && (year%100!=0)))
cout<<"\n"<<year<<"is a leap year."; else
cout<<"\n"<<year<<"is NOT a leap year."; 
}

C and C++ Programs to Check Whether a Given Number is Armstrong Number or Not

This post contains two programs, a C++ program and a C program. These are programs to check whether a given number is an Armstrong number or not. An Armstrong number is a positive integer for which the sum of 'n'th power of all of its digits is equal to the number itself. For example, 153 is a 3 digit Armstrong number because 13+53+33=153.

C ++ program:

# include <iostream.h>

# include <conio.h>

# include <math.h>

void main ()

{

clrscr();

int a,b=0,sum=0;

long int n;

cout<<"Enter the number to be checked\n ";

cin>>n;

if(n<1)
{
cout<<"\nThe number should be greater than 0";
}
else
{
a=n;
//counting the digits
while (a>0) 
{
a=a/10; 
b++;
}
a=n;
//adding up bth power of digits
while(a>0)
{
sum=sum+pow(( a%10) ,b);
a=a/10;
}
if(sum==n)
cout<<"\nThe number is an ARMSTRONG number";
else
cout<<"\nThe number is NOT an ARMSTRONG number";
}
getch();
}


C Program:
# include <stdio.h>

# include <conio.h>

# include <math.h>

void main ()

{

clrscr();

int a,b=0,sum=0;

long int n;

printf("Enter the number to check\n ");

scanf("%i",&n);
if(n<1)
{
printf ("\nThe number should be greater than 0");
}
else
{
a=n;
//counting the digits
while (a>0) 
{
a=a/10; 
b++;
}
a=n;
//adding up bth power of digits
while(a>0)
{
sum=sum+pow(( a%10) ,b);
a=a/10;
}
if(sum==n)
printf ("\nThe number is an ARMSTRONG number");
else
printf ("\nThe number is NOT an ARMSTRONG number");
}
getch();
} 

C Program to Check Whether a Given String is Palindrome or Not

Here i am writing two different programs two check whether an input string is palindrome or not. The first program checks the string manually character by chatter from both ends. The other program make use of a string function strrev() (under string.h) to reverse the string. Therefore, the second program is more compact.

Program 1:

#include<stdio.h>

#include<string.h>

void main()

{

char a[20];

int i, len;

printf("\nEnter the string:\n");

gets(a);

len = strlen(a);

for (i = 0; i < len / 2; i++)

{

if (a[i] != a[len - i - 1])

break;

}