You are on page 1of 22

WIPRO TRAINING DAY 2 ASSIGNMENTS

JAVA FUNDAMENTALS

NAME = HARSHIT MORE


ENROLLMENT = 0103CS193D05

Q 1.Write a program to check if a given number is Positive, Negative, or Zero.

import java.util.*;
public class Positive_negative {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if(a==0)
{
System.out.println(a+" is ZERO");
}
else if(a<0)
{
System.out.println(a+" is Negative");
}
else
{
System.out.println(a+" is Positive");
}
}
}

Q 2. Write a program to check if a given number is odd or even.

import java.util.*;

public class odd_even {

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int a = sc.nextInt();
if(a%2==0)

System.out.println(a+" is EVEN");

else

System.out.println(a+" is ODD");

Q 3. Write a program to check if the program has received command line arguments or
not. If the program has not received the values, then print "No Values", else print all
the values in a single line separated by, (comma).
Eg1) java Example
O/P: No values
Eg2) java Example Mumbai Bangalore
O/P: Mumbai, Bangalore
[Note: You can use length property of an array to check its length
public class CountCommandLine {
public static void main(String args[])
{
if(args.length ==0 )
{
System.out.println("No Values");
}
else
{
for(int i=0;i<agrs.length;i++)
{
System.out.println(args[i]+", ");
}
}
}
}

Q 4. Initialize two character variables in a program and display the characters in


alphabetical order.
Eg1) if first character is s and second is e
O/P: e,s
Eg2) if first character is a and second is e
O/P:a,e

package myproject;
import java.util.*;
public class alphabeticalorder
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
char a = sc.next().charAt(0);
char b = sc.next().charAt(0);
if(a>b)
{
System.out.println(b+","+a);
}
else
{
System.out.println(a+","+b);
}
}
}
Q 5. Intialize 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"
package myproject;
import java.util.*;

public class alpabet_digit_special


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
char a = sc.next().charAt(0);
if(a >= 'a' && a<='z' || a>='A' && a<='Z')
{
System.out.println("ALPHABET");
}
else if(a>='0' && a<='9')
{
System.out.println("DIGIT");
}
else
{
System.out.println("Special Character");
}

Q 6. Write a program to accept gender ("Male" or "Female") and age (1-120) from
command line arguments and print the percentage of interest based on the given
conditions.
Interest == 8.2%
Gender ==> Female
Age ==>1 to 58
Interest == 7.6%
Gender ==> Female
Age ==>59 -120
Interest == 9.2%
Gender ==> Male
Age ==>1-60
Interest == 8.3%
Gender ==> Male
Age ==>61-120

package myproject;
import java.util.*;
public class percentage_basedon_age_gender
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String gender = sc.nextLine();
int age = sc.nextInt();
if(gender.equalsIgnoreCase("Female"))
{
if(age >=1 && age <=58)
{
System.out.println("Interest = 8.2%");
}
else if(age>58 && age <=120)
{
System.out.println("Interest = 7.6%");
}
}
else if(gender.equalsIgnoreCase("Male"))
{
if(age>=1 && age <=60)
{
System.out.println("Interest = 9.2%");
}
else if(age >60 && age <=120)
{
System.out.println("Interest = 8.3%");
}
}

Q 7. 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).

package myproject;
import java.util.*;
public class upper_to_lower
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
char a = sc.next().charAt(0);
if(a >='a' && a<='z')
{
char b = Character.toUpperCase(a);
System.out.println(a+"->"+b);
}
else
{
char b = Character.toLowerCase(a);
System.out.println(a+"->"+b);
}
}

Q 8. 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.

package myproject;
import java.util.*;

public class colorcode


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
char n = sc.nextLine().charAt(0);
switch (n)
{
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 Code");
}
}

Q9. Write a program to print month in words, based on input month in numbers
Example1:

C:\>java Sample 12

O/P Expected : December

Example2:

C:\>java Sample

O/P Expected : Please enter the month in numbers

Example3:

C:\>java Sample 15

O/P Expected : Invalid month

package myproject;
import java.util.*;

public class monthswitch


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
switch (n)
{
case 1:
{
System.out.println("January");
break;
}
case 2:
{
System.out.println("Feburary");
break;
}
case 3:
{
System.out.println("March");
break;
}
case 4 :
{
System.out.println("April");
break;
}
case 5:
{
System.out.println("May");
break;
}
case 6:
{
System.out.println("June");
break;
}
case 7:
{
System.out.println("July");
break;
}
case 8:
{
System.out.println("August");
break;
}
case 9:
{
System.out.println("September");
break;
}
case 10:
{
System.out.println("Octomber");
break;
}
case 11:
{
System.out.println("November");
break;
}
case 12:
{
System.out.println("December");
break;
}
default:
System.out.println("Invalid Month");
}
}

Q10. Write a program to print numbers from 1 to 10 in a single row with one tab space.

package myproject;
import java.util.*;

public class for_loop_withtabspace


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
for (int i=1;i<=10;i++)
{
System.out.print(i+"\t");
}
}

Q11. Write a program to print even numbers between 23 and 57, each number should
be printed in a separate row.

