rss
Twitter Delicious Facebook Digg Stumbleupon Favorites
Showing posts with label avarage. Show all posts
Showing posts with label avarage. Show all posts

Tuesday, October 11, 2011

Write a program to find average of consecutive N Odd number and Even number.


In mathematics, the parity of an object states whether it is even or odd.
This concept begins with integers. An even number is an integer that is "evenly divisible" by 2, i.e., divisible by 2 without remainder; an odd number is an integer that is not evenly divisible by 2. (The old-fashioned term "evenly divisible" is now almost always shortened to "divisible".) A formal definition of an even number is that it is an integer of the form n = 2k, where k is an integer; it can then be showed that an odd number is an integer of the form n = 2k + 1. An even number has the form n = 2k where k is an integer.
Examples of even numbers are −4, 8, and 1728. Examples of odd numbers are −5, 9, 3, and 71. This classification only applies to integers, i.e., a fractional number like 1/2 or 4.201 is neither even nor odd.
The sets of even and odd numbers can be defined as following:
  • Even = \{ 2k; \forall k \in \mathbb{Z} \}
  • Odd = \{ 2k+1; \forall k \in \mathbb{Z} \}
A number (i.e., integer) expressed in the decimal numeral system is even or odd according to whether its last digit is even or odd. That is, if the last digit is 1, 3, 5, 7, or 9, then it's odd; otherwise it's even. The same idea will work using any even base. In particular, a number expressed in the binary numeral system is odd if its last digit is 1 and even if its last digit is 0. In an odd base, the number is even according to the sum of its digits – it is even if and only if the sum of its digits is even.

class EvenOdd{
      public static void main(String args[]){
      int n = Integer.parseInt(args[0]);
      int cntEven=0,cntOdd=0,sumEven=0,sumOdd=0;
      while(n > 0){
           if(n%2==0){
               cntEven++;
               sumEven = sumEven + n;
           }
           else{
               cntOdd++;
               sumOdd = sumOdd + n;
           }
           n--;
      }
      int evenAvg,oddAvg;
      evenAvg = sumEven/cntEven;
      oddAvg = sumOdd/cntOdd;
      System.out.println("Average of first N Even no is "+evenAvg);
      System.out.println("Average of first N Odd no is "+oddAvg);

  }
}

Friday, October 7, 2011

A greet message according to marks obtained by a student.




class Greet{
      public static void main(String args[]){
          int marks = Integer.parseInt(args[0]);                
         switch(marks/10){
            case 10:
            case 9:
            case 8:
                     System.out.println("Excellent");
                     break;
            case 7:
                     System.out.println("Very Good");
                     break;
            case 6:
                     System.out.println("Good");
                     break;
            case 5:
                     System.out.println("Work Hard");
                     break;
            case 4:
                     System.out.println("Poor");
                     break;
            case 3:
            case 2:
            case 1:
            case 0:
                     System.out.println("Very Poor");
                     break;
            default:
                     System.out.println("Invalid value Entered");
      }
 }
}

Find the average and grade of a student


Write a programe to input three marks of a student and determine the average. Print a suitable grade if the average is between;
75-100 – A
60-74 – B
50-59 – C
0-49 – D
Print an error message if a negative number is entered or if the number is not with in the range from 0 to 100.


class Results {
 public static void main(String[] args) {
  int x = 35;
  int y = 24;
  int z = 12;
  int average=0;
 if (x > 100 | y > 100 | z > 100 | x < 0 | y < 0 | z <0 ) {
  System.out.println("The entered number is not with in the range from 0 to 100. Please enter a valid number.");
 } else {
  average = (x + y + z) / 3;
  System.out.println("Average : " + average);
 } 

 if (average>75){
  System.out.println("Grade : A");
}else if(average>60){
  System.out.println("Grade : B");
}else if(average>50){
  System.out.println("Grade : c");
}else if(average>0){
  System.out.println("Grade : D");
}
}
}
Powered by Blogger.