Advertisement
Kvarz

Sum of even numbers from n to 105

Oct 12th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.90 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3. public class TheMain {
  4.    
  5.     public static void main (String args[]){
  6.         //int s;
  7.         System.out.println("Type any number from 1 to 105");
  8.        
  9.         Scanner s = new Scanner(System.in);
  10.         int n = s.nextInt();
  11.         System.out.println("Your number is "+ n);
  12.    
  13.         int gen = 0;
  14. //we have to check if number is odd or even.
  15.        
  16.             if(n%2==0){
  17. //if number is even
  18.                 System.out.println("your number is even");
  19.                  for(int t =n; t<105; t=t+2){
  20.                      System.out.println(t);
  21.                        
  22.                         gen+=t;
  23.                  }
  24.                  System.out.println();
  25.                     System.out.println(gen);
  26.             }
  27.             else {
  28. //number is odd.
  29.                 System.out.println("your number is odd");
  30.                 int turn = n+1;
  31.                 //now number is even.
  32.                 for(int t =turn; t<105; t=t+2){
  33.                      System.out.println(t);
  34.                        
  35.                         gen+=t;
  36.                  }
  37.                  System.out.println();
  38.                     System.out.println(gen);
  39.                
  40.                
  41.             }
  42.         s.close();
  43.     }
  44.    
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement