- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 338 Articles for Java Programming
742 Views
The conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable. The operator is written as −variable x = (expression) ? value if true: value if falseExampleLive Demopublic class SmallestOf3NumsUsingTernary { public static void main(String args[]) { int a, b, c, temp, result; a = 10; b = 20; c = 30; temp = a > b ? a:b; result = c > temp ? c:temp; System.out.println("Smallest number is ::"+result); } }OutputSmallest number is ::10
13K+ Views
The following program accepts average from the user, calculates the grade and prints it.Examplepublic class CalculateStudentGrades { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter average of your marks (less than 100)::"); int average = sc.nextInt(); char grade; if(average>=80){ grade = 'A'; }else if(average>=60 && average=40 && average
9K+ Views
The following program accepts two integer variables, takes an operator regarding the operation. According to the selected operator, the program performs the respective operation and print the result.Exampleimport java.util.Scanner; public class ab39_CalculatorUsingSwitch { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter value of 1st number ::"); int a = sc.nextInt(); System.out.println("Enter value of 2nd number ::"); int b = sc.nextInt(); System.out.println("Select operation"); System.out.println("Addition-a: Subtraction-s: Multiplication-m: Division-d: "); char ch ... Read More
641 Views
Stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of plates, etc.A stack is first in first out, it has two main operations push and pop. Push inserts data into it and pop retrieves data from it.To reverse an array using stack initially push all elements into the stack using the push() method then, retrieve them back using the pop() method into another array.Exampleimport java.util.Arrays; import java.util.Stack; public class ab38_ReverseOfArray { public ... Read More
4K+ Views
Comparing three integer variables is one of the simplest programs you can write at ease. Take two integer variables, say A, B& C Assign values to variables If A is greater than B & C, Display A is the largest value If B is greater than A & C, Display B is the largest value If C is greater than A & B, Display A is the largest value Otherwise, Display A, B & C are not unique valuesExampleimport java.util.Scanner; public class LargestOfThreeNumbers { public static void main(String args[]){ Scanner sc =new Scanner(System.in); System.out.println("Enter 1st number :"); ... Read More
2K+ Views
Fibonacci Series generates subsequent number by adding two previous numbers. Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken 0, 1 or 1, 1 respectively.ExampleLive Demopublic class FibonacciSeriesWithWhileLoop{ public static void main(String args[]) { int a, b, c, i = 1, n; n = 10; a = b = 1; System.out.print(a+" "+b); while(i
6K+ Views
In English alphabet the characters 'a', 'e', 'i', 'o', 'u' are vowels and remaining letters are consonants. To find whether the given letter is a vowel or consonant.Using loop and or operator verify whether given character is 'a' or 'e' or 'i' or 'o' or 'u' else it is consonant.Exampleimport java.util.Scanner; public class VowelOrConsonant { public static void main(String args[]){ System.out.println("Enter a character :"); Scanner sc = new Scanner(System.in); char ch = sc.next().charAt(0); if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' ||ch == ... Read More
360 Views
Following is a program to swap two numbers using XOR operator.Examplepublic class ab31_SwapTwoNumberUsingXOR { public static void main(String args[]){ int a,b; Scanner sc = new Scanner(System.in); System.out.println("Enter a value :"); a = sc.nextInt(); System.out.println("Enter b value :"); b = sc.nextInt(); a = a^b; b = a^b; a = a^b; System.out.println("Value of the variable a after swapping : "+a); System.out.println("Value of the variable b after swapping : "+b); } }OutputEnter a value : 55 Enter b value : 64 Value of the variable a after swapping : 64 Value of the variable b after swapping : 55
6K+ Views
The circumference of a circle is double the product of its radius and the value of PI. Therefore, to calculate the circumference of a circleGet the radius of the circle form the user.Calculate the productPrint the final result.Example: Finding the circumference of a circle import java.util.Scanner; public class CircumfrenceOfCircle { public static void main(String args[]){ int radius; double circumference; Scanner sc = new Scanner(System.in); System.out.println("Enter the radius of the circle ::"); radius = sc.nextInt(); circumference = Math.PI*2*radius; System.out.println("Circumference ... Read More
16K+ Views
Area of a circle is the product of the square of its radius, and the value of PI, Therefore, to calculate the area of a rectangleGet the radius of the circle.Calculate the square of the radius.Calculate the product of the value of PI and the value of the square of the radius.Print the result.Exampleimport java.util.Scanner; public class AreaOfCircle { public static void main(String args[]){ int radius; double area; Scanner sc = new Scanner(System.in); System.out.println("Enter the radius of the circle ::"); radius = sc.nextInt(); ... Read More