Chapter:

C-Programs-on-integers

1. C Program to Check if a given Integer is Odd or Even:

Problem Description

The program takes the given integer and checks whether the integer is odd or even.


Problem Solution

  • Take the integer to be checked as input.

  • Find the remainder of the integer by dividing it by 2.

  •  Use if,else statement to check whether the remainder is equal to zero or not.

  • Print the output and exit.


Program/Source Code

#include

void main()

{

    int ival, remainder;

 

    printf("Enter an integer : ");

    scanf("%d", &ival);

    remainder = ival % 2;

    if (remainder == 0)

        printf("%d is an even integer\n", ival);

    else

        printf("%d is an odd integer\n", ival);

}

Program Explanation

  • User must first enter the integer to be checked which is stored in the variable ival.

  • Find the remainder of the integer by dividing the variable ival by integer 2 and the value is stored in the variable remainder.

  • Use if,else statement to check whether the value of the variable remainder is equal to zero or not.

  •  If it is equal to zero, then print the output as ??the integer is an even integer?.

  •  If it is not equal to zero, then print the output as ??the integer is an odd integer?.

Show More

2. .C Program to Find Number of Integers Divisible by 5 in a given range:

This is a C Program which calculates the number of integers divisible by 5 in the given range.

Problem Description

  • This program takes the range as input and finds the number of integers divisible by 5 in the given range.

  • Also finds the sum of all integers that are divisible by 5 in the given range.


Problem Solution

  • Take the range as input.

  • Find all the integers that gives remainder zero when divided by 5 and print them as output.

  • Add all the integers that are divisible by 5 and print the sum.

  • Also print the count of integers that are divisible by 5.

Program/Source Code

/*

 * C program to find the number of integers divisible by 

 * 5 between the given range num1 and num2, where num1 < num2.

 *

 * Also find the sum of all these integer numbers which are divisible

 * by 5 and display the total.

 */

#include

 

void main()

