Found 338 Articles for Java Programming

Java program to find smallest of the three numbers using ternary operators

Lakshmi Srinivas
Updated on 13-Mar-2020 09:59:17

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

Java program to calculate student grades

karthikeya Boyini
Updated on 13-Mar-2020 09:57:09

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

Java program to generate a calculator using the switch case

Chandu yadav
Updated on 21-Jun-2024 11:32:02

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

Java program to reverse an array

Samual Sam
Updated on 30-Jul-2019 22:30:22

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

Java program to Largest number among three numbers

Lakshmi Srinivas
Updated on 13-Mar-2020 09:53:45

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

Java program to print the fibonacci series of a given number using while loop

Ankith Reddy
Updated on 13-Mar-2020 09:51:11

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

Java program to find whether given character is vowel or consonant

karthikeya Boyini
Updated on 19-Jun-2020 14:27:51

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

Java program to swap two numbers using XOR operator

Arjun Thakur
Updated on 13-Mar-2020 09:47:31

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

Java program to find the circumference of a circle

Ankith Reddy
Updated on 24-Jun-2024 15:46:10

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

Java program to find the area of a circle

karthikeya Boyini
Updated on 13-Mar-2020 09:43:27

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

Advertisements