Java Assignment1
Java Assignment1
Java Assignment1
import java.util.Scanner;
public class Multiply
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
float num1, num2, multiply;
System.out.print("Enter the first number: ");
num1 = sc.nextFloat();
System.out.print("Enter the second number: ");
num2 = sc.nextFloat();
multiply = num1 * num2;
System.out.print("Multiply of two Floating Point Numbers = " + multiply);
sc.close();
}
}
2.
import java.util.Scanner;
public class ASCIIValueOfChar
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
char ch;
System.out.print("Enter the character: ");
ch = sc.next().charAt(0);
int ascii_val = (int)ch;
System.out.println("The ASCII value of " + ch + " is " + ascii_val);
sc.close();
}
}
3.
import java.util.Scanner;
public class QuotientAndRemainder
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num, div, quotient, remainder;
System.out.print("Enter the number: ");
num = sc.nextInt();
System.out.print("Enter the divisor: ");
div = sc.nextInt();
quotient = num/div;
remainder = num%div;
System.out.print("Quotient = " + quotient);
System.out.print("Remainder = " + remainder);
sc.close();
}
}
4.
import java.util.Scanner;
public class EvenOdd
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num;
System.out.print("Enter the number: ");
num = sc.nextInt();
if(num%2==0)
System.out.print(num + " is an even number.");
else
System.out.print(num + " is an odd numbers.");
sc.close();
}
}
5.
import java.util.Scanner;
public class VowelConsonant
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
char ch;
System.out.print("Enter the alphabet: ");
ch = sc.next().charAt(0);
if(ch == 'a'||ch == 'e'||ch == 'i'||ch == 'o'||ch == 'u'||ch == 'A'||ch ==
'E'||ch == 'I'||ch == 'O'||ch == 'U')
System.out.print(ch + " is a vowel.");
else
System.out.print(ch + " is a consonant.");
sc.close();
}
}
6.
import java.util.Scanner;
public class LargestNumber
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num1, num2, num3;
System.out.print("Enter the first number: ");
num1 = sc.nextInt();
System.out.print("Enter the second number: ");
num2 = sc.nextInt();
System.out.print("Enter the third number: ");
num3 = sc.nextInt();
if(num1 >= num2 && num1 >= num3)
System.out.println(num1 + " is largest among three numbers.");
else if(num2 >= num1 && num2 >= num3)
System.out.println(num2 + " is largest among three numbers.");
else
System.out.println(num3 + " is largest among three numbers.");
sc.close();
}
}
7.
import java.util.Scanner;
import java.lang.Math;
public class RootsOfQuadraticEquation
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a, b, c, d;
double x1, x2, D;
System.out.print("Enter the value of a: ");
a = sc.nextInt();
System.out.print("Enter the value of b: ");
b = sc.nextInt();
System.out.print("Enter the value of c: ");
c = sc.nextInt();
if(a!=0)
{
d = (b*b) - 4*a*c;
D = Math.sqrt(d);
x1 = (-b + D) / (2*a);
x2 = (-b - D) / (2*a);
System.out.println("Roots of the quadratic equation are " + x1 + " and
" + x2);
}
else
System.out.println("Value of a cannot be zero.");
sc.close();
}
}
8.
import java.util.Scanner;
public class LeapYear
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the year ");
int year = sc.nextInt();
if((year % 4 == 0) && ((year % 100!=0) || (year % 400 == 0)))
System.out.print(year + " is a leap year.");
else
System.out.print(year + " is not a leap year.");
}
}
9.
import java.util.Scanner;
public class CheckAlphabet
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
char ch;
System.out.print("Enter the character: ");
ch = sc.next().charAt(0);
if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
System.out.println(ch + " is an alphabet.");
else
System.out.println(ch + " is not an alphabet.");
sc.close();
}
}
10.
import java.util.Scanner;
public class SumNaturalNumber
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num, sum=0, i;
System.out.print("Enter the number: ");
num = sc.nextInt();
for(i=1; i<=num; i++)
sum+=i;
System.out.println("Sum of natural numbers = " + sum);
sc.close();
}
}
11.
import java.util.Scanner;
public class Factorial
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num, i, fact=1;
System.out.print("Enter the number whose factorial you want: ");
num = sc.nextInt();
for(i = 1; i <= num; i++)
fact *= i;
System.out.println("Fatorial of " + num + " = " + fact);
sc.close();
}
}
12.
import java.util.Scanner;
public class MultiplicationTable
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num, limit, i, res;
System.out.print("Enter the number whose multiplication table you want: ");
num = sc.nextInt();
System.out.print("Enter the limit: ");
limit = sc.nextInt();
for(i = 1; i<= limit; i++)
{
res = num * i;
System.out.println(num + " x " + i + " = " + res);
}
sc.close();
}
}
13.
import java.util.Scanner;
public class FibonacciSeries
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int num, n1, n2, n3, i;
System.out.print("Enter the number whose Fibonacci series you want: ");
num = sc.nextInt();
if(num <= 0)
System.out.print("Fibonacci series cannot be formed.");
else if(num == 1)
{
System.out.println("The Fibonacci series:");
System.out.println(0);
}
else if(num == 2)
{
System.out.println("The Fibonacci series:");
System.out.println(0);
System.out.println(1);
}
else if(num > 2)
{
System.out.println("The Fibonacci series:");
System.out.println(0);
System.out.println(1);
n1=0;
n2=1;
for(i=3; i<=num; i++)
{
n3=n1+n2;
System.out.println(n3);
n1=n2;
n2=n3;
}
}
sc.close();
}
}
14.
import java.util.Scanner;
public class GCD
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num1, num2, gcd=1, i;
System.out.print("Enter the number: ");
num1 = sc.nextInt();
System.out.print("Enter the number: ");
num2 = sc.nextInt();
for(i=1; i<=num1 && i<=num2; i++)
if(num1%i==0 && num2%i==0)
gcd = i;
System.out.println("GCD of " + num1 + " and " + num2 + " = " + gcd);
sc.close();
}
}
15.
import java.util.Scanner;
public class LCM
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num1, num2, gcd=1, lcm, i;
System.out.print("Enter the number: ");
num1 = sc.nextInt();
System.out.print("Enter the number: ");
num2 = sc.nextInt();
for(i=1; i<=num1 && i<=num2; i++)
if(num1%i==0 && num2%i==0)
gcd = i;
lcm = (num1 * num2)/gcd;
System.out.println("LCM of " + num1 + " and " + num2 + " = " + lcm);
sc.close();
}
}
16.
import java.util.*;
public class alphabetcount
{
public static void main(String[] args) {
for(char c='A';c<='Z';c++)
System.out.print(c+" ");
}
}
17
import java.util.*;
public class countnumber {
public static void main(String[] args) {
int count = 0;
System.out.println("Enter a number:");
Scanner sc= new Scanner(System.in);
int num = sc.nextInt();
while(num!=0){
num = num/10;
count++;
}
System.out.println("Number of digits in the entered integer are : "+count);
18
import java.util.*;
public class reverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter : ");
int r=0;
int n=sc.nextInt();
while(n!=0){
r=n%10;
System.out.print(r);
n=n/10;
}
19
import java.util.*;
public class power {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter number: ");
int n = sc.nextInt();
System.out.println("enter power: ");
int p = sc.nextInt();
int result=1;
for(int i=0;i<p;i++){
result *=n;
}
System.out.println(n+"^"+p+"="+result);
}
20
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string/number to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("it is palindrome.");
else
System.out.println(" isn't a palindrome.");
}
}
21
import java.util.*;
public class checkprime {
public static void main(String[] args)
{
int num, i, count=0;
Scanner sc = new Scanner(System.in);
if(count==0)
System.out.println("\nIt is a Prime Number.");
else
System.out.println("\nIt is not a Prime Number.");
22
import java.util.*;
public class primetwointerval {
public static void main(String[] args) {
int num1,num2, count=0;
Scanner sc = new Scanner(System.in);
number = num;
while (number != 0)
{
temp = number % 10;
total = total + temp*temp*temp;
number /= 10;
}
if(total == num)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
24
import java.util.*;
public class armstrongtwointer {
public static void main(String[] args) {
int num1, num2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number ::");
num1 = sc.nextInt();
System.out.println("Enter second number ::");
num2 = sc.nextInt();
25
import java.util.*;
public class primetwointerfunction {
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
System.out.println("Prime numbers between " + num1 + " and " + num2 + " are:
");
26
import java.util.*;
public class amstrongfunction {
27
import java.util.Scanner;
public class factor {
public static void main(String[] args) {
int my_input, i;