Java
Share more and more And comment for help
Java Assignment 1
Java Workspace used for making assignment contain all java files of the program below
Question 1: Write a program to check if a given number is Positive, Negative or Zero.
Code:
package first;
import java.util.Scanner;
public class firstcode {
public static void main(String[] args)
{
int n;
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);
System.out.print("Enter the number you want to check:");
n = s.nextInt();
if(n > 0)
{
System.out.println("The given number "+n+" is Positive");
}
else if(n < 0)
{
System.out.println("The given number "+n+" is Negative");
}
else
{
System.out.println("The given number "+n+" is neither Positive nor Negative ");
}
}
}
Output:
Question 2:
Code:
package second;
import java.util.Scanner;
public class secondcode {
public static void main(String[] args)
{
int n;
try (Scanner s = new Scanner(System.in)) {
System.out.print("Enter the number you want to check:");
n = s.nextInt();
}
if(n % 2 == 0)
{
System.out.println("The given number "+n+" is Even ");
}
else
{
System.out.println("The given number "+n+" is Odd ");
}
}
}
Output:
Question 3: Initialize two character variables in a program and display the characters in alphabetical order.Eg1) if first character is s and second is eO/P: e,s Eg2) if first character is a and second is eO/P:a,e (If Statement)
Code:
package third;
public class thirdcode {
public static void main (String[] args)
{
char item1='a';
char item2='b';
if (item1>item2)
System.out.println(item2+" , "+item1);
else
System.out.println(item1+" , "+item2);
}
}
Output:
Question 4: Initialize a character variable in a program and if the value is alphabet then print "Alphabet" if it’s a number then print "Digit" and for other characters print "Special Character" (If Statement)
Code:
public static void main(String args[])
{
Scanner scanner=new Scanner(System.in);
char char1 =scanner.next().charAt(0);
if(char1>=48 && char1<=57)
{
System.out.print("char is Digit");
}
else if((char1>='a' && char1<='z')||(char1>='A' && char1<='Z'))
{
System.out.print("char is Alphabet");
}
else
{
System.out.print("char is special character");
}
Output:
Question 5: Write a program to convert from upper case to lower case and vice versa of an alphabet and print the old character and new character as shown in example (Ex: a->A, M->m). (If Statement)
Code:
Lower To Upper
package fifth;
import java.util.Scanner;
public class fifthcode {
public static void main(String[] input)
{
String strUpper, strLower;
try (Scanner scan = new Scanner(System.in)) {
System.out.print("Enter one word/name in Lowercase : ");
strUpper = scan.nextLine();
}
strLower = strUpper.toUpperCase();
System.out.print("Equivalent Word/Name in Uppercase : " +strLower);
}
}
Output:
Upper To Lower
package fifihcode;
import java.util.Scanner;
public class fifthrtwo {
public static void main(String[] input)
{
String strUpper, strLower;
try (Scanner scan = new Scanner(System.in)) {
System.out.print("Enter one word/name in Uppercase : ");
strLower = scan.nextLine();
}
strUpper = strLower.toLowerCase();
System.out.print("Equivalent Word/Name in Lowercase : " +strUpper);
}
}
Output:
Question 6: Write a program to print the color name, based on color code. If color code in not valid then print "Invalid Code". R->Red, B->Blue, G->Green, O->Orange, Y->Yellow, W->White.( Switch Statement)
Code:
import java.util.Scanner;
public class sixthcode {
public static void main(String[] args)
{
try (Scanner colour = new Scanner(System.in)) {
System.out.print("Enter the character(R/B/G/O/Y/W): ");
char clr = colour.next().charAt(0);
switch(clr)
{
case 'R':
System.out.println("Red");
break;
case 'B':
System.out.println("Blue");
break;
case 'G':
System.out.println("Green");
break;
case 'O':
System.out.println("Orange");
break;
case 'Y':
System.out.println("Yellow");
break;
case 'W':
System.out.println("White");
break;
default:
System.out.println("Invalid Code");
break;
}
}
}
}
Output:
Question 7: Write a program to print numbers from 1 to 10 in a single row with one tab space.( For Loop)
Code:
package seventh;
public class seventhcode {
public static void main(String[] args)
{
for(int i = 1; i <= 10; i++)
{
System.out.println(i);
}
}
}
output:
Question 8: Write a program to print even numbers between 23 and 57, each number should be printed in a separate row. ( For Loop)
Code:
package eight;
public class eighthcode {
public static void main(String[] args)
{
System.out.println("Even Numbers between 23 and 57");
for(int i=23;i<=57;i++){
if(i%2==0){
System.out.println(i);
}
}
}
}
Output:
Question 9: Write a program to check if a given number is prime or not. ( For Loop)
Code:
package ninth;
import java.util.Scanner;
public class ninthcode {
public static void main(String args[])
{
int temp;
boolean isPrime=true;
Scanner scan= new Scanner(System.in);
System.out.println("Enter any number:");
//capture the input in an integer
int num=scan.nextInt();
scan.close();
for(int i=2;i<=num/2;i++)
{
temp=num%i;
if(temp==0)
{
isPrime=false;
break;
}
}
//If isPrime is true then the number is prime else not
if(isPrime)
System.out.println(num + " is a Prime Number");
else
System.out.println(num + " is not a Prime Number");
}
}
Output:
Question 10: Write a program to print prime numbers between 10 and 99. ( For Loop)
Code:
package tenth;
public class tenth {
public static void main (String[] args)
{
int i =0;
int num =0;
//Empty String
String primeNumbers = "";
for (i = 10; i <= 99; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("Prime numbers from 10 to 99 are :");
System.out.println(primeNumbers);
}
}
Output:
Comments
Post a Comment