Write programs for the following using nested loop concept. (JAVA)
1. To accept any 3 integers and check and display the smallest among the 3 integers.
2. To accept a number and check and display if it is divisible by 5 and 11
Answers
Solution:
Question 1:
import java.util.*;
public class Min{
public static void main(String s[]){
Scanner sc=new Scanner(System.in);
int a,b,c,min;
System.out.print("Enter first number: ");
a=sc.nextInt();
System.out.print("Enter second number: ");
b=sc.nextInt();
System.out.print("Enter third number: ");
c=sc.nextInt();
sc.close();
min=Math.min(a,Math.min(b,c));
System.out.println("Smallest Number: "+min);
}
}
Question 2:
import java.util.*;
public class Check{
public static void main(String s[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
int a=sc.nextInt();
sc.close();
if(a%5==0){
if(a%11==0)
System.out.println("Number is divisible by both 5 and 11.");
else
System.out.println("Number is divisible by 5 but not by 11.");
}
else{
if(a%11==0)
System.out.println("Number is divisible by 11 but not by 5.");
else
System.out.println("Number is not divisible by 5 and 11.");
}
}
}
See attachments for output.
Answer:
Program 1:-
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a,b,c;
System.out.println("Enter the numbers");
a=in.nextInt();
b=in.nextInt();
c=in.nextInt();
if(a<b&&a<c)
System.out.println("The smallest number="+a);
else if(b<a&&b<c)
System.out.println("The smallest number="+b);
else if(c<a&&c<b)
System.out.println("The smallest number="+c);
}
}
Program 2:-
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n;
System.out.println("Enter a number");
for(int i=1;i<=4;i++)
{
n=in.nextInt();
if(n%5==0&&n%11==0)
System.out.println("The number is divisible by both 5 and 11");
else if(n%5==0&&n%11!=0)
System.out.println("The number is divisible by 5 but not by 11");
else if(n%5!=0&&n%11==0)
System.out.println("The number is divisible by 11 but not by 5");
else
System.out.println("The number is neither divisible by 5 nor by 11");
}
}
}