Thursday, 28 April 2011

A PROGRAM TO DISPLAY MESSAGE ACCORDING TO MARKS OBTAINED.


/* Write a program to display a greet message according to
   Marks obtained by student.
 */
class SwitchDemo{
 public static void main(String args[]){
  int marks = Integer.parseInt(args[0]); //take marks as command line
  argument.
   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");
   }
 }
}

TO FIND SUM AND PRODUCT OF A GIVEN DIGIT


class Sum_Product_ofDigit{
 public static void main(String args[]){
  int num = Integer.parseInt(args[0]); //taking value as command line
  argument.
   int temp = num,result=0;
  //Logic for sum of digit
  while(temp>0){
   result = result + temp;
   temp--;
  }
  System.out.println("Sum of Digit for "+num+" is : "+result);
  //Logic for product of digit
  temp = num;
  result = 1;
  while(temp > 0){
   result = result * temp;
   temp--;
  }
  System.out.println("Product of Digit for "+num+" is : "+result);
 }
}

FACTORIAL OF A GIVEN NUMBER


class Factorial{
 public static void main(String args[]){
  int num = Integer.parseInt(args[0]); //take argument as command line
  int result = 1;
  while(num>0){
   result = result * num;
   num--;
  }
  System.out.println("Factorial of Given no. is : "+result);
 }
}

FOR REVERSING A NUMBER

class Reverse{
 public static void main(String args[]){
  int num = Integer.parseInt(args[0]); //take argument as command line
  int remainder, result=0;
  while(num>0){
   remainder = num%10;
   result = result * 10 + remainder;
   num = num/10;
  }
  System.out.println("Reverse number is : "+result);
 }
}

TO GENERATE FIBONACCI SERIES



FOR  Example :
  Input - 8
  Output - 1 1 2 3 5 8 13 21
 
class Fibonacci{
 public static void main(String args[]){
  int num = Integer.parseInt(args[0]); //taking no. as command line
  argument.
   System.out.println("*****Fibonacci Series*****");
  int f1, f2=0, f3=1;
  for(int i=1;i<=num;i++){
   System.out.print(" "+f3+" ");
   f1 = f2;
   f2 = f3;
   f3 = f1 + f2;
  }
 }
}

Write a program to Concatenate natural numbers up to given range using for Loop



   Example:
   Input - 5
   Output - 1 2 3 4 5 */
class Join{
 public static void main(String args[]){
 
 
  int num = Integer.parseInt(args[0]);
  String result = " ";
  for(int i=1;i<=num;i++){
   result = result + i + " ";
  }
  System.out.println(result);
 }
}

Write a program to find sum of all integers greater than 100 and less than 200 that are divisible by 7


* Write a program to find sum of all integers greater than 100 and
   less than 200 that are divisible by 7 */
class SumOfDigit{
 public static void main(String args[]){
  int result=0;
  for(int i=100;i<=200;i++){
   if(i%7==0)
    result+=i;
  }
  System.out.println("Output of Program is : "+result);
 }
}