Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
62 views

Java Assign

The document contains 11 code examples written by Jimut Jayadev to demonstrate various Java programming concepts like printing output, taking user input, performing calculations, implementing control structures like if-else and for loops, and printing patterns. Each code example is accompanied by its source code, objective, output and date of creation. The examples cover basic concepts like Hello World, addition, checking even-odd numbers to more complex topics like Fibonacci series, string repetition, pattern printing using nested for loops.

Uploaded by

Jimut Jayadev
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Java Assign

The document contains 11 code examples written by Jimut Jayadev to demonstrate various Java programming concepts like printing output, taking user input, performing calculations, implementing control structures like if-else and for loops, and printing patterns. Each code example is accompanied by its source code, objective, output and date of creation. The examples cover basic concepts like Hello World, addition, checking even-odd numbers to more complex topics like Fibonacci series, string repetition, pattern printing using nested for loops.

Uploaded by

Jimut Jayadev
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

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[]) { System.out.println("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; System.out.println("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 java.util.Scanner; public class EvenOdd { public static void main(String[] args) { int numberToCheck; Scanner sc= new Scanner(System.in); //create object of Scanner class System.out.print("Enter a number: "); numberToCheck = sc.nextInt(); //input number here if(numberToCheck % 2 == 0)

System.out.println("Number is even."); else System.out.println("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 java.util.Scanner; public class SumOfDigits { public static void main(String[] args) { int numberToSum, extractedDigit, sum=0; Scanner sc= new Scanner(System.in); System.out.print("Enter a number: "); numberToSum = sc.nextInt();

//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 } System.out.println("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 java.util.Scanner; public class Fibonacci { public static void main(String[] args) { int a = 0 , b = 1, c, lengthOfSeries, i; Scanner sc = new Scanner(System.in); //create object of Scanner class System.out.print("Enter length of Fibonacci Series: ");

lengthOfSeries = sc.nextInt(); //enter length of fibonacci series System.out.println("Generated Fibonacci Series:"); System.out.print(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 System.out.print(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 java.util.Scanner; public class PatternOne { public static void main(String[] args) { int numOfRows, i, j; Scanner sc= new Scanner(System.in); //create object of Scanner class System.out.print("Enter number of rows: "); numOfRows = sc.nextInt(); //input number of rows for(i=1 ; i<=numOfRows ; i++) //loop for row { for(j=1 ; j<=i ; j++) //loop for column { System.out.print(" * "); } System.out.println(); } }//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 java.util.Scanner; public class StringRepeat { public static void main(String[] args) { String stringToRepeat; int i, iterations; Scanner sc= new Scanner(System.in); //create object of Scanner class System.out.print("Enter a string: "); stringToRepeat = sc.next(); //input string here System.out.print("Enter no. of repetitions: "); iterations = sc.nextInt(); for(i=0 ; i<iterations ; i++) System.out.println(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 { System.out.print(" ");

} else System.out.print(" * "); } else System.out.print(" * "); } System.out.println(); } }//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 { System.out.print(" "); } for(j=1 ; j<=2*i ; j++) //loop to print * in each row { System.out.print(" * "); } System.out.println(); } 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 { System.out.print(" "); }

for(j=1 ; j<=8-2*i ; j++) //loop to print * in each row { System.out.print(" * "); } System.out.println(); } }//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 { System.out.print(" "); } else System.out.print(" * "); } else System.out.print(" * "); } System.out.println(); } }//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) { System.out.println("We are inside Class One....."); }//end of main() }//end of class class classTwo { public static void main(String[] args) { System.out.println("We are inside Class Two....."); }//end of main() }//end of class

Output
>javac classOne.java >java classOne We are inside Class One..... >java classTwo We are inside Class Two.....

You might also like