Advertisement
JackHoughton00

Odd Sum

Mar 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. package oddsum;
  2. import java.util.Scanner;
  3.  
  4. public class OddSum {
  5.  
  6. public static void main(String[] args) {
  7. Scanner input = new Scanner(System.in);
  8. int N,i,sum = 0;
  9. System.out.print("Please enter any number:");
  10. N= input.nextInt();
  11.  
  12. for(i=0; i <=N; i++){
  13. if ((i%2)==1){
  14. sum +=i;
  15. }
  16. }
  17. System.out.print("The sum of the odd numbers from 1 to "+N+" is "+sum);
  18. } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement