Skip to main content

Check the inpute NUMBER is(even,odd,prime,armstrong,palindrom......)

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.

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.

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/10
        n /= 10;
        ++count;
    }
    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 while loop is iterated until the test expression n != 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.
Armstrong number
To understand this example, you should have the knowledge of following C programmingtopics:
A positive integer is called an Armstrong number of order n if
abcd... = 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

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int number, originalNumber, remainder, result = 0;
  5. printf("Enter a three digit integer: ");
  6. scanf("%d", &number);
  7. originalNumber = number;
  8. while (originalNumber != 0)
  9. {
  10. remainder = originalNumber%10;
  11. result += remainder*remainder*remainder;
  12. originalNumber /= 10;
  13. }
  14. if(result == number)
  15. printf("%d is an Armstrong number.",number);
  16. else
  17. printf("%d is not an Armstrong number.",number);
  18. return 0;
  19. }
Output
Enter a three digit integer: 371
371 is an Armstrong number.

Example #2: Check Armstrong Number of n digits

  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5. int number, originalNumber, remainder, result = 0, n = 0 ;
  6. printf("Enter an integer: ");
  7. scanf("%d", &number);
  8. originalNumber = number;
  9. while (originalNumber != 0)
  10. {
  11. originalNumber /= 10;
  12. ++n;
  13. }
  14. originalNumber = number;
  15. while (originalNumber != 0)
  16. {
  17. remainder = originalNumber%10;
  18. result += pow(remainder, n);
  19. originalNumber /= 10;
  20. }
  21. if(result == number)
  22. printf("%d is an Armstrong number.", number);
  23. else
  24. printf("%d is not an Armstrong number.", number);
  25. return 0;
  26. }
Output
Enter 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.
Palindrome Number
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

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int n, reversedInteger = 0, remainder, originalInteger;
  5. printf("Enter an integer: ");
  6. scanf("%d", &n);
  7. originalInteger = n;
  8. // reversed integer is stored in variable
  9. while( n!=0 )
  10. {
  11. remainder = n%10;
  12. reversedInteger = reversedInteger*10 + remainder;
  13. n /= 10;
  14. }
  15. // palindrome if orignalInteger and reversedInteger are equal
  16. if (originalInteger == reversedInteger)
  17. printf("%d is a palindrome.", originalInteger);
  18. else
  19. printf("%d is not a palindrome.", originalInteger);
  20. return 0;
  21. }
Output
Enter 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

  1. printf("Enter divisor: ");
  2. scanf("%d", &divisor);
  3. // Computes quotient
  4. quotient = dividend / divisor;
  5. // Computes remainder
  6. remainder = dividend % divisor;
  7. printf("Quotient = %d\n", quotient);
  8. printf("Remainder = %d", remainder);
  9. return 0;
  10. }
Output
Enter dividend: 25 Enter divisor: 4 Quotient = 6 Remainder = 1
In 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.
Finally, the quotient and remainder are displayed using printf() function.
================================================================
#include <stdio.h>
int main(){
    int dividend, divisor, quotient, remainder;
    printf("Enter dividend: ");
    scanf("%d", &dividend);

Comments

Popular posts from this blog

short key of c programming

short key of c programming Shortcut Keys(Hot Keys)     Operation    Alt + F  -            Open file Alt + F3 -           Close Alt + F + N  -    New file Alt + X  -           Exit turbo c Alt + F5 -          Output Screen Alt + F + L  -    Check programme load or not. Alt + ENTER  - Full screen Shift + Del -       Cut Shift + Insert -   Paste Ctr + Insert -    Copy Alt+F9 -           Compile a program Ctr + F8           Bbreak point Ctrl+F9 -          To run a program Ctrl+s -...

C LANGUAGE - OVERVIEW C - VARIABLES

C LANGUAGE - OVERVIEW Advertisements C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in C. C has now become a widely used professional language for various reasons − Easy to learn Structured language It produces efficient programs It can handle low-level activities It can be compiled on a variety of computer platforms Facts about C C was invented to write an operating system called UNIX. C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute  A N S I A N S ...

environment

It looks like you meant "environment." The environment refers to the surroundings or conditions in which a person, animal, or plant lives or operates. It encompasses various elements, including natural resources, ecosystems, climate, and human-made structures. Protecting and preserving the environment is crucial for the well-being of all living organisms on Earth. Is there anything specific you'd like to discuss or learn about regarding the environment?