Chapter:

C-programs-on-various-integers

1. C Program to Print Armstrong Number from 1 to 1000

This is a C Program to print armstrong number from 1 to 1000.

Problem Description

This C Program print armstrong number from 1 to 1000.

Problem Solution

An Armstrong number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself. Hence 153 because `1^3 + 5/3 + 3^3 = 1 + 125 + 27 = 153.`

Program/Source Code

/*

 * C Program to Print Armstrong Number from 1 to 1000

 */

#include

 

main()

{

    int number, temp, digit1, digit2, digit3;

 

    printf("Print all Armstrong numbers between 1 and 1000:\n");

    number = 001;

    while (number <= 900)

    {

        digit1 = number - ((number / 10) * 10);

        digit2 = (number / 10) - ((number / 100) * 10);

        digit3 = (number / 100) - ((number / 1000) * 10);

        temp = (digit1 * digit1 * digit1) + (digit2 * digit2 * digit2) + (digit3 * digit3 * digit3);

        if (temp == number)

        {

            printf("\n Armstrong no is:%d", temp);

        }

        number++;

    }

}

Program Explanation

In this C program, we are printing the Armstrong number from 1 to 1000. An Armstrong number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself. Hence, 153 because `1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`. We are initializing the number variable value as 001.

Using while loop checks the condition that the value of number variable is less than or equal to 900. If the condition is true then, execute the iteration of the loop. The digit1 variable is used to compute the division of the value of number variable by 10 and multiply the resulting value by 10 and subtract the multiplied value with the value of number variable.


The digit2 variable is used to compute the division of the value of number variable by 100, multiply the resulting value with 10, divide the value of number variable by 10 and subtract the multiplied value from the divided value.


The digit3 variable is used to compute the division of the value of number variable by 100 and also by 1000, then multiply the value divided by 100 with 10 and subtract both the values. The temp variable is used to multiply the value of digit1, digit2, digit3 variables to the power of 3 respectively. Compute the summation of all the three multiplied values. If condition statement is used to check the value of temp and number variables are equal, if the condition is true print the Armstrong number.

Output:

Print all Armstrong numbers between 1 and 1000:

 

Amstrong no is:1

Amstrong no is:153

Amstrong no is:370

Amstrong no is:371

Amstrong no is:407

Show More

2. C Program to find prime numbers in a given range

Upon execution of below program, the user would be asked to provide the from & to range and then the program would display all the prime numbers in sequential manner for the provided range. Using this program you can find out the prime numbers between 1 to 100, 100 to 999 etc. You just need to input the range, for e.g. if you want the prime numbers from 100 to 999 then enter numbers 100 and 999 when program prompts for input.


Source code:

#include

int main()

