Blog about Programming Languages & Coding

Blog about Programming Languages & Coding
Contents for Computer Science, IT, B.Sc. CS & IT, M.Sc. CS & IT, MCA, BE CS & IT, ME CS & IT , Interview Questions, Books and Online Course Recommendations from Udemy, Coursera, etc

Java Programs Based on if statement

 Programs Based on if statement



1. Write a program to display a greet message according to percentage obtained by student.

import java.util.Scanner;


public class MessageWithGradeElseIfLadder {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter letter grade: ");

String grade = sc.next();

//read user input

if(grade.equals("A")) //passes in A

System.out.println("Excellent!");

else if (grade.equals("B"))

System.out.println("Good");

else if(grade.equals("C") || grade.equals("D"))

System.out.println("Poor");

else if (grade.equals("F"))

System.out.println("Bad");

else

System.out.println("This is not valid at all. ");

sc.close();

}

}

Output:

Enter letter grade: 

A

Excellent!







2. WAP program to print ODD Numbers from 1 to N.

package assignments;


import java.util.Scanner;


public class PrintOddNumbersFrom1ToUserInput {


public static void main(String[] args) {

int n;

Scanner sc = new Scanner(System.in);

System.out.println("Enter any number: ");

n = sc.nextInt();

for(int i =1; i<=n;i++) {

if(i%2!=0)   System.out.print(i+" is odd!");

System.out.println();

}

}

}

Output:

Enter any number: 

10

1 is odd!


3 is odd!


5 is odd!


7 is odd!


9 is odd!


3. WAP to find sum of digits of entered number. 

package assignments;


import java.util.Scanner;


public class SumOfDigits {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number: ");

int n = sc.nextInt();

int sum = 0;

while(n != 0)

{

sum = sum + n % 10;

n = n / 10;

}

System.out.println("Sum of the digits of your number: "+sum);

}

}

Output:

Enter a number: 

61

Sum of the digits of your number: 7


4. WAP program for Factorial.

package assignments;

import java.util.Scanner;

public class Factorial {


public static void main(String[] args) {

int i, num1, factorial = 1;


Scanner sc = new Scanner(System.in);

System.out.println("Enter any number: ");

num1 = sc.nextInt();

for(i = 1; i<=num1;i++) {

factorial = factorial * i;

}

System.out.println("The factorial of your number is: "+ factorial);

}

}

Output:

Enter any number: 

4

The factorial of your number is: 24


5. Write a program to convert given no. of days into months and days.  (Assume that each month is of 30 days)

package assignments;


import java.util.Scanner;


public class DaysToYearsWeeks10 {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter days: ");

int days = sc.nextInt();

int years=days/365;

int months = days/30;

int week=(days-(years*365))/7;

days=days-((years*365)+(week*7));

System.out.println("Total of years: "+ years);

System.out.println("Total of months: "+ months);

System.out.println("Total of weeks: "+ week);

System.out.println("Total of days: "+ days);

}

}


Output:

Enter days: 

61

Total of years: 0

Total of months: 2

Total of weeks: 8

Total of days: 5



Java Programs Based on if statement Java Programs Based on if statement Reviewed by Asst. Prof. Sunita Rai on March 27, 2022 Rating: 5

No comments:

Powered by Blogger.