We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6
ASSIGNMENT-2
Name:Chameswari Id No:N190455
A)Write a program to check if a given number is positive, negative or zero
import java.util.Scanner; public class ProgramDemo{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("enter the number:"); int num=sc.nextInt(); if(num>0) { System.out.println("Entered number is positive"); } else if(num<0) { System.out.println("Entered number is negative"); } else{ System.out.println("entered number is Zero"); } } } B)Given two non-negative int values, print true if they have the same last digit,such as with 27 and 57. import java.util.Scanner; public class ProgramDemo{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("enter two numbers:"); int num1=sc.nextInt(); int num2=sc.nextInt(); if(num1<0 && num2<0) { System.out.println("please enter the non-negative numbers"); } int lastdigit1=num1%10; int lastdigit2=num2%10; boolean res=(lastdigit1==lastdigit2); System.out.println(res); } } 2)Write a program to check if a given integer number is odd or even import java.util.Scanner; public class ProgramDemo{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("enter a number:"); int num=sc.nextInt(); if(num%2==0) { System.out.println("Number is even number"); } else{ System.out.println("Number is odd number"); } } } 3)Write a program to check if the program has received command line arguments if the program not received arguments then print "No Values", else print all the values in a single line seperated by ,(comma) public class ProgramDemo{ public static void main(String args[]) { if(args.length==0) { System.out.println("No Values"); } else { for(int i=0;i<args.length;i++) { if(i>0) { System.out.print(","); } System.out.print(args[i]); } System.out.println(); } } } 4)Initialize two character variables in a program display the characters in a program and display the characters in alphabetical order public class ProgramDemo{ public static void main(String args[]){ char ch1 = 'a'; char ch2 = 'e'; System.out.println("Character 1: " + ch1); System.out.println("Character 2: " + ch2); if (ch1<ch2) { System.out.println("Alphabetical order: " + ch1 + ", " + ch2); } else { System.out.println("Alphabetical order: " + ch2 + ", " + ch1); } } } 6)Write a program to accept gender and age from command line arguments and print the percentage of interest based on the given condition public class ProgramDemo { public static void main(String[] args) { if (args.length!= 2) { System.out.println("Please provide exactly two arguments"); System.exit(1); }
String str = args[0];
int age = Integer.parseInt(args[1]);
if (str.equals("Female") && (age >= 1 && age <= 58))
{ System.out.println("The percentage of Interest is 8.2%"); } else if (str.equals("Female") && (age >= 59 && age <= 100)) { System.out.println("The percentage of Interest is 9.2%"); } else if (str.equals("Male") && (age >= 1 && age <= 58)) { System.out.println("The percentage of Interest is 8.4%"); } else if (str.equals("Male") && (age >= 59 && age <= 100)) { System.out.println("The percentage of Interest is 10.5%"); } else { System.out.println("Invalid input"); } } }
7)Initialize a character variable with an alphabhet in a program if the
character value is lowercase the output should be displayed in uppercase import java.util.Scanner; public class ProgramDemo{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("enter a character:"); String input=sc.nextLine(); if (input.length() != 1) { System.out.println("please enter a single character."); } char character = input.charAt(0); if (Character.isLowerCase(character)) { character=Character.toUpperCase(character); System.out.println("The character is: " + character); } else if(Character.isUpperCase(character)) { character=Character.toLowerCase(character); System.out.println("The character is: " + character); } else { System.out.println("invalid input"); } } } 8)Write a program to receive a color code from the user (an Alphabhet). the program should then print the color, based on the color code given. import java.util.Scanner; public class ProgramDemo{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("enter a character:"); String input=sc.nextLine(); if (input.length() != 1) { System.out.println("please enter a single character."); } char ch=input.charAt(0); ch=Character.toUpperCase(ch); switch(ch) { case 'R': System.out.println("R->Red"); break; case 'B': System.out.println("B->Blue"); break; case 'G': System.out.println("G->Green"); break; case 'O': System.out.println("O->Orange"); break; case 'Y': System.out.println("Y->Yellow"); break; case 'W': System.out.println("W->White"); break; default: System.out.println("Invalid input"); break; } } }
Where can buy The Future of the Professions: How Technology Will Transform the Work of Human Experts 2nd Edition Richard Susskind ebook with cheap price