{

   int num1, num2, flag_var, i, j;

 

   /* Ask user to input the from/to range

    * like 1 to 100, 10 to 1000 etc.

    */

   printf("Enter two range(input integer numbers only):");

   //Store the range in variables using scanf

   scanf("%d %d", &num1, &num2);

 

   //Display prime numbers for input range

   printf("Prime numbers from %d and %d are:\n", num1, num2);

   for(i=num1+1; i

   {

      flag_var=0;

      for(j=2; j<=i/2; ++j)

      {

         if(i%j==0)

         {

            flag_var=1;

            break;

         }

      }

      if(flag_var==0)

         printf("%d\n",i);

  }

  return 0;

}

Show More

3. C Program to Reverse a Number & Check if it is a Palindrome:

This is a C Program which reverses a number & checks if it is a palindrome or not.

Problem Description

This C program accepts an integer, reverse it and also checks if it is a palindrome or not.

Problem Solution

  1. Take the number which you have to reverse as the input.

  2. Obtain its quotient and remainder.

  3. Multiply the separate variable with 10 and add the obtained remainder to it.

  4. Do step 2 again for the quotient and step 3 for the remainder obtained in step 4.

  5. Repeat the process until quotient becomes zero.

  6. When it becomes zero, check if the reversed number is equal to original number or not.

  7. Print the output and exit.

Program/Source Code

#include

 

void main()

{

    int num, temp, remainder, reverse = 0;

 

    printf("Enter an integer \n");

    scanf("%d", &num);

    /*  original number is stored at temp */

    temp = num;

    while (num > 0)

    {

        remainder = num % 10;

        reverse = reverse * 10 + remainder;

        num /= 10;

    }

    printf("Given number is = %d\n", temp);

    printf("Its reverse is  = %d\n", reverse);

    if (temp == reverse)

        printf("Number is a palindrome \n");

    else

        printf("Number is not a palindrome \n");

}

Program Explanation

  1. Take the number which you have to reverse as the input and store it in the variable num.

  2. Copy the input number to the another variable temp.

  3. Firstly initialize the variable reverse to zero.

  4. Obtain the remainder of the input number.

  5. Multiply the variable reverse with 10 and add the Obtained remainder to it and store the result in the same variable.

  6. Obtain the quotient of the input number and considering this as input number repeat the steps as mentioned above until the obtained quotient becomes zero.

  7.  When it becomes zero, using if,else statement check whether the reversed number is equal to original number or not.

  8.  If it is equal, then print the output as ??Number is a palindrome?, otherwise print the output as ??Number is not a palindrome?.

Output:

Case:1

Enter an integer

6789

Given number is = 6789

Its reverse is  = 9876

Number is not a palindrome

 

Case:2

Enter an integer

58085

Given number is = 58085

Its reverse is  = 58085

Number is a palindrome

Show More

4. C Program to check Leap Year:

This program checks whether the input year is leap year or not.

You can check whether a year is leap or not by using this mathematical logic:

Leap Year:

  • If a year is divisible by 4, 100 and 400 then it is a leap year.

  • If a year is divisible by 4 but not divisible by 100 then it is a leap year

Not a Leap Year:

  1. If a year is not divisible by 4 then it is not a leap year

  2. If a year is divisible by 4 and 100 but not divisible by 400 then it is not a leap year

Source Code:

#include

int main()

{

    int y;


    printf("Enter year: ");

    scanf("%d",&y);


    if(y % 4 == 0)

    {

        //Nested if else

        if( y % 100 == 0)

        {

            if ( y % 400 == 0)

                printf("%d is a Leap Year", y);

            else

                printf("%d is not a Leap Year", y);

        }

        else

            printf("%d is a Leap Year", y );

    }

    else

        printf("%d is not a Leap Year", y);


    return 0;

}


Output:

Enter year: 1991

1991 is not a Leap Year

Show More

5. C Program to Find Sum of two Binary Numbers:

This is a C program to Find the Sum of two Binary Numbers.

Problem Description

This program finds the sum of two binary numbers.

Problem Solution

  •  Take two binary numbers as input.

  •  Add each bits from the two binary numbers separately starting from LSB.

  • The operations may be as follows.

    • (0+0)=0,

    •  (1+0)=1,

    • (1+1)=0 and 1 will be remainder.

Program/Source Code

/*

 * C Program to Find the Sum of two Binary Numbers

 */

#include

 

int main()

{

 

    long binary1, binary2;

    int i = 0, remainder = 0, sum[20];

 

    printf("Enter the first binary number: ");

    scanf("%ld", &binary1);

    printf("Enter the second binary number: ");

    scanf("%ld", &binary2);

    while (binary1 != 0 || binary2 != 0)

    {

        sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;

        remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;

        binary1 = binary1 / 10;

        binary2 = binary2 / 10;

    }

    if (remainder != 0)

        sum[i++] = remainder;

    --i;

    printf("Sum of two binary numbers: ");

    while (i >= 0)

        printf("%d", sum[i--]);

    return 0;

}

Program Explanation

  •  Take two binary numbers as input and store it in the variables binary1 and binary2.

  •  Initialize the variables i and remainder to zero.

  • Obtain the remainders of both the binary numbers.

  • Obtain the quotients of both the binary numbers.

  • Firstly add the remainders of both binary numbers and further add the variable remainder.

  • Obtain the remainder of the result got at step 5 when divided by 2 and store it in the array sum[].

  • Obtain the quotient of the result got at step 5 when divided by 2 and override the variable remainder with this value.

  • Override the variables binary1 and binary2 with their quotient got at step 4.

  • Repeat the steps 3-8 with the new values of binary1 and binary2 until both becomes zero.

  • When it becomes zero check if any remainder exits. If it is, then copy it into the array sum.

  •  Print the sum as output.

Output:

Enter the first binary number: 100000

Enter the second binary number: 101010

Sum of two binary numbers: 1001010

Show More

6. C Program to Check wher a given Number is Perfect Number:

This is a C Program to check whether a given number is perfect number.

Program Description:

This C Program checks whether a given number is perfect number.

Problem Solution

Perfect number is a number which is equal to sum of its divisor. For eg, divisors of 6 are 1,2 and 3. The sum of these divisors is 6. So 6 is called as a perfect number.

Program/Source Code

/*

 * C Program to Check whether a given Number is Perfect Number

 */

#include

 

int main()

{

    int number, rem, sum = 0, i;

 

    printf("Enter a Number\n");

    scanf("%d", &number);

    for (i = 1; i <= (number - 1); i++)

    {

        rem = number % i;

    if (rem == 0)

        {

            sum = sum + i;

        }

    }

    if (sum == number)

        printf("Entered Number is perfect number");

    else

        printf("Entered Number is not a perfect number");

    return 0;

}

Program Explanation

In this C program, we are reading the integer value using number variable. Perfect number is a number which is equal to sum of its divisor. For example, divisors of 6 are 1, 2 and 3. The sum of these divisors is 6. So the number 6 is called as perfect number.

For loop statement is used to assign the modulus of the value of number variable by the value of i variable. If condition statement is used to check the value of rem variable is equal to 0, if the condition is true to execute if condition statement and compute the summation the value of sum variable with the value of i variable.

Another If-else condition statement is used to check that both the value of sum variable and the value of number variable are equal, if the condition is true print the statement as perfect number. Otherwise, execute else condition statement and print the statement as not a perfect number.

Output:

Enter a Number

6

Entered Number is perfect number

 

Enter a Number

100

Entered Number is not a perfect number

Show More

7. C Program to Find ASCII value of a Character:

ASCII value represents the English characters as numbers, each letter is assigned a number from 0 to 127. For example, the ASCII value for uppercase Q is 81.

Source Code:

#include

int main()

{

    char ch;

    printf("Enter any character:");


    /* Reads the entered character and stores it

     * into the char variable ch

     */

    scanf("%c", &ch);


    /* Using the format specifiers we can get the ASCII code

     * of a character. When we use %d format specifier for a

     * char variable then it displays the ASCII value of a char

     */

    printf("ASCII value of character %c is: %d", ch, ch);

    return 0;

}

Output:

Enter any character:Q

ASCII value of character Q is: 81

Show More

8. C Program to Find Multiplication of two Binary Numbers:

This is a C program to Calculate Multiplication of two Binary Numbers.

Problem Description

This program takes two binary numbers as input and multiply them.

Problem Solution

  •  Take two binary numbers as input.

  • Do the repeated addition of binary numbers.

  • The result is the output.

Program/Source Code.

/*

 * C Program to Find Multiplication of two Binary Numbers

 */

#include

 

int binaryproduct(int, int);

 

int main()

{

 

    long binary1, binary2, multiply = 0;

    int digit, factor = 1;

 

    printf("Enter the first binary number: ");

    scanf("%ld", &binary1);

    printf("Enter the second binary number: ");

    scanf("%ld", &binary2);

    while (binary2 != 0)

    {

        digit =  binary2 % 10;

        if (digit == 1)

        {

            binary1 = binary1 * factor;

            multiply = binaryproduct(binary1, multiply);

        }

        else

            binary1 = binary1 * factor;

        binary2 = binary2 / 10;

        factor = 10;

    }

    printf("Product of two binary numbers: %ld", multiply);

    return 0;

}

 

int binaryproduct(int binary1, int binary2)

{

    int i = 0, remainder = 0, sum[20];

    int binaryprod = 0;

 

    while (binary1 != 0 || binary2 != 0)

    {

        sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;

        remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;

        binary1 = binary1 / 10;

        binary2 = binary2 / 10;

    }

    if (remainder != 0)

        sum[i++] = remainder;

    --i;

    while (i >= 0)

        binaryprod = binaryprod * 10 + sum[i--];

    return binaryprod;

}

Program Explanation

  1.  Take two binary numbers as input and store it in the variables binary1 and binary2. Initialize the variables multiply and factor with 0 and 1 respectively.

  2. Divide the variable binary2 by 10 and obtain its remainder. Store this remainder in the variable digit.

  3. Check if the digit is equal to 1 or 0. If it is 1, then multiply binary 1 with factor and override binary1 with this value. Call function binaryproduct() by passing binary1 and multiply as parameters.

  4. If it is 0, then multiply binary 1 with factor and override binary1 with this value and override binary2 with its quotient got when it is divided by 10.

  5.  Do steps 2-4 until binary2 becomes zero.

  6. In the function binaryproduct(), obtain the remainder and quotient of both the parameters.

  7. Firstly add the remainders of both parameters and further add the variable remainder.

  8. Obtain the remainder and quotient of the result got at step 7 when divided by 2. Store the remainder in the array sum[] and override the variable remainder with the quotient.

  9. Override the variables binary1 and binary2 with their quotient got at step 6.

  10.  Repeat the steps 6-9 with the new values of binary1 and binary2 until both becomes zero.

  11. When it becomes zero check if any remainder exits. If it is, then copy it into the array sum.

  12. Multiply the variable binaryprod with 10 and add the result to array sum. Override the variable binaryprod with the got result. Do this step for all array elements and return binaryprod.

  13. Print the output and exit.

Output:

Enter the first binary number: 10010

Enter the second binary number: 10101

Product of two binary numbers: 101111010

Show More

9. C Program to Check wher an Alphabet is Vowel or Consonant

This program checks whether the input character is vowel or consonant.

Problem Description 

This program takes the character value(entered by user) as input and checks whether that character is a vowel or consonant using if-else statement. Since a user is allowed to enter an alphabet in lowercase and uppercase, the program checks for both uppercase and lowercase vowels and consonants. To understand this program, you should be familiar with the following C Programming concepts:


  • C Programming if statement

  • C Programming if..else statement

Source Code 

#include

int main()

{

    char ch;

    bool isVowel = false;


    printf("Enter an alphabet: ");

    scanf("%c",&ch);


    if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'

            ||ch=='o'||ch=='O'||ch=='u'||ch=='U')

    {

        isVowel = true;


    }

    if (isVowel == true)

        printf("%c is a Vowel", ch);

    else

        printf("%c is a Consonant", ch);

    return 0;

}

