Menu

Read a month and year and display the number of days for that month

import java.util.*;


public static void main(String args[]){
System.out.println("Enter a year:");
Scanner sc= new Scanner(System.in);
int year=sc.nextInt();
System.out.println(" 1.January");
System.out.println(" 2.February");
System.out.println(" 3.March");       
System.out.println(" 4.April");
System.out.println(" 5.May");
System.out.println(" 6.June");
System.out.println(" 7.July");
System.out.println(" 8.August");
System.out.println(" 9.September");
System.out.println(" 10.October");
System.out.println(" 11.November");
System.out.println(" 12.December");
System.out.println("Please pick a month");
int choice = sc.nextInt();

int flag= 0;
if(year%400==0)                          flag=1;
else if(year%100==0)                                  flag=0;
else if(year%4==0)                      flag=1;
else                                                 flag=0;

switch(choice){
case 1:           System.out.println("In the year " +year+ " January"+" has 31 days");break;
case 2:           if (flag==0)             System.out.println("In the year " +year+ " February"+" has 28 days");
                        else if(flag==1)     System.out.println("In the year " +year+ " February"+" has 29 days");break;
case 3:           System.out.println("In the year " +year+ " March"+" has 31 days");break;
case 4:           System.out.println("In the year " +year+ " April"+" has 30 days");break;
case 5:System.out.println("In the year " +year+ " May"+" has 31 days");break;
case 6:System.out.println("In the year " +year+ " June"+" has 30 days");break;
case 7:System.out.println("In the year " +year+ " July"+" has 31 days");break;
case 8:System.out.println("In the year " +year+ " August"+" has 31 days");break;
case 9:System.out.println("In the year " +year+ " September"+" has 30 days");break;
case 10:System.out.println("In the year " +year+ " October"+" has 31 days");break;
case 11:System.out.println("In the year " +year+ " November"+" has 30 days");break;
case 12:System.out.println("In the year " +year+ " December"+" has 31 days");break;
default: System.out.println("It is invalid choice"); break;
}

}}

Read a number[1-7] and display corresponding weekday’s name

Read a number[1-7] and display corresponding weekday’s name

1.Sunday
2.Monday
3.Tuesday
4.Wednesday
5.Thursday
6.Friday
7.Saturday
>7. Invalid number
import java.util.*;
class Week_days{
public static void main(String args[]){
System.out.println("Please enter a number");
Scanner sc= new Scanner(System.in);
int number=sc.nextInt();

switch(number)
{
case 1: System.out.println("Sunday");break;
case 2: System.out.println("Monday");break;
case 3: System.out.println("Tueday");break;
case 4: System.out.println("Wednesday");break;
case 5: System.out.println("Thursday");break;
case 6: System.out.println("Friday");break;
case 7: System.out.println("Saturday");break;
default : System.out.println("invalid number");
}

}}

Read year and display whether it is leap year or not.

Read year and display whether it is leap year or not.

import java.util.*;
class Check_leap_year_m1{
public static void main(String args[]){
System.out.println("Enter a year to check for leap year:");
Scanner sc= new Scanner(System.in);
int year=sc.nextInt();
if(year%400==0)                          System.out.println(year+" is a Leap year");
else if(year%100==0)                  System.out.println(year+" is not a Leap year");
else if(year%4==0)      System.out.println(year+" is a Leap year");
else                                 System.out.println(year+ " is not a leap year");
}}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
import java.util.*;
class Check_leap_year_m2{
public static void main(String args[]){
System.out.println("Enter a year to check for leap year:");
Scanner sc= new Scanner(System.in);
int year=sc.nextInt();
if((year%400==0) || (year%4==0) && (year%100 !=0))      
System.out.println(year+" is a Leap year");
else                                                                                                
System.out.println(year+ " is not a leap year");

}}

Read three subject marks of a student and perform the following.

Read three subject marks of a student and perform the following.

a) Display the total marks scored
b) Display the average of marks
c) Display the grade and Remarks as per the following criteria
MARKS                  GRADE                    REMARKS
>=80                      A                             Excellent
>=70                      B                              Very Good
>=60                      C                              Good
>=50                       D                             Satisfactory
<50                        E                              Failure, Try Again!!!
import java.util.*;
class Student_grades_m1{
public static void main(String args[]){
System.out.println("Please enter the three subjects marks");
Scanner sc= new Scanner(System.in);
int sub1= sc.nextInt();
int sub2=sc.nextInt();
int sub3=sc.nextInt();
int total= (sub1+sub2+sub3);
float average = (float)total/3;
System.out.println("Total Marks "+total);
System.out.println("Average  "+average);
String grade;
if(average>=80)   grade="Excellent";
else if(average>=70)           grade="very good";
else if(average>=60)           grade="good";
else if(average>=50)   grade="Satisfactory";
else                                         grade="failure, try again!!!";

System.out.println("Remarks: "+grade);  }}

