Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package oddsum;
- import java.util.Scanner;
- public class OddSum {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int N,i,sum = 0;
- System.out.print("Please enter any number:");
- N= input.nextInt();
- for(i=0; i <=N; i++){
- if ((i%2)==1){
- sum +=i;
- }
- }
- System.out.print("The sum of the odd numbers from 1 to "+N+" is "+sum);
- } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement