Advertisement
Guest User

e.1

a guest
Oct 24th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. package ch6;
  2.  
  3. public class E1a {
  4.  
  5.         public static void main(String[] args) {
  6.         int sum = 0;
  7.         for(int i=2; i <= 100; i = i +2){
  8.                 sum = sum + i;
  9.                
  10.                 }
  11.         System.out.print(sum);
  12.        
  13.         }      
  14. }
  15.  
  16. package ch6;
  17.  
  18. public class E1b {
  19.  
  20.         public static void main(String[] args) {
  21.                 int n = 1;
  22.                 int square;
  23.                 int sum = 0;
  24.                 while (n <= 10) {
  25.                     square= (n*n);
  26.                     sum += square;
  27.                     n++;
  28.                         }
  29.                 System.out.println(sum);
  30.                 }
  31. }
  32.  
  33. package ch6;
  34.  
  35. public class E1c {
  36.  
  37.         public static void main(String[] args) {  
  38.                 for(int i=1; i <= 20; i++){
  39.                         System.out.print(Math.pow(2, i) + " ");
  40.                 }                  
  41.         }
  42.  
  43. package ch6;
  44. import java.util.Scanner;
  45. public class E1d {
  46.  
  47.         public static void main(String[] args) {
  48.                 Scanner scan = new Scanner(System.in);
  49.                 int sumOdds=0;
  50.                 int a;
  51.                 int b;
  52.                 System.out.println("lower value");
  53.                 a = scan.nextInt();
  54.                 System.out.println("upper value");
  55.                 b = scan.nextInt();
  56.                
  57.                 if(a % 2==0)
  58.                 {
  59.                         a++;
  60.                 }
  61.                 if( b % 2 ==0)
  62.                 {
  63.                         b--;
  64.                 }
  65.                 for(int j=a; j>=a && j<=b; j+=2)
  66.                 {
  67.                         sumOdds = sumOdds + j;
  68.                        
  69.                 }
  70.                 System.out.println(sumOdds);
  71.                    
  72.         }
  73.  
  74. }
  75.  
  76. package ch6;
  77.  
  78. public class E1e {
  79.  
  80.         public static void main(String[] args) {
  81.                 int sum= 0;
  82.                 int digit=0;
  83.                 int number= 123456789;
  84.                 while ( number > 0 )
  85.                 {
  86.                     digit=number % 10;
  87.                     if ( digit %2 == 1 )
  88.                     {
  89.                         sum+=digit;
  90.                     }
  91.                     number/=10;
  92.                 }
  93.                 System.out.println(sum);
  94.         }
  95.        
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement