C Program to Print Sum of Odd Numbers

How to write a C Program to Print Sum of Odd Numbers with an example?.

C Program to Print Sum of Odd Numbers from 1 to n

This program allows the user to enter the maximum limit value. Next, this c program calculates the sum of odd numbers between 1 and the maximum limit value

TIP: We already explained the logic to check whether the given is Even or Not in C Program to Check Odd or Even article.

/* C Program to Print Sum of Odd Numbers from 1 to N */
 
#include<stdio.h>
 
int main()
{
  int i, number, Sum = 0;
 
  printf("\n Please Enter the Maximum Limit Value : ");
  scanf("%d", &number);
  
  printf("\n Odd Numbers between 0 and %d are : ", number);
  for(i = 1; i <= number; i++)
  {
  	if ( i%2 != 0 ) 
  	{
  		printf("%d  ", i);
        Sum = Sum + i;
  	}
  }
  printf("\n \n The Sum of Odd Numbers from 1 to %d  = %d", number, Sum);

  return 0;
}
C Program to Print Sum of Odd Numbers from 1 to N 1

Within this Program to Print Sum of Odd Numbers from 1 to n example, For Loop will make sure that the number is between 1 and maximum limit value.

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

In the Next line, We declared the If statement

if ( number % 2 != 0 )

Any number that is not divisible by 2 is odd. If condition will check whether the remainder of the number divided by 2 is not equal to 0 or not. If the condition is True, then it is an Odd number, and the C Programming compiler will add i value to sum.

C Program to Print Sum of Odd Numbers from 1 to n

This program to find the Sum of Odd Numbers in C is the same as above, but we altered for loop to eliminate the If statement.

/* C Program to Print Sum of Odd Numbers from 1 to N */
 
#include<stdio.h>
 
int main()
{
  int i, number, Sum = 0;
 
  printf("\n Please Enter the Maximum Limit Value : ");
  scanf("%d", &number);
  
  printf("\n Odd Numbers between 0 and %d are : ", number);
  for(i = 1; i <= number; i=i+2)
  {
    Sum = Sum + i;
    printf("%d  ", i);
  }
  printf("\n \n The Sum of Odd Numbers from 1 to %d  = %d", number, Sum);

  return 0;
}

C sum of odd number using for loop output

 Please Enter the Maximum Limit Value : 40

 Odd Numbers between 0 and 40 are : 1  3  5  7  9  11  13  15  17  19  21  23  25  27  29  31  33  35  37  39  
 
 The Sum of Odd Numbers from 1 to 40  = 400

Program to Print Sum of Odd Numbers in a Given Range

This C program allows the user to enter Minimum and maximum limit value. Next, the C program will calculate the sum of odd numbers between Minimum value and maximum value.

/* C Program to Print Sum of Odd Numbers in a Given Range */
 
#include<stdio.h>
 
int main()
{
  int i, Minimum, Maximum, Sum = 0;
 
  printf("\n Please Enter the Minimum, and Maximum Limit Values : \n");
  scanf("%d %d", &Minimum, &Maximum);
  
  printf("\n Odd Numbers between %d and %d are : \n", Minimum, Maximum);
  for(i = Minimum; i <= Maximum; i++)
  {
  	if ( i%2 != 0 )
	{
		printf("%d  ", i);
		Sum = Sum + i;
	}   
  }
  printf("\n \n The Sum of Odd Numbers betwen %d and %d  = %d", Minimum, Maximum, Sum);

  return 0;
}
 Please Enter the Minimum, and Maximum Limit Values : 
15
105

 Odd Numbers between 15 and 105 are : 
15  17  19  21  23  25  27  29  31  33  35  37  39  41  43  45  47  49  51  53  55  57  59  61  63  65  67  69  71  73  75  77  79  81  83  85  87  89  91  93  95  97  99  101  103  105  
 
 The Sum of Odd Numbers betwen 15 and 105  = 2760

Comments are closed.