Read age of a person and display whether he/she is eligible for voting or not.


import java.util.*;
class Check_eligibility_voting{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Please enter your age");
int age= sc.nextInt();

if(age>18) System.out.println("You are eligible for voting");
else System.out.println("You are not eligible for voting");

}}

Write a program to convert Fahrenheit to Celsius


import java.util.*;
class Fahrenheit_celcius{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter the temperature in Fahrenheit");
float temper_f = sc.nextFloat();
float temper_c = (temper_f-32)*5/9;
System.out.println("The temperature in celcius is "+temper_c);

}}

PRINT ALPHABETS


class Test{
public static void main(String args[]){

char ch;

System.out.println("Upper Case alphabets");
for(ch='A'; ch<='Z';ch++) System.out.print(ch+" ");

System.out.println("\n");

System.out.println("Lower Case alphabets");
for( ch = 'a' ; ch <= 'z' ; ch++ )      System.out.print(ch+" ");


}}

PRINT HELLO WORD WITHOUT USING SEMICOLON


class Test{
public static void main(String args[]){

if(System.out.printf("Hello World")!=null) // printlnà error
{
}


}}

Read a number from the user and Display sum of first n odd numbers import java.util.*;


class Sum_of_n_odd_numbers{
public static void main(String args[]){

System.out.print("How many odd numbers you want to add ?");
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int sum=0;
int count=0;
if(n>=1)
{

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

if(i%2!=0)
{sum=sum+i; count++;}
if(count==n) break;
}

}
System.out.println("The sum of first "+n+" odd numbers is "+sum);
System.out.println(count);

}}

Read a number from the user and Display sum of first n even numbers

import java.util.*;
class Sum_of_n_even_numbers{
public static void main(String args[]){

System.out.print("How many even numbers you want to add ?");
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int sum=0;
int count=0;
if(n>=1)
{

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

if(i%2==0)
{sum=sum+i; count++;}
if(count==n) break;
}

}

System.out.println("The sum of first "+n+" even numbers is "+sum);
System.out.println(count);

}}

Read a number from the user and Display sum of prime numbers upto n;

import java.util.Scanner;
class Sum_Prime
{
public static void main(String args[])
{

Scanner s = new Scanner(System.in);
System.out.println ("Enter the upper limit :");
int end = s.nextInt();
System.out.println ("The prime numbers are :");
boolean isprime=false;
int i;
int SUM_PRIME=0;
for(int number = 1; number <= end; number++)
{

for(i = 2; i < number; i++)
  {
  if(number % i == 0)
          {
         isprime = false;
        break;
        }
                 
       else
       {
       isprime = true;
       }
}

if(number==i) isprime=true;
if(isprime == true) {System.out.print(number+" "); SUM_PRIME=SUM_PRIME+number;}
}

System.out.println("The sum of prime numbers in the given range " +SUM_PRIME);
}}

Read a number from the user and Display sum of strong numbers upto n;

import java.util.*;
class Sum_Strong{
public static void main(String[] args) {

Scanner sc= new Scanner(System.in);
System.out.print("Enter UPPER limit: ");
int end= sc.nextInt();
int SUM_STRONG = 0;

System.out.println("The Strong numbers in the given range are");

for(int i=1;i<end;i++)
{
int number=i;
int sum=0;

        while(number!=0)
        {
        int digit=number%10;
        int fact=1;

        for(int j=1;j<=digit;j++)
        {
        fact=fact*j;
        }
sum=sum+fact;
number=number/10;
}

if(sum==i) {System.out.println(i); SUM_STRONG= SUM_STRONG+i;}

}
System.out.println("The sum of strong numbers is "+SUM_STRONG);

}}

Display sum of Armstrong numbers upto n.


import java.util.*;
class Sum_Armstrong{
public static void main(String[] args) {

Scanner sc= new Scanner(System.in);
System.out.print("Enter a number to end: ");
int end= sc.nextInt();
int SUM_ARMSTRONG= 0;

System.out.print("The Armstrong numbers in the given range are ");

for(int i=1;i<=end;i++)
{
int number =i;
int sum=0;

     while(number > 0)
      {
      int digit = number % 10;
      sum = sum + (digit *digit  * digit);
      number = number / 10;
      }
if(sum == i){ System.out.print(i+" "); SUM_ARMSTRONG=SUM_ARMSTRONG+i;}

}
System.out.println();
System.out.println("The sum of armstrong numbers is "+SUM_ARMSTRONG);
}}