Output:

Enter an alphabet: E

E is a Vowel

Show More

10. C Program to Check wher a given Number is Armstrong:

This is a C Program to check whether a given number is armstrong.

Problem Description

This C Program checks whether a given number is armstrong number.

Problem Solution

An Armstrong number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself. Hence 153 because `1^3 + 5^3 + 3^3 ``= 1 + 125 + 27 = 153`

Program/Source Code

/*

 * C Program to Check whether a given Number is Armstrong

 */

#include

#include

 

void main()

{

    int number, sum = 0, rem = 0, cube = 0, temp;

 

    printf ("enter a number");

    scanf("%d", &number);

    temp = number;

    while (number != 0)

    {

        rem = number % 10;

        cube = pow(rem, 3);

        sum = sum + cube;

        number = number / 10;

    }

    if (sum == temp)

        printf ("The given no is armstrong no");

    else

        printf ("The given no is not a armstrong no");

}

Program Explanation

In this C program, we are reading the integer value using number variable. An Armstrong number is an n-digit base b number, such that the sum of its digits raised to the power n is the number itself. Hence, 153 because `1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.`

Using while loop checks the value of number variable is not equal to 0. If the condition is true, execute the iteration of the loop. The rem variable is used to compute the modulus of the value of number variable by 10 and cube variable is used to compute the cube of the value of rem variable using pow().


Then sum variable is used to compute the summation of the value of sum variable with the value of cube variable. The If-else condition statement is used to check both the value of sum variable and the value of temp variable are equal. If the condition is true, then it will print Armstrong number. Otherwise, it will execute the else condition statement and print not Armstrong number.

Output:

enter a number370

The given no is armstrong no


enter a number1500

The given no is not a armstrong no

Show More

All Chapters

View all Chapter and number of question available From each chapter from C-programming-