{

    int i, num1, num2, count = 0, sum = 0;

 

    printf("Enter the value of num1 and num2 \n");

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

    /* Count the number and compute their sum*/

    printf("Integers divisible by 5 are \n");

    for (i = num1; i < num2; i++)

    {

        if (i % 5 == 0)

        {

            printf("%3d,", i);

            count++;

            sum = sum + i;

        }

    }

    printf("\n Number of integers divisible by 5 between %d and %d =

 %d\n", num1, num2, count);

    printf("Sum of all integers that are divisible by 5 = %d\n", sum);

}


Program Explanation

  • Take the range as input and store it in the variables num1 and num2 respectively.

  • Firstly initialize the variables count and sum to zero.

  • Using the for loop, find all the integers that gives remainder zero when divided by 5 and print them consecutively.

  • Along with this, increment both the variables i.e increment the variable count by 1 and variable sum by the number that is divisible by 5.

  • Print the variables count and sum as output.

Output:

Case:1

Enter the value of num1 and num2

12 17

Integers divisible by 5 are

 15,

Number of integers divisible by 5 between 12 and 17 = 1

Sum of all integers that are divisible by 5 = 15

 

Case:2

Enter the value of num1 and num2

1 10

Integers divisible by 5 are

 5,10

Number of integers divisible by 5 between 1 and 10 = 2

Sum of all integers that are divisible by 5 = 15

Show More

3. C Program to Accept two Integers and Check if y are Equal:

This is a C program to accept two integers and check if they are equal.

Problem Description

This program accepts two integers and check if they are equal or not.

Problem Solution

  • Take the two integers as input.

  • Using if,else statements check if they are equal or not.

  • Print the output accordingly and exit.

Program/Source Code

/*

 * C program to accept two integers and check if they are equal

 */

#include

void main()

{

    int m, n;

 

    printf("Enter the values for M and N\n");

    scanf("%d %d", &m, &n);

    if (m == n)

        printf("M and N are equal\n");

    else

        printf("M and N are not equal\n");

}

Program Explanation

  1. Take the two integers as input and store it in the variables m and n respectively.

  2. Using if,else statements check if m is equal to n.

  3. If they are equal, then print the output as ??M and N are equal?.

  4. Otherwise print it as ??M and N are not equal?.

Output:

Case:1

Enter the values for M and N

3 3

M and N are equal

 

Case:2

Enter the values for M and N

5 8

M and N are not equal

Show More

4. C Program to Read Two Integers M and N & Swap ir Values:

This is a C program to read two integers & swap their values.

Problem Description

This program reads two integers & swaps their values.

Problem Solution

  •  Take two integers as input.

  • Use a function for swapping process.

  • Exchange the values using another variable.

  • Print the output and exit.

Program/Source Code

/*

 * C program to read two integers M and N and to swap their values.

 * Use a user-defined function for swapping. Output the values of M

 * and N before and after swapping.

 */

#include

void swap(float *ptr1, float  *ptr2);

 

void main()

{

    float m, n;

 

    printf("Enter the values of M and N \n");

    scanf("%f %f", &m, &n);

    printf("Before Swapping:M = %5.2ftN = %5.2f\n", m, n);

    swap(&m, &n);

    printf("After Swapping:M  = %5.2ftN = %5.2f\n", m, n);

}

/*  Function swap - to interchanges the contents of two items */

void swap(float *ptr1, float *ptr2)

{

    float temp;

 

    temp = *ptr1;

    *ptr1 = *ptr2;

    *ptr2 = temp;

}

Program Explanation

  1. Take the two integers as input and store it in the variables m and n respectively.

  2.  Call function swap. Pass addresses of variables to the function swap.

  3. Receive the addresses by the two pointers ptr1 and ptr2.

  4. First copy the value stored at &m to the variable temp.

  5. Copy the value stored at &n to &m.

  6. Copy the value in the variable temp to &n.

  7. Print the variables m and n as output and exit

Output:

Enter the values of M and N

2 3

Before Swapping:M =  2.00 N = 3.00

After Swapping:M  = 3.00 N = 2.00




Show More

5. C Program to Calculate Sum of Odd & Even Numbers:

This is a C program to find the sum of odd and even numbers from 1 to N.

Problem Description

The program takes the number N and finds the sum of odd and even numbers from 1 to N.

Problem Solution

  • Take the number N upto which we have to find the sum as input.

  •  Using for loop take the elements one by one from 1 to N.

  • Using if,else statements separate the element as even or odd.

  • Add the even and odd numbers separately and store it in different variables.

  • Print the sum separately and exit.

Program/Source Code

#include

 

void main()

{

    int i, num, odd_sum = 0, even_sum = 0;

 

    printf("Enter the value of num\n");

    scanf("%d", &num);

    for (i = 1; i <= num; i++)

    {

        if (i % 2 == 0)

            even_sum = even_sum + i;

        else

            odd_sum = odd_sum + i;

    }

    printf("Sum of all odd numbers  = %d\n", odd_sum);

    printf("Sum of all even numbers = %d\n", even_sum);

}

 Program Explanation

  • User must first enter the number upto which he/she wants to find the sum and is stored in the variable num.

  • Using for loop take the elements one by one from 1 to num.

  •  Use if,else statement for each element to find whether it is odd or even by dividing the element by 2.

  •  Initialize the variables odd_sum and even_sum to zero.

  •  If the element is even,then increment the variable even_sum with the current element.

  •  If the element is odd,then increment the variable odd_sum with the current element.

  • Print the variables odd_sum and even_sum separately and exit.

Output:

Case 1:

Enter the value of num

10

Sum of all odd numbers  = 25

Sum of all even numbers = 30

 

Case 2:

Enter the value of num

100

Sum of all odd numbers  = 2500

Sum of all even numbers = 2550

Show More

6. C Program to Check if a given Integer is Positive or Negative:

This is a C program to check whether a given integer is positive or negative.

Problem Description

The program takes the given integer and checks whether the integer is positive or negative.

Problem Solution

  • Take the integer which you want to check as input.

  • Check if it is greater or lesser than zero and print the output accordingly.

  • Exit.

Program/Source Code

#include

 

void main()

{

    int number;

 

    printf("Enter a number \n");

    scanf("%d", &number);

    if (number >= 0)

        printf("%d is a positive number \n", number);

    else

        printf("%d is a negative number \n", number);

}

Program Explanation

  • Take the integer which you want to check as input and store it in a variable number.

  • Using if,else statements check whether the integer is greater or lesser than zero.

  •  If it is greater than or equal to zero, then print the ouput as ??it is a positive number?.

  •  If it is lesser than zero, then print the ouput as ??it is a negative number?.

  • Exit.

Output:

Case:1

Enter a number

-10

-10 is a negative number

 

Case:2

Enter a number

45

45 is a positive number

Show More

7. C Program to Compute Sum of Digits in a given Integer:

This is a C program to compute the sum of digits in a given integer.

Problem Description

This program computes the sum of digits in a given integer.

Problem Solution
  • Take the integer as input.

  • Divide the input integer by 10, obtain its remainder and quotient.

  •  Increment the new variable with the remainder got at step 2.

  • Repeat the step 2 & 3 with the quotient obtained until the quotient becomes zero.

  •  Print the output and exit.

    Program/Source Code

/*

 * C program to accept an integer & find the sum of its digits

 */

#include

 

void main()

{

    long num, temp, digit, sum = 0;

 

    printf("Enter the number \n");

    scanf("%ld", &num);

    temp = num;

    while (num > 0)

    {

        digit = num % 10;

        sum  = sum + digit;

        num /= 10;

    }

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

    printf("Sum of the digits %ld = %ld\n", temp, sum);

}

Program Explanation
  1. Take an integer as a input and store it in the variable num.

  2.  Initialize the variable sum to zero.

  3. Divide the input integer by 10 and obtain its remainder & quotient.

  4. Store the remainder in the variable digit.

  5. Increment the variable sum with variable digit.

  6. Store the quotient into the variable num.

  7. Repeat the steps 3,4,5,6 with the new num.

  8. Do step 7 until the quotient becomes zero.

  9. Print the variable sum as output and exit.

Output:

Enter the number

300

Given number = 300

Sum of the digits 300 = 3

 

 

Enter the number

16789

Given number = 16789

Sum of the digits 16789 = 31

Show More

All Chapters

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