public class Assignment11 {

public static void main(String[] args) {


for (int i = 23; i <= 57; i++) {
if (i % 2 == 0)
System.out.println(i);
}

Q12. Write a program to check if a given number is prime or not

package myproject;
import java.util.*;
public class primeor_not
{

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);
int no = sc.nextInt();

if (no < 0) no *= -1;

Boolean isPrime = true;

for (int i = 2; i < no/2+1; i++)


{
if (no % i == 0)
{
isPrime = false;
break;
}
}

if (no == 0 || no == 1) isPrime = false;

if (isPrime) System.out.println("prime");
else System.out.println("not prime");
}

Q.13 Write a program to print prime numbers between 10 and 99.

package myproject;
import java.util.*;
public class prime_numbers_list
{
public static void main(String[] args)
{
int flag;
for(int i = 10;i<=99;i++)
{
flag =1;
for(int j=2;j<=i/2;++j)
{
if(i%j==0)
{
flag =0;
break;
}
}
if(flag ==1)
{
System.out.println(i);
}
}
}

Q.14 Write a Java program to find if the given number is prime or not.

Example1:

C:\>java Sample
O/P Expected : Please enter an integer number

Example2:

C:\>java Sample 1

O/P Expected : 1 is neither prime nor composite

Example3:

C:\>java Sample 0

O/P Expected : 0 is neither prime nor composite

Example4:

C:\>java Sample 10

O/P Expected : 10 is not a prime number

Example5:

C:\>java Sample 7

O/P Expected : 7 is a prime number

package myproject;
import java.util.*;

public class prime_check


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please Enter an integer number");
int num = sc.nextInt();
int flag;
boolean isprime = true;
if(num == 0 || num == 1)
{
System.out.println(num +" is neither prime nor composite");
}
else
{
for(int i =2; i<=num/2+1;i++)
{
if(num %i ==0)
{
isprime = false;
break;
}
}
if(isprime)
{
System.out.println(num+" is a prime number");
}
else
{
System.out.println(num+" is not a prime number");
}
}
}

Q15. Write a program to add all the values in a given number and print. Ex: 1234->10

package myproject;
import java.util.*;
public class add_all_values_in_a_number
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int sum = 0;
while(num!=0)
{
int temp = num%10;
sum = temp+sum;
num = num/10;

}
System.out.println(sum);

Q16. Write a program to print * in Floyds format (using for and while loop)
*
* *
* * *

Example1:

C:\>java Sample

O/P Expected : Please enter an integer number

Example1:
C:\>java Sample 3

O/P Expected :
*
* *
* * *
package myproject;
import java.util.*;

public class star_program


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
if (args.length == 0)
{
System.out.println("Please enter an integer number");
System.exit(0);
}

int rowCount = Integer.parseInt(args[0]);

for (int i = 0; i < rowCount; i++)


{
for (int j = 0; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}

}
}

Q17. Write a program to reverse a given number and print


Eg1)
I/P: 1234
O/P:4321
Eg2)
I/P:1004
O/P:4001
package myproject;
import java.util.*;

public class reverse_a_number


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int rev = 0;
int i = (int) Math.pow(10,String.valueOf(num).length()-1);
while(num !=0)
{
int digit = num%10;
rev = rev + digit * i;
i = i/10;
num = num/10;
}
System.out.println(rev);
}

}
Q18. Write a Java program to find if the given number is palindrome or not

Example1:

C:\>java Sample 110011

O/P Expected : 110011 is a palindrome

Example2:

C:\>java Sample 1234

O/P Expected : 1234 is not a palindrome

package myproject;
import java.util.*;

public class palindrome


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int temp = num;
int rev = 0;
int i = (int) Math.pow(10,String.valueOf(num).length()-1);
while(num !=0)
{
int digit = num%10;
rev = rev + digit * i;
i = i/10;
num = num/10;
}
if(temp ==rev )
{
System.out.println(temp+" is a palindrome");
}
else
{
System.out.println(temp+" is not a palindrome");
}
}

}
Q19. Write a program to print first 5 values which are divisible by 2, 3, and 5.

package myproject;
import java.util.*;

public class divisible


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int flag=0;
int i = 0;
while(flag!=5)
{
if(i%2==0 && i%3==0 && i%5==0)
{
System.out.println(i);
flag = flag+1;
}
i++;
}

Q20. Write a program that displays a menu with options 1. Add 2. Sub
Based on the options chosen, read 2 numbers and perform the relevant operation. After
performing the operation, the program should ask the user if he wants to continue. If
the user presses y or Y, then the program should continue displaying the menu else the
program should terminate.
[ Note: Use Scanner class, you can take help from the trainer regarding the same ]

package myproject;
import java.util.*;

public class option_program


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int choice = 0;
do
{
System.out.println("1.Add");
System.out.println("2.Sub");
int option = sc.nextInt();
int num1;
int num2;
if(option == 1)
{
System.out.println("Enter two numbers ");
num1 = sc.nextInt();
num2 = sc.nextInt();
int result = num1 + num2;
System.out.println(result);
}
else if(option == 2)
{
System.out.println("Enter two numbers ");
num1 = sc.nextInt();
num2= sc.nextInt();
int result = num1 - num2;
System.out.println(result);
}
System.out.println("Continue ?");
char ch = sc.next().charAt(0);
if(ch=='Y' || ch =='y')
{
choice = 0;
}
else
{
choice = 1;
}
}
while(choice != 1);
}

You might also like