C Program to Check Whether a Number is Even or Odd
In this example, if...else statement is used to check whether a number entered by the user is even or odd.
An even number is an integer that is exactly divisible by 2. Example: 0, 8, -24An odd number is an integer that is not exactly divisible by 2. Example: 1, 7, -11, 15
Example #1: Program to Check Even or Odd
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the number is perfectly divisible by 2
if(number % 2 == 0)
printf("%d is even.", number);
else
printf("%d is odd.", number);
return 0;
}
Output
Enter an integer: -7 -7 is odd.
In the program, integer entered by the user is stored in variable number.
Then, whether the number is perfectly divisible by 2 or not is checked using modulus operator.If the number is perfectly divisible by 2, test expression
number%2 == 0
evaluates to 1 (true) and the number is even.However, if the test expression evaluates to 0 (false), the number is odd.Example #2: Program to Check Odd or Even Using Conditional Operater
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
(number % 2 == 0) ? printf("%d is even.", number) : printf("%d is odd.", number);
return 0;
}
C Program to Count Number of Digits in an Integer
Example to count the number of digits in an integer entered by the user. The while loop is used to solve this program.
This program takes an integer from the user and calculates the number of digits. For example: If the user enters 2319, the output of the program will be 4.Example #1: Program to Count Number of Digits in an Integer
#include <stdio.h>int main(){long long n;int count = 0;printf("Enter an integer: ");scanf("%lld", &n);while(n != 0){// n = n/10n /= 10;++count;}printf("Number of digits: %d", count);}OutputEnter an integer: 3452 Number of digits: 4The integer entered by the user is stored in variable n. Then the while loop is iterated until the test expressionn != 0
is evaluated to 0 (false).
- After first iteration, the value of n will be 345 and the count is incremented to 1.
- After second iteration, the value of n will be 34 and the count is incremented to 2.
- After third iteration, the value of n will be 3 and the count is incremented to 3.
- After fourth iteration, the value of n will be 0 and the count is incremented to 4.
- Then the test expression is evaluated to false and the loop terminates.
C Program to Check Armstrong Number
Example to check whether an integer (entered by the user) is an Armstrong number or not using while loop and if...else statement.To understand this example, you should have the knowledge of following C programmingtopics:A positive integer is called an Armstrong number of order n ifabcd... = an + bn + cn + dn + ...In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example:153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.Example #1: Check Armstrong Number of three digits
#include <stdio.h>
int main()
{
int number, originalNumber, remainder, result = 0;
printf("Enter a three digit integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}
if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);
return 0;
}
OutputEnter a three digit integer: 371 371 is an Armstrong number.Example #2: Check Armstrong Number of n digits
#include <stdio.h>
#include <math.h>
int main()
{
int number, originalNumber, remainder, result = 0, n = 0 ;
printf("Enter an integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
{
originalNumber /= 10;
++n;
}
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += pow(remainder, n);
originalNumber /= 10;
}
if(result == number)
printf("%d is an Armstrong number.", number);
else
printf("%d is not an Armstrong number.", number);
return 0;
}
OutputEnter an integer: 1634 1634 is an Armstrong number.In this program, the number of digits of an integer is calculated first and stored in nvariable. And, the pow() function is used to compute the power of individual digits in each iteration of the while loop.C Program to Check Whether a Number is Palindrome or Not
This program reverses an integer (entered by the user) using while loop. Then, if statement is used to check whether the reversed number is equal to the original number or not.To understand this example, you should have the knowledge of following C programmingtopics:An integer is a palindrome if the reverse of that number is equal to the original number.Example: Program to Check Palindrome
#include <stdio.h>
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
printf("Enter an integer: ");
scanf("%d", &n);
originalInteger = n;
// reversed integer is stored in variable
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}
// palindrome if orignalInteger and reversedInteger are equal
if (originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%d is not a palindrome.", originalInteger);
return 0;
}
OutputEnter an integer: 1001 1001 is a palindrome.
C Program to Compute Quotient and Remainder
This program evaluates the quotient and remainder when an integer is divided by another integer.
Program to Compute Quotient and Remainder
printf("Enter divisor: ");
scanf("%d", &divisor);
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
return 0;
}
OutputEnter dividend: 25 Enter divisor: 4 Quotient = 6 Remainder = 1In this program, user is asked to enter two integers (dividend and divisor) which is stored in variable dividend and divisor respectively.Then the quotient is evaluated using division / operator and stored in variable quotient.Similarly, the remainder is evaluated using modulus % operator and stored in remaindervariable.
Comments
Post a Comment