Você está na página 1de 7

Conditional Statements

class MyDemo1 {
public static int maximumOfThreeNumbers( int a,int b,int c)
{ int max = 0 ;
if (a>b||a==b)
{ if(a>c||a==c)
max = a ;
else
max=c ;
}
else if (b>c||b==c)
max=b ;
return(max);
}
}

class MyDemo2 {
public static String checkVowel (char alphabet )
{ if(alphabet=='a'||alphabet=='e'||alphabet=='i'||alphabet=='o'||alphabet=='u'||
alphabet=='A'||alphabet=='E'||alphabet=='I'||alphabet=='O'||alphabet=='U')
return ("YES");
else
return("NO") ;
}
}

class MyDemo3 {
public static double calculationOfInterest( double depositAmount)
{ double amountOfInterest = 0;
if (depositAmount < 1000)
amountOfInterest = (depositAmount*4);
else if (1000<=depositAmount && depositAmount<=5000)
amountOfInterest = (depositAmount*4.5);
else if ( depositAmount> 5000)
amountOfInterest = (depositAmount*5);
return(amountOfInterest/100);
}
}

class MyDemo4 {
public static double calculateTaxPaid( double grossPay )
{ double taxAmount ;
if(grossPay<240)
taxAmount = 0;
else if (grossPay>=240 && grossPay<=480)
taxAmount =15*grossPay;
else
taxAmount =28*grossPay;
return (taxAmount/100) ;
}
}

class MyDemo5 {
public static double calculatePayBack( double chargeAmount)
{ double payBack=0;
if (chargeAmount<=500)
payBack=(.25*chargeAmount)/100;
else if (chargeAmount>500 && chargeAmount<=1500)
payBack=((.25*500)+(.50*(chargeAmount-500)))/100;
else if (chargeAmount>1500 && chargeAmount<= 2500)
payBack=((.25*500)+(.50*1000)+(.75*(chargeAmount-1500)))/100;
else if (chargeAmount > 2500)
payBack =((.25*500)+(.50*1000)+(.75*1000)+(1.00*(chargeAmount-
2500)))/100;
return(payBack);
}
}

class MyDemo6 {
public static void manageLibrary(char character)
{ int choice = (int)character ;
switch(choice)
{ case 77 :
System.out.println("Member Registration function opened");
break ;
case 66 :
System.out.println("Book addition function opened");
break ;
case 74 :
System.out.println("Magazine addition function opened");
break ;
case 73 :
System.out.println("Book Issue function opened");
break ;
case 83 :
System.out.println("Book section addition function opened");
break ;
default :
System.out.println(“Invalid Option”);
break ;
}
}
}

class MyDemo7 {
public static void findDayofTheWeek(int date ,String month ,int year)
{ int totalDays , result ;
totalDays = totalNumberOfDays(date , month ,year);
result = totalDays%7 ;
switch(result)
{ case 0 :
System.out.println("Sunday");
break ;
case 1 :
System.out.println("Monday");
break ;
case 2 :
System.out.println("Tuesday");
break ;
case 3 :
System.out.println("Wednesday");
break ;
case 4 :
System.out.println("Thursday");
break ;
case 5 :
System.out.println("Friday");
break ;
case 6 :
System.out.println("Saturday");
break ;
}
}
public static int totalNumberOfDays(int date ,String month ,int year)
{ int temp1 ,temp2 , temp3 = 0,leapYearDays;
temp1 = year - 1900 ;
leapYearDays = (int)temp1/4 ;
temp2 = (temp1*365)+ leapYearDays ;
if(year%4==0&&((month.equals("January"))||(month.equals("February"))))
{ temp2=temp2-1;}
if(month.equals("January"))
temp3 = 0;
else if(month.equals("February"))
temp3 = 31 ;
else if(month.equals("March"))
temp3 = 59 ;
else if(month.equals("April"))
temp3=90 ;
else if(month.equals("May"))
temp3=120;
else if(month.equals("June"))
temp3=151;
else if(month.equals("July"))
temp3=181;
else if(month.equals("August"))
temp3=212;
else if(month.equals("September"))
temp3 = 243 ;
else if(month.equals("October"))
temp3 = 273 ;
else if(month.equals("November"))
temp3 = 304 ;
else if(month.equals("December"))
temp3 =334 ;
return(temp2 + temp3 + date);
}
}

