Monty374

Odd Sum

Oct 5th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. package oddsum;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. public class OddSum {
  6.  
  7. /**
  8. * @param args the command line arguments
  9. */
  10. public static void main(String[] args) {
  11. int number, total = 0;
  12. String num1 = JOptionPane.showInputDialog("Input a number");
  13. number = Integer.parseInt(num1);
  14. for (int i = 0; i <= number; i++) {
  15. if (i%2 != 0){
  16. total += i;
  17. }
  18. }
  19. System.out.print("Sum of odd numbers: " + total);
  20. }
  21.  
  22. }
Advertisement
Add Comment
Please, Sign In to add comment