Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Example 2: Program to find largest number among

three numbers using nested if


public class JavaExample{

public static void main(String[] args) {

int num1 = 10, num2 = 20, num3 = 7;

if(num1 >= num2) {

if(num1 >= num3)


/* This will only execute if conditions given in
both
* the if blocks are true, which means num1 is
greater
* than num2 and num3
*/
System.out.println(num1+" is the largest Number");
else
/* This will only execute if the condition in
outer if
* is true and condition in inner if is false. which
* means num1 is grater than num2 but less than
num3.
* which means num3 is the largest
*/
System.out.println(num3+" is the largest Number");
}
else {
if(num2 >= num3)
/* This will execute if the condition in outer if is
false
* and inner if is true which means num3 is greater
than num1
* but num2 is greater than num3. That means num2 is
largest
*/
System.out.println(num2+" is the largest Number");
else
/* This will execute if the condition in outer if is
false
* and inner if is false which means num3 is greater
than num1
* and num2. That means num3 is largest
*/
System.out.println(num3+" is the largest Number");
}
}
}

import java.util.*;
public class Exercise53 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input the first number : ");
int x = in.nextInt();
System.out.print("Input the second number: ");
int y = in.nextInt();
System.out.print("Input the third number : ");
int z = in.nextInt();
System.out.print("The result is: "+test(x, y, z,true));
System.out.print("\n");
}

public static boolean test(int p, int q, int r, boolean xyz)


{
if(xyz)
return (r > q);
return (q > p && r > q);
}
}

4)Converting string to number


How to convert a string to int in
Java
Java has two methods, which deal with converting strings to each of these
types. Let’s look at both the methods used for this conversion.
1. An int data type which is a primitive data type
2. An Integer class which contains an element of type int

Converting string to int


This uses the method, Integer.parseInt(str). This method takes in one
parameter of type String, str, and returns the number in int format.

class stringToint {
public static void main( String args[] ) {
String str = "1000";
int num = Integer.parseInt(str);
System.out.println(num);
}
}

Converting string to integer


This uses the method Integer.valueOf(str) to convert a string
to Integer format. In this case, it also takes in a single String str, and
changes the value to an Integer.

Converting string to integer


This uses the method Integer.valueOf(str) to convert a string
to Integer format. In this case, it also takes in a single String str, and
changes the value to an Integer.

class stringToInteger {
public static void main( String args[] ) {
String str = "1000";
Integer num = Integer.valueOf(str);
System.out.println(num);
}
}
Note: If the string contains letters along with numbers the function will return
an exception in this case, as shown in the code below.

class stringToInteger {
public static void main( String args[] ) {
String str = "100A0";
Integer num = Integer.valueOf(str);
System.out.println(num);
}
}
5)eligibility to vote
ava voting program
In this chapter of java programs tutorial, our task is to:
 accept age of the voter and
 verify whether he or she is eligible to vote or not. (age should be 18 or greater)

Java Voting Program

Given below is a java voting program that checks if person is eligible to vote or not. Here we
have considered that a person is eligible to vote once he attains age of 18 years.
If person is currently not eligible to vote at present then calculate the years after which the
person can cast vote.

package TIHIfElsePrograms;

import java.util.Scanner;

public class TIHVoting {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


System.out.println("Enter your age");
int age = scan.nextInt();

if(age>18){
System.out.println("Welcome to vote");
}else{
int shortBy = (18 - age);
System.out.println("Sorry, you can't vote now! You can
vote after :"+ shortBy + " years");
}

You might also like