class MyDemo8 {
public static int calculateLoanAmount(int age ,char gender,String jobStatus,int
personalAssets)
{ int loanAmount = 0 ;
if(age>=16 && age<=25)
{ if( gender =='M'||gender=='F')
{ if(jobStatus.equals("Self-Employed")&& personalAssets >25000)
{ loanAmount = 10000 ;}
else if(jobStatus.equals("Professional")&& personalAssets >25000)
{loanAmount = 15000 ;}
}
}
else if(age>=26 && age<=40)
{if(gender =='M')
{ if(((jobStatus.equals("Self-Employed"))||(jobStatus.equals("Professional")))&&
personalAssets >40000)
{ loanAmount = 25000 ;}
}
else if(gender =='F')
{if(((jobStatus.equals("Self-Employed"))||(jobStatus.equals("Professional")))&&
personalAssets >40000)
{ loanAmount = 30000 ;}
}
}
else if(age>=41 && age<=60)
{if( gender =='M'||gender=='F')
{ if(((jobStatus.equals("Self-Employed"))||(jobStatus.equals("Professional")))&&
personalAssets >50000)
{ loanAmount = 40000 ;}
}
}
else if(age>60)
{if( gender =='M'||gender=='F')
{if(jobStatus.equals("Self-Employed")&& personalAssets >25000)
{ loanAmount = 35000-(age*100) ;}
else if(jobStatus.equals("Retired")&& personalAssets >25000)
{loanAmount = 25000-(age*100) ;}
}
}
return(loanAmount);
}
}

Recursion or Iteration
class MyDemo1 {
public static int occuranceOfDigitOne(int givenNumber)
{ int count = 0 , temp = 0;
if(givenNumber<0)
{givenNumber =0 - givenNumber;}
while(givenNumber!=0)
{ temp=givenNumber%10;
if(temp==1)
{ count++;}
givenNumber=givenNumber/10;
}
return(count);
}
}

class MyDemo2 {
public static int occuranceOfGivenDigit(int givenNumber ,int givenDigit)
{ int count = 0 , temp = 0;
if(givenNumber<0)
{givenNumber =0 - givenNumber;}
while(givenNumber!=0)
{ temp=givenNumber%10;
if(temp==givenDigit)
{ count++;}
givenNumber=givenNumber/10;
}
return(count);
}
}

class MyDemo3 {
public static void findMostOccuringDigit(int givenNumber)
{ int count1=0,count2=0,count3=0,temp1,temp2,temp3;
if(givenNumber<0)
{ givenNumber = 0 - givenNumber;}
int a[]= new int[10] ;
int b[] =new int[10] ;
temp1= givenNumber ;
for(count1=0;count1<=9;count1++)
{ while(givenNumber!=0)
{ temp2=givenNumber%10;
if(temp2==count1)
{ count2++;}
givenNumber=givenNumber/10;}
a[count1]=count2 ;
b[count1]=count2 ;
givenNumber=temp1;
count2=0;}
for (count1=0;count1<9;count1++)
{ for (count2=0;count2<=8-count1;count2++)
{if (a[count2]>a[count2+1])
{ temp3=a[count2];
a[count2]=a[count2+1];
a[count2+1]=temp3;}
else
temp3=0;}}
while(count3<=9)
{ if(a[9]==b[count3])
System.out.println(count3);
count3++ ;
}
}
}

class MyDemo4{
public static int numberOfOccuranceoOfGivenWord(String givenWord)
{ int count1 ,count2=0;
String sentence[]={"Ajay","is","a","good","boy","Ajay","loves","chocolates"} ;
int temp = sentence.length ;
for(count1=0;count1<temp;count1++)
{ boolean f = sentence[count1].equals(givenWord);
if(f)
{count2++;}
}
return(count2);
}
}

class MyDemo5 {
public static int findNthTermOfTribonacci( int n)
{ int firstTerm=0,secondTerm=1,thirdTerm=1,sum=0,count=1;
while(count<=n-3)
{ sum=firstTerm+secondTerm+thirdTerm;
firstTerm=secondTerm;
secondTerm=thirdTerm;
thirdTerm=sum;
count++;}
return(sum);
}
}

class MyDemo6{
public static int calculateSumOfEvenTermsOfFibonacciDivisibleByThree(int n){
int firstTerm=0,secondTerm=1, nextTerm ,count,tempTotal=0 ;
for (count=1;count<=n-2;count++)
{ nextTerm = firstTerm + secondTerm;
if (nextTerm%2==0 && nextTerm%3==0)
{ tempTotal=tempTotal+nextTerm;}
firstTerm=secondTerm;
secondTerm=nextTerm;
}
return(tempTotal) ;
}
}

class MyDemo7 {
public static void checkingOfAscendingOrderOfAlphabets(String word)
{ int count = 0,temp = 0;
int stringLength = word.length();
int a[]=new int[stringLength] ;
for(count=0;count<stringLength;count++)
{ a[count]=(int)word.charAt(count);}
for(count=0;count<stringLength-1;count++)
{ if(a[count]>a[count+1])
{ System.out.println("The alphabets are not in ascending order");
temp++;
break ;}
}
if(temp==0)
System.out.println("The alphabets are in ascending order");
}
}

class MyDemo8 {
public static void calculateGreatestCommonDivisor(int a ,int b)
{ if(a<0||b<0)
{System.out.print("The Greatest Common Divisor is -1");}
if (a>b)
calculateGreatestCommonDivisor(a-b,b);
else if (a<b)
calculateGreatestCommonDivisor(a,b-a);
else if (a==b)
System.out.println("The Greatest Common Divisor is "+ a);
}
}

Você também pode gostar