Complete
Complete
Conditional Constructs
and Iterative Constructs
in Java
1. Write a program in Java to accept three integers and find the largest
among them using an if Statement.
/*FindLargestOf3.java*/
import java. util. Scanner;
public class FindLargestOf3
{
public static void main (String args[])
{
int maxNum;
Scanner scan = new Scanner (System. in);
System.out.print (“Enter the first number :”);
int num1= scan.nextInt ();
maxNum= num1;
if (num2 > maxNum)
maxNum= num2;
if (num3> maxNum)
maxNum= num3;
System.out.println (“Largest of’’+ num1+”,” + num2+”and” + num3+”is:’ ’+
maxNum);
scan. close ();
}
}
2. Write a program in Java to determine the nature of roots of the quadric
equation ax2+ bx+c= 0 where discriminant= b2- 4ac determines the
nature of the roots as follows:
/*Quadratic. java*/
import java. util.Scanner;
if (discriminant>=0)
{
System. out. print (“The roots are equal. :”);
root1 = (-b + discriminant)/ (2.0*a);
root2 = (-b – discriminant)/ (2.0*a);
System. out. print (“The roots are” + root1 + “and” + root2);
}
else
{
System. out. print (“The roots are imaginary.”);
}
input. close ();
}
}
3. Write a program in Java to read the gender code (M or F) and the age
and print the price of the hair cut
Male Customers:
Female Customers:
/*HairCutSalon.java */
import java.util.Scanner;
int price = 0;
if (genderCode == ‘M’)
if (age<=13)
price = 200;
else
price = 300;
if (age<= 13)
price = 250;
else
price = 350;
else
}
4. Write a program to accept a two digit number. Add the sum of its digits
to the product of its digits. If the value is equal to the input number,
display the message” Special 2 digit number”; otherwise, display the
message” Not a special 2 digit number”.
/*SpecialNumber.java*/
import java.util.Scanner;
if (newNumber == number)
System.out.println (“Special 2 digit number”);
else
System.out.println (“Not a Special 2 digit number”);
}
}
5. Write a Java program that reads two positive integer numbers ( a and
b) and calculations ab using only addition and multiplication
operations.
/*ComputePower.java*/
import java. util.Scanner;
}
}
7. Design a class to overload a function SumSeries() as follows:
(i). void SumSeries (int n, double x) – with one integer argument and
one double argument and one double argument to find and display the
sum of the series given below:
(ii). void SumSeries ()—To find and display the sum of the following
series:
s = 1+ (1*2) + (1*2*3) + ……… + (1*2*3*4 ………………*20)
double sum = 0;
if (i% 2 == 1)
else
}
public void SumSeries ()
long sum = 0;
long product = 1;
product = product * j;
}
8. Design a class to overload a function area() as follows:
(i). double area (double a, double b, double c) with three double
arguments, returns the area of a scalene triangle using the formula:
area = s(s-a) (s-b) (s-c)
s=a+b+c
2
(ii). double area (int a, int b, int height) with three integer arguments,
returns the area of a trapezium using the formula:
area = 1/2height (a + b)
(iii). double area (double diagonal1, double diagonal2) with double
arguments, returns the area of a rhombus using the formula:
area = ½(diagonal * diagonal)
/*PrimeNumberWithBreak.java*/
import java. util.Scanner;
while (number! = 0)
{
if (IsPrime (number))
System.out.println (number + “is a prime number”);
else
System.out.println (number + “is not a prime number”);
}
}
10. Write a program to check whether the given number is a composite
number or not.
Hint: A composite number is a positive integer that has factors other
than 1 and itself
/*CompositeNumber.java*/
import java.util.Scanner;
while (number!= 0)
{
if (IsComposite (number))
System.out.println (number + “is a Composite number”);
else
System.out.println (number + “is not a Composite number”);
/*Pattern1.java*/
import java.util.*;
/*PalindromeString.java*/
import java.util.*;
/*Vowels java.util.*;
import java.util.*;
/*import java.util.Scanner;
Sample Input:
The quick brown fox jumps over the lazy dog.
Sample Output:
Frequency is: 2
/*WordCount.java*/
import java.util.*;
if (tmpChar == ‘‘)
{
if (tmpWord.compareToIgnoreCase (searchWord) = = 0)
{
count++;
}
tmpWord = “”;
}
else
tmpWord = tmpWord + tmpChar;
}
import. java.util.*;
if (i % 2 == 1)
productOdd = productOdd * numbers[i];
else
sumEven = sumEven + numbers[i];
}
System.out.println (“\nSum of integers at even indices:” + sumEven);
}
17. Write a program to accept 10 different numbers in a single
dimensional array. Display the greatest and smallest numbers of the
array elements.
import java.util.*;
System.out.println(“Enter 10 integers:”);
numbers[i] = keyboard.nextInt();
min = numbers[0];
max = numbers[0];
{
if(numbers[i]> max)
max = numbers[i];
min = numbers[i];
}
18. Write a program in Java to store the characters in word COMPUTER
in a single dimensional array. Arrange and display all the characters in
descending order.
C 1
D 1
E 2
H 1
M 1
O 1
P 1
R 3
T 1
U 1
W 1
import java.util.Scanner;
{
Scanner keyboard = new Scanner (System. in);
System.out.print(“Enter a string:”);
input = input.toUpperCase();
if (Character. isLetter(ch))
charFrequency[ch – ‘A’]++;
System.out.println(“Characters Frequency”);
if (charFrequency[i]! =0)
keyboard. close();
}
20. Write a program to input 15 integer elements in array and sort
them in ascending order.
import java.util.Scanner;
public class Ascending
{
public static void main (String args[])
{
int list[] = new int[15];