1.
Source Code
/*Objective- To print Hello World Name- Jimut Jayadev SIC No- CA100550 Date of creation- 16/03/2012 */ class ExampleOne { public static void main(String args[]) { [Link]("Hello World!"); }//end of main() }//end of class
Output
Hello World!
2. Source Code
/*Objective- To add 2 numbers Name- Jimut Jayadev SIC No- CA100550 Date of creation- 16/03/2012 */ public class Sum { public static void main(String[] args) { int a=5, b=10, c; //variable initialization c = a + b; [Link]("Sum = "+c); }//end of main() }//end of class
Output
Sum = 15
3. Source Code
/*Objective- To check whether a number is even or odd Name- Jimut Jayadev SIC No- CA100550 Date of creation- 16/03/2012 */ import [Link]; public class EvenOdd { public static void main(String[] args) { int numberToCheck; Scanner sc= new Scanner([Link]); //create object of Scanner class [Link]("Enter a number: "); numberToCheck = [Link](); //input number here if(numberToCheck % 2 == 0)
[Link]("Number is even."); else [Link]("Number is odd."); }//end of main() }//end of class
Output
Enter a number: 34 Number is even.
4. Source Code
/*Objective- To print sum of digits of an inputted number Name- Jimut Jayadev SIC No- CA100550 Date of creation- 16/03/2012 */ import [Link]; public class SumOfDigits { public static void main(String[] args) { int numberToSum, extractedDigit, sum=0; Scanner sc= new Scanner([Link]); [Link]("Enter a number: "); numberToSum = [Link]();
//create object of Scanner class //input number here
while(numberToSum > 0) //while loop to add digits { extractedDigit = numberToSum % 10; //extract each digit sum = sum + extractedDigit; //add to running sum numberToSum = numberToSum / 10; //remove last digit } [Link]("Sum of digits= "+sum); } }
Output
Enter a number: 2362 Sum of digits= 13
5. Source Code
/*Objective- To print Fibonacci Series Name- Jimut Jayadev SIC No- CA100550 Date of creation- 16/03/2012 */ import [Link]; public class Fibonacci { public static void main(String[] args) { int a = 0 , b = 1, c, lengthOfSeries, i; Scanner sc = new Scanner([Link]); //create object of Scanner class [Link]("Enter length of Fibonacci Series: ");
lengthOfSeries = [Link](); //enter length of fibonacci series [Link]("Generated Fibonacci Series:"); [Link](a+"\t"+b+"\t"); //print first two numbers of the series for(i=0 ; i<=lengthOfSeries-2 ; i++) //loop to print rest of the series { c = a + b; //a number in series is the sum of previous two numbers [Link](c+"\t"); a=b; // update new value into temporary locations b=c; } }//end of main() }//end of class
Output
Enter length of Fibonacci Series: 8 Generated Fibonacci Series: 0 1 1 2 3 5 8 13 21
6. Source Code
/*Objective- To print a pattern of '*' Name- Jimut Jayadev SIC No- CA100550 Date of creation- 16/03/2012 */ import [Link]; public class PatternOne { public static void main(String[] args) { int numOfRows, i, j; Scanner sc= new Scanner([Link]); //create object of Scanner class [Link]("Enter number of rows: "); numOfRows = [Link](); //input number of rows for(i=1 ; i<=numOfRows ; i++) //loop for row { for(j=1 ; j<=i ; j++) //loop for column { [Link](" * "); } [Link](); } }//end of main() }//end of class
Output
Enter number of rows: 4
* * * * * * * * * *
7. Source Code
/*Objective- To print an inputted string "n" times Name- Jimut Jayadev SIC No- CA100550 Date of creation- 19/03/2012 */ import [Link]; public class StringRepeat { public static void main(String[] args) { String stringToRepeat; int i, iterations; Scanner sc= new Scanner([Link]); //create object of Scanner class [Link]("Enter a string: "); stringToRepeat = [Link](); //input string here [Link]("Enter no. of repetitions: "); iterations = [Link](); for(i=0 ; i<iterations ; i++) [Link](stringToRepeat); }//end of main() }//end of class
Output
Enter a string: SILICON Enter no. of repetitions: 4 SILICON SILICON SILICON SILICON
8. Source Code
/*Objective- To print a H using "*" Name- Jimut Jayadev SIC No- CA100550 Date of creation- 19/03/2012 */ public class PrintH { public static void main(String[] args) { int i,j; for(i=1 ; i<=8 ; i++) //row loop { for(j=1 ; j<=6 ; j++) //column loop { if(i==1 || i==2 || i==3 || i==6 || i==7 || i==8) //check rows with blank spaces { if((j==3) || (j==4)) //check columns with blank spaces { [Link](" ");
} else [Link](" * "); } else [Link](" * "); } [Link](); } }//end of main() }//end of class
Output
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
9. Source Code
/*Objective- To print a square using "*" Name- Jimut Jayadev SIC No- CA100550 Date of creation- 19/03/2012 */ public class PrintSquare { public static void main(String[] args) { int i,j; for(i=1 ; i<=4 ; i++) //loop to print first 4 rows { for(j=1 ; j<=4-i ; j++) //loop to print spaces in each row { [Link](" "); } for(j=1 ; j<=2*i ; j++) //loop to print * in each row { [Link](" * "); } [Link](); } for(i=1 ; i<=3 ; i++) //loop to print last 3 rows { for(j=1 ; j<=i ; j++) //loop to print spaces in each row { [Link](" "); }
for(j=1 ; j<=8-2*i ; j++) //loop to print * in each row { [Link](" * "); } [Link](); } }//end of main() }//end of class
Output
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10. Source Code
/*Objective- To print a 8 using "*" Name- Jimut Jayadev SIC No- CA100550 Date of creation- 19/03/2012 */ public class PrintEight { public static void main(String[] args) { int i,j; for(i=1 ; i<=5 ; i++) //row loop { for(j=1 ; j<=4 ; j++) //column loop { if((i==2) || (i==4)) //check rows with blank spaces { if((j==2) || (j==3)) //check columns with blank spaces { [Link](" "); } else [Link](" * "); } else [Link](" * "); } [Link](); } }//end of main() }//end of class
Output
* * * *
* * * * * * * * * * * *
11. Source Code
/*Objective- To test two classes in a single file Name- Jimut Jayadev SIC No- CA100550 Date of creation- 19/03/2012 */ public class classOne { public static void main(String[] args) { [Link]("We are inside Class One....."); }//end of main() }//end of class class classTwo { public static void main(String[] args) { [Link]("We are inside Class Two....."); }//end of main() }//end of class
Output
>javac [Link] >java classOne We are inside Class One..... >java classTwo We are inside Class Two.....