JAVA PROGRAMS BASED ON CONDITIONAL STATEMENT
Conditional Statement
The statement which specify the conditions in following ways
1. Simple if : The conditional statement which have no else statement.
if(condition)
{ }
2. Binary if: The conditional statement which have one 'if' and one 'else' statement.
if(condition)
{}
else
{}
3. Ladder if:The conditional statement which have one 'if' many if else () and one 'else' statement.
if(condition)
{}
else if(condition)
{}
else
{}
4. Nested if: The conditional statement which have one condition inside the other.
if(condition)
{
if(condition)
{}
}
else if(condition)
{}.
Switch Case Statement
SAMPLE PROGRAMSProgram 1:Write a program which accepts two numbers if first number is greater than second the squares the first number ad cubes the second number ans visa versa.
Solution:
import java.util.*;import java.util.*;
public class Program1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int x,y;
System.out.println("Enter First Number: ");
x=sc.nextInt();
System.out.println("Enter Second Number: ");
y=sc.nextInt();
if(x>y)
{
sq = x * x;
cb = y * y * y;
System.out.println("Square of First Number:"+sq);
System.out.println("Cube of Second Number:"+sq);
}
if(y>x)
{
sq = y * y;
cb = x * x * x;
System.out.println("Square of Second Number:"+sq);
System.out.println("Cube of First Number:"+cb);
}
}
}
Program 2:Write a program which accepts choice from user if choice is '1' then checks Armstrong Number or if choice is '2' then checks Magic Number.
Solution:
import java.util.*;import java.util.*;
public class Program2
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int armstr=0,mgcno=0,actno,no,choice;
System.out.println("Enter Your Choice: ");
choice=sc.nextInt();
switch(choice)
{
case 1:
//Checks Arm Strong Number
System.out.println("Enter A Number:");
no=sc.nextInt();
actno=no;
while(no>0)
{
int p=no%10;
armstr=armstr*10+p;
no=no/10;
}
if(actno==armstr)
{
System.out.println("Its an ArmStrong Number!");
}
else
{
System.out.println("Its not an ArmStrong Number!");
}
break;
case 2:
//Checks Magic Number
System.out.println("Enter A Number:");
no=sc.nextInt();
actno=no;
while(no>0)
{
int p=no%10;
mgcno=mgcno*10+p;
no=no/10;
}
if(mgcno==10 || mgcno==1)
{
System.out.println("Its a Magic Number!");
}
else
{
System.out.println("Its not a Magic Number!");
}
break;
}
}
}
Program
3:Write a program which accepts choice from user if choice is '1' then
checks Perfect Number or if choice is '2' then checks Palindrome Number.
Solution:
import java.util.*;import java.util.*;
public class Program3
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int preft=0,sum=0,prod=1palin=0,actno,no,choice;
System.out.println("Enter Your Choice: ");
choice=sc.nextInt();
switch(choice)
{
case 1:
//Checks Perfect Number
System.out.println("Enter A Number:");
no=sc.nextInt();
perftno=no;
for(int i=1;i<=no/2;i++)
{
if(no%i==0)
{
sum=sum+i;
prod=prod*i;
}
}
if(sum==prod)
{
System.out.println("Its a Perfect Number!");
}
else
{
System.out.println("Its not a Perfect Number!");
}
break;
case 2:
//Checks Palindrome Number
int rev=0;
System.out.println("Enter A Number:");
no=sc.nextInt();
actno=no;
while(no>0)
{
int p=no%10;
rev=rev*10+p;
no=no/10;
}
if(rev==actno)
{
System.out.println("Its a Palindrome Number!");
}
else
{
System.out.println("Its not a Palindrome Number!");
}
break;
}
}
}
PROGRAMS ASKED IN BOARD EXAMINATION
Program 1:Using the switch-case statement, write a menu driven program to do the following : (a) To generate and print Letters from A to Z and their Unicode Letters [I.C.S.E. 2019] Unicode
(b) Display the following pattern using iteration (looping) statement: 1
Solution:-
import java.io.*;
import java.util.*;
class SwitchCase {
public static void main(String args[ ]) {
Scanner sc = new Scanner (System.in);
System.out.println(” 1. Enter 1 for Unicode:”);
System.out.prindn(” 2. Enter 2 for Pattern:”);
System.out.println(“Enter your choice:”);
int choice sc.nextlntO;
switch(choice){
case 1:
char ch;
System.out.println( “Letters \t Unicode”);
for (ch = ‘A’; ch < = ‘Z’; ch+ +) {
System.out.println(ch +”\t” + (int)ch);
}
break;
case 2:
int i, j;
for (i = 1; i < = 5; i+ +) {
for (j = 1; j < = i; j + +)
{
System.out.print(j + “”);.
}
System.out.printlnO;
}
break;
default:
System.out.println(“Wrong choice entered:”);
}
}
}
Program 2: Write a program to input a number and check and print whether it is a Pronic number [15] or not. (Pronic number is the number which is the product of two consecutive integers) [I.C.S.E 2018]
Examples : 12 = 3 × 4 .
20 = 4 × 5
42 = 6 × 7
Solution:
import java.io.*;
import java.util. Scanner;
class Pronic]
public static void main(String argsQ) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.print(“Enter the number: “);
int n = sc.nextlnt();
int i = 0;
while(i * (i + 1) < n) {
i++;
}
if(i *(i + 1) = = n){
System.out.println(n + ” is a Pronic Number.”);
}
else {
System.out.prindn(n + ” is not a Pronic Number.”);
}
}
}
Program 3:Using switch statement, write a menu driven program for the following : [ICSE 2017]
(i) To find and display the sum of the series given below :
S = x1 -x2 + x2 – x4 + x5 – x20
(where x = 2)
(ii) To display the following series :
1 11 111 1111 11111
For an incorrect option, an appropriate error message should be displayed.
Solution:-
import java.io.*;
class SwitchStatement {
public static void main(String argsQ) throws IOException {
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.println(“l-Sum of Series:”);
System.out.println(“2-Display Special Series:”);
System.out.println(“Enter your choice:”);
String n1 = input.readLine( );
int ch = Integer.parselnt(nl);
System.out.println(“Enter Number of Terms
String t = input.readLine();
int n = Integer.parselnt(t);
switch (ch) {
case 1:
int sign = -1;
double term = 0;
double sum = 0;
int x = 2;
System.out.println(“Value of x: ” +x);
System.out.println(“Number of terms: ” +n);
sum + = x; // First term added here,
for (int i = 2; i < = n; i+ +){
term = sign * Math.pow(x,i);
sum + = term;
sign *= -1;
}
System.out.println(“Sum of Series +sum);
break;
case 2 :
int num;
System.out.println(“Enter the number of terms: ”);
String tm = input.readLine(); .
num = Integer.parselnt(tm);
int s = 0, c;
for (c = 1; c < = num; c+ +){
s = s * 10 + 1;
System.out.print(s + ” “);
}
break;
}
}
}
No comments:
Post a Comment