You are on page 1of 9

WEEK-I Programs

1.Java user inputs


import java.util.Scanner;
class test
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter any byte number");
byte a = in.nextByte();
System.out.println("Enter any short number");
short b = in.nextShort();
System.out.println("Enter any int number");
int c = in.nextInt();
System.out.println("Enter any long number");
long d = in.nextLong();
System.out.println("Enter any float number");
float e = in.nextFloat();
System.out.println("Enter any double number");
double f = in.nextDouble();
System.out.println("Enter any char");
char g = in.next().charAt(0);
System.out.println("Enter any String");
String h = in.next();
System.out.println("Byte number is"+a);
System.out.println("Short number is"+b);
1|Page
System.out.println("Integer number is"+c);
System.out.println("Long number is"+d);
System.out.println("float number is"+e);
System.out.println("Double number is"+f);
System.out.println("Char is"+g);
System.out.println("String is"+h);
}
}

2|Page
2. Write a Java program that prints all real and imaginary solutions to the
quadratic equation ax2 + bx + c = 0. Read in a, b, c and use the quadratic
formula
import java.util.Scanner;
import java.io.*;
class quadratic
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the first number");
float a = in.nextFloat();
System.out.println("Enter the second number");
float b = in.nextFloat();
System.out.println("Enter the third number");
float c = in.nextFloat();
//if b^2-4ac is >0 then roots are r1=-b-sqrt(b2-4ac)/2a, r2=-b+sqrt(b2-4ac)/2a
// if b^2-4ac is =0 then roots are r1=-b-sqrt(b2-4ac), r2=-b+sqrt(b2-4ac)
//if b^2-4ac<0 then roots are imaginary
double temp=b*b-4*a*c;
if(temp>0)
{
double root1=(-b-Math.sqrt(temp))/2*a;
double root2=(-b+Math.sqrt(temp))/2*a;
System.out.println("Root1: "+root1);
System.out.println("Root2: "+root2);
}
3|Page
else if(temp==0)
{
double root1 =-b/2*a;
double root2 =-b/2*a;
System.out.println("Root1: "+root1);
System.out.println("Root2: "+root2);
}
else
System.out.println("Roots imaginary");
}
}

4|Page
3. Write a Java program to implement calculator operations
import java.util.Scanner;
class calc
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the first number");
int a = in.nextInt();
System.out.println("Enter the second number");
int b = in.nextInt();
System.out.println("Addition of two numbers"+ (a+b));
System.out.println("Sub of two numbers"+ (a-b));
System.out.println("Multiplication of two numbers"+ (a*b));
System.out.println("Division of two numbers"+ (a/b));
System.out.println("Rem of two numbers"+ (a%b));
}
}

5|Page
4. Write a java program to find prime factors of given number
import java.util.Scanner;
public class prime_factors
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any Number to find Prime Factors "); //
int number=sc.nextInt();
System.out.println("Given Number is : " + number);
System.out.print("Prime Factors are : " );
for (int i = 2; i <= number; i++)
{
while (number % i == 0)
{
System.out.print(i + "");
number = number / i;
}
}
if (number < 1)
System.out.println(number);
}
}

6|Page
5. Write a java program to find whether given number is Palindrome or not
import java.util.Scanner;
class palindrome
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter any number number");
int n,temp,s,rem;
n = in.nextInt();
s=0;
temp=n;
while(n!=0)
{
rem=n%10;
s=(s*10) + rem;
n=n/10;
}
if(s==temp)
{
System.out.print("The given number is palindrome");
}
else
{
System.out.println("The given number is not a plaindrome");
}
7|Page
}
}
6. Write an application that declares 5 integers, determines and prints the
largest and smallest in the group.
import java.util.Scanner;
class max_min
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter any five numbers: ");
int a = in.nextInt();
//System.out.println("Enter the second number");
int b = in.nextInt();
//System.out.println("Enter the third number");
int c = in.nextInt();
//System.out.println("Enter the fourth number");
int d = in.nextInt();
//System.out.println("Enter the fifth number");
int e = in.nextInt();
int small,big;
int t1=a<b?(a<c?a:c):(b<c?b:c);
small=t1<d?(t1<d?t1:d):(d<e?d:e);
System.out.println("Small of five numbers:" +small);
int t2=a>b?(a>c?a:c):(b>c?b:c);
big=t2>d?(t2>e?t2:e):(d>e?d:e);

8|Page
System.out.println("Max of given five numbers: "+big);
}
}

9|Page

You might also like