JAVA CONDITIONAL STATEMENT EXERCISE
AverageOfSumOfValues
import java.util.Scanner;
public class AverageOfSumOfValues {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
double avg;
int sum=0;
for(int i=a;i<=b;i++) {
sum+=i;
}
System.out.println("the sum is:"+sum);
avg=sum/b;
System.out.println("average of sum is:"+avg);
}
}
CharacterRhombusStructure
import java.util.Scanner;
public class CharacterRhombusStructure {
public static void main(String[] args) {
// TODO Auto-generated method stub
// A
// ABA
// ABCBA
// ABCDCBA
// ABCDEDCBA
// ABCDEFEDCBA
// ABCDEFGFEDCBA
// ABCDEFEDCBA
// ABCDEDCBA
// ABCDCBA
// ABCBA
// ABA
// A
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int c1=1;
int c2=1;
char c='A';
for(int i=1;i<(a*2);i++) {
for(int b=a-c2;b>0;b--) {
System.out.print(" ");
}
if(i<a) {
c2++;
}
else {
c2--;
}
for(int j=0;j<c1;j++) {
System.out.print(c);
if(j<c1/2) {
c++;
}
else {
c--;
}
}
if(i<a) {
c1=c1+2;
}
else {
c1=c1-2;
}
c='A';
System.out.println();
}
}
}
CubeOfGivenRangeOfNumbers
import java.util.Scanner;
public class CubeOfGivenRangeOfNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
for(int i=a;i<=b;i++) {
System.out.println("the cube number is:"+i+" "+(i*i*i));
}
}
}
DisplayTheWeekdayBetween1And7
import java.util.Scanner;
public class DisplayTheWeekdayBetween1And7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
System.out.println(getDay(a));
}
public static String getDay(int a) {
String d="";
switch(a) {
case 1:d="Monday";
break;
case 2:d="Tueshday";
break;
case 3:d="Wedneshday";
break;
case 4:d="Thurshday";
break;
case 5:d="Friday";
break;
case 6:d="Saturday";
break;
case 7:d="Sunday";
break;
default:d="Invalid day range";
}
return d;
}
}
FindAYearLeapYearOrNot
import java.util.Scanner;
import java.util.*;
public class FindAYearLeapYearOrNot {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
if((a%400==0)||(a%4==0)&&(a%100!=0)) {
System.out.println("the year is leap year:"+a);
}
else {
System.out.println("the year is not a leap year:"+a);
}
}
}
FindTheCharcaterVowelOrConsonant
import java.util.Scanner;
import java.util.*;
public class FindTheCharcaterVowelOrConsonant {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=sc.next().toLowerCase();
boolean upper=a.charAt(0)>=65 && a.charAt(0)<=90;
boolean lower=a.charAt(0)>=97 && a.charAt(0)<=122;
boolean vowels=a.equals("a")||a.equals("e")||a.equals("i")||a.equals("o")||a.equals("u");
if(a.length()>1) {
System.out.println("Error the character length is high "+a);
}
else if(!(upper || lower)) {
System.out.println("Error,Not A letter Enter upper or lower case "+a);
}
else if(vowels) {
System.out.println("the character is vowel "+a);
}
else {
System.out.println("the charcter is consonant "+a);
}
}
}
FindTheGreatestOfThreeNumbers
import java.util.Scanner;
public class FindTheGreatestOfThreeNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
if(a>b)
if(a>c)
System.out.println("the greatest number is:"+a);
if(b>a)
if(b>c)
System.out.println("the greatest number is:"+b);
if(c>a)
if(c>b)
System.out.println("the greatest number is:"+c);
}
}
FindTheNumbersOfDaysInMonth
import java.util.Scanner;
public class FindTheNumbersOfDaysInMonth {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int monthDays=0;
String monthName="unknown";
int month=sc.nextInt();
int year=sc.nextInt();
switch(month) {
case 1:
monthName="January";
monthDays=31;
break;
case 2:
monthName="February";
if((year%400==0)||((year%4==0)&&(year%100!=0))) {
monthDays=29;
}
else {
monthDays=28;
}
break;
case 3:
monthName="March";
monthDays=31;
break;
case 4:
monthName="April";
monthDays=30;
break;
case 5:
monthName="May";
monthDays=31;
break;
case 6:
monthName="June";
monthDays=30;
break;
case 7:
monthName="July";
monthDays=31;
break;
case 8:
monthName="August";
monthDays=31;
break;
case 9:
monthName="September";
monthDays=30;
break;
case 10:
monthName="October";
monthDays=31;
break;
case 11:
monthName="November";
monthDays=30;
break;
case 12:
monthName="December";
monthDays=31;
break;
default:
System.out.println("Invalid Entery");
}
System.out.println("MonthName"+" "+year+" has "+monthDays+" days\n");
}
}
GenerateAsTriangle
import java.util.Scanner;
public class GenerateAsTriangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
// @
// @@
// @@@
// @@@@
// @@@@@
// @@@@@@
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
for(int i=0;i<a;i++) {
for(int b=a-i;b>0;b--) {
System.out.print(" ");
}
for(int j=0;j<=i;j++) {
System.out.print("@");
}
System.out.println();
}
}
}
GenerateUpToDownTriangle
import java.util.Scanner;
public class GenerateUpToDownTriangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
// ******
// *****
// ****
// ***
// **
// *
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
for(int i=a;i>0;i--) {
for(int b=a-i;b>0;b--) {
System.out.print(" ");
}
for(int j=0;j<i;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
GetTheNumberWheatherItIsPositiveOrNegativeNumber
import java.util.Scanner;
public class GetTheNumberWheatherItIsPositiveOrNegativeNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
if(a>0) {
System.out.println("the number is positive:"+a);
}
else if(a<0) {
System.out.println("the number is negative:"+a);
}
else {
System.out.println("the number is negative");
}
}
}
MultiplicationTable
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
//int b=sc.nextInt();
for(int i=1;i<=a;i++) {
System.out.println("the result is:"+a+"*"+i+"="+a*i);
}
}
}
NumberRhombusStructure
import java.util.Scanner;
public class NumberRhombusStructure {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1
// 212
// 32123
// 4321234
// 543212345
// 65432123456
// 7654321234567
// 65432123456
// 543212345
// 4321234
// 32123
// 212
// 1
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int c=1;
int no=1;
int start=0;
for(int i=1;i<(a*2);i++) {
for(int b=a-no;b>0;b--) {
System.out.print(" ");
}
if(i<a) {
start=i;//for number
no++;//for space
}
else {
start=a*2-i;//for number
no--;//for space
}
for(int j=0;j<c;j++) {
System.out.print(start);
if(j<c/2) {
start--;
}
else {
start++;
}
}
if(i<a) {
c=c+2;
}
else {
c=c-2;
}
System.out.println();
}
}
}
OddNaturalNumbers
import java.util.Scanner;
public class OddNaturalNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=0;
int sum=0;
for(int i=a;i<=b;i++) {
if(i%2!=0) {
sum+=i;
System.out.println("the odd natural numbers is:"+i);
}
}
System.out.println("sum of odd natural numbers is:"+sum);
}
}
PascalsTriangle
import java.util.Scanner;
import java.applet.*;
public class PascalsTriangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1
// 1 1
// 1 2 1
// 1 3 3 1
// 1 4 6 4 1
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int c=0;
for(int i=0;i<a;i++) {
for(int b=1;b<=a-i;b++) {
System.out.print(" ");
}
for(int j=0;j<=i;j++) {
if(j==0 || i==0)
c=1;
else
c=c*(i-j+1)/j;
System.out.print(" "+c);
}
System.out.print("\n");
}
}
}
SumOfNaturalNumbers
import java.util.Scanner;
public class SumOfNaturalNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int sum=0;
for(int i=a;i<=b;i++) {
sum+=i;
System.out.println("the sum is:"+sum);
}
System.out.println("the sum of natural number in the range "+a+" "+b+" is "+sum);
}
}