QN017. Write a program to print sum of odd numbers between 1 to 100.
1. Objective
To print the sum of odd numbers between 1 to 100.
2. Theory
In this program, we use for loop to generate numbers between 1 to 100. Initialize sum variable as 0 at the beginning to store sum of all odd numbers and print the result and also use if statement to get the odd numbers.
3. Source Code
      public class Qn017{ 
        public static void main(String[] args){
          int sum = 0;
          for(int i=1;i<=100;i++)
          {
            if(i%2!=0)
              sum+=i;
          }
          System.out.println("Sum of odd number between 1 to 100 is " + sum);
        }
      }
    
    4. Output
Sum of odd number between 1 to 100 is 2500
5. Conclusion
In this way, I would like to conclude that, we can calculate sum of odd numbers between 1 to 100 using this program.