Posts

Showing posts from 2017

Print your name in Java | Java Programs

Image
Java is a general purpose computer programming language based on classes and objects. It is fast, flexible, secure as well as easy to learn. In this post we will see how you can print your name in Java. If you are reading this post, I assume that you might be a beginner in the world of Java and YES I know you want to learn it. Note: If you want to read about "What is Java?", Click this link... This is the very basic example of Java. In this post, we are going to learn about how can we print our name in Java. So, Mr.beginner don't focus on other words only focus on the line below: System.out.println(" Your Name Goes Here..."); As we are at our initial stage of Java, we will not focus on other keyword only focusing on the line i.e "System.out.println("Hello world")". Use this line and write anything inside quotation marks to print as it is. The image below shows the syntax of the program along with its desired output. ...

Find Perimeter Of Rectangle | Java Programs

Image
Perimeter of Rectangle Output: *** Perimeter Of Rectangle *** Enter the width of rectangle: 3 Enter the height of rectangle: 4 Perimeter is: 14.0 Try this compiler and compile Java code online! Click Here For   J a v a O n l i n e C o m p i l e r Solution: import java.util.Scanner ; public class PerimeterOfRectangle { public static void main ( String [] strings ) { Scanner scanner = new Scanner ( System . in ); System . out . println ( "*** Perimeter Of Rectangle ***" + "\n" ); System . out . print ( "Enter the width of rectangle: " ); double width = scanner . nextDouble (); System . out . print ( "Enter the height of rectangle: " ); double height = scanner . nextDouble (); double perimeter = 2 * ( height + width ); System . out . println (); System . out . println ( "Perimeter is: " + perimeter...

Print Cubes Of Natural Numbers Upto User-Entered Number | Java Programs

Output: *** Find Cubes UPTO *** Enter no . of terms: 6 Cube of 1 is : 1 Cube of 2 is : 8 Cube of 3 is : 27 Cube of 4 is : 64 Cube of 5 is : 125 Cube of 6 is : 216 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; public class CubeUpto { public static void main ( String [] args ) { System . out . println ( "*** Find Cubes UPTO ***" + "\n" ); System . out . print ( "Enter no. of terms: " ); Scanner in = new Scanner ( System . in ); int numberOfTerms = in . nextInt (); for ( int i = 1; i <= numberOfTerms ; i ++) { System . out . println ( "Cube of " + i + " is : " + i * i * i ); } } }

Find Smallest Of Three Numbers Using Ternary Operator | Java Programs

Output: *** Smallest Number Using Ternary Operator *** Enter First Number: 1 Enter Second Number: 2 Enter Third Number: 3 Smallest Number is: 1 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; class SmallestUsingTernary { public static void main ( String [] args ) { int num1 , num2 , num3 , result , temp ; Scanner scanner = new Scanner ( System . in ); System . out . println ( "*** Smallest Number Using Ternary Operator ***" + "\n" ); System . out . println ( "Enter First Number:" ); num1 = scanner . nextInt (); System . out . println ( "Enter Second Number:" ); num2 = scanner . nextInt (); System . out . println ( "Enter Third Number:" ); num3 = scanner . nextInt (); temp = num1 < num2 ? num1 : num2 ; //ternary result = num3 < temp ? num3 : temp ; // terna...

Read The Input Number Entered By User | Java Programs

Output: *** Take Input From User *** Enter any number: 1868 You entered: 1868 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; class Input { public static void main ( String [] args ) { Scanner sc = new Scanner ( System . in ); System . out . println ( "*** Take Input From User ***" + "\n" ); System . out . print ( "Enter any number: " ); // This method reads the number provided using keyboard int num = sc . nextInt (); System . out . println ( "You entered: " + num ); } }

Take Two Inputs From A User And Find Their Sum | Java Programs

Output: *** Add Two Numbers *** Enter first number: 3 Enter second number: 8 Sum of 3 and 8 is: 11 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; class AddTwoNumbersTakingInput { public static void main ( String [] args ) { int num1 = 0; int num2 = 0; int sum = 0; Scanner sc = new Scanner ( System . in ); System . out . println ( "*** Add Two Numbers ***" + "\n" ); System . out . print ( "Enter first number: " ); num1 = sc . nextInt (); System . out . print ( "Enter second number: " ); num2 = sc . nextInt (); sum = num1 + num2 ; System . out . println ( "Sum of " + num1 + " and " + num2 + " is: " + sum ); } }

Write A Program To Add Two Numbers | Java Programs

Output: *** Add Two Numbers *** Enter first number: 3 Enter second number: 5 Sum of 3 and 5 is: 8 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; class AddTwoNumbersTakingInput { public static void main ( String [] args ) { int num1 = 0; int num2 = 0; int sum = 0; Scanner sc = new Scanner ( System . in ); System . out . println ( "*** Add Two Numbers ***" + "\n" ); System . out . print ( "Enter first number: " ); num1 = sc . nextInt (); System . out . print ( "Enter second number: " ); num2 = sc . nextInt (); sum = num1 + num2 ; System . out . println ( "Sum of " + num1 + " and " + num2 + " is: " + sum ); } }

Perform Addition of Two Numbers in Java | Java Programs

Output: *** Addition Of Two Numbers *** The first number is: 10 The second number is: 20 The sum of the 10 and 20 is: 30 Click Here For  Java Online Compiler Solution: class AdditionOfTwoNumbers { public static void main ( String [] args ) { int firstNumber = 10; int secondNumber = 20; int sum = firstNumber + secondNumber ; System . out . println ( "*** Addition Of Two Numbers ***" + "\n" ); System . out . println ( "The first number is: " + firstNumber ); System . out . println ( "The second number is: " + secondNumber ); System . out . println ( "The sum of the " + firstNumber + " and " + secondNumber + " is: " + sum ); } }

Find Largest Of 3 Numbers Using "Nested-If" | Java Programs

Output: *** Largest of 3 Number Using Nested If *** The Three Numbers are: 103, 120, 70. The largest number is: 120 Click Here For  Java Online Compiler Solution: public class LargestOf3NumbersUsingNestedIf { public static void main ( String [] args ) { int num1 = 103, num2 = 120, num3 = 70; System . out . println ( "*** Largest of 3 Number Using Nested If ***" + "\n" ); System . out . println ( "The Three Numbers are: " + num1 + ", " + num2 + ", " + num3 + "." ); System . out . println (); if ( num1 >= num2 ) { if ( num1 >= num3 ) { System . out . println ( "The largest number is: " + num1 ); } else { System . out . println ( "The largest number is: " + num3 ); } } else { if ( num2 >= num3 ) { ...

Print Java | Java Programs

Output: J a v v a J a a v v a a J J aaaaa V V aaaaa JJ a a V a a Click Here For  Java Online Compiler Solution: class PrintBasic { public static void main ( String [] args ) { System . out . println ( " J a v v a " ); System . out . println ( " J a a v v a a" ); System . out . println ( "J J aaaaa V V aaaaa" ); System . out . println ( " JJ a a V a a" ); } }

Find whether a number is positive, negative or zero | Java Programs

Output: *** Positive Or Negative Or Zero *** Enter any number: -4 The number is negative . ------------------------------------ *** Positive Or Negative Or Zero *** Enter any number: 18 The number is positive . ------------------------------------ *** Positive Or Negative Or Zero *** Enter any number: 0 The number is zero . Click Here For  Java Online Compiler Solution: import java.util.Scanner ; class PositiveOrNegativeOrZero { public static void main ( String [] args ) { Scanner obj = new Scanner ( System . in ); System . out . println ( "*** Positive Or Negative Or Zero ***" + "\n" ); System . out . println ( "Enter any number: " ); int num = obj . nextInt (); if ( num > 0) { System . out . println ( "The number is positive." ); } else if ( num < 0) { System . out . println ( "The number is negativ...

Take Number From User And Print Its Multiplication Table | Java Programs

Output: *** Multiplication Table *** Enter any number for its Multiplication Table: 9 9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81 9 x 10 = 90 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; class PrintMultiplicationTable { public static void main ( String [] args ) { Scanner scanner = new Scanner ( System . in ); System . out . println ( "*** Multiplication Table ***" + "\n" ); System . out . print ( "Enter any number for its Multiplication Table: " ); int number = scanner . nextInt (); for ( int i = 1; i <= 10; i ++) { System . out . println ( number + " x " + i + " = " + number * i ); } } }

Generate Random Numbers | Java Programs

Output: Random Numbers: *************** 47 60 18 9 67 ---------------- Random Numbers: *************** 78 68 14 79 69 Click Here For  Java Online Compiler Solution: import java.util.* ; class GenerateRandomNumbers { public static void main ( String [] args ) { Random r = new Random (); int howManyRandom = 5; System . out . println ( "Random Numbers:" ); System . out . println ( "***************" ); for ( int i = 1; i <= howManyRandom ; i ++) { System . out . println ( r . nextInt (100)); //Here 100 shows that random number } //will not exceed 100 } }

Finding Factorial Using "For Loop" | Java Programs

Output: *** Factorial Using For LOOp Only *** Find Factorial Of: 4 Factorial of 4 is: 24 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; class FactorialUsingForLoop { public static void main ( String [] args ) { Scanner scanner = new Scanner ( System . in ); System . out . println ( "*** Factorial Using For LOOp Only ***" + "\n" ); System . out . print ( "Find Factorial Of: " ); int factorialOf = scanner . nextInt (); long fact = 1; for ( int i = 1; i <= factorialOf ; i ++) { fact = fact * i ; } System . out . println ( "Factorial of " + factorialOf + " is: " + fact ); } }