0% found this document useful (0 votes)
20 views

Java Assigment 1

Uploaded by

ajaykarthic02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Java Assigment 1

Uploaded by

ajaykarthic02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

HOME ASSIGNMENT - 1

AJAY KARTHIC SP - ECE ‘A’


210420106004
Write and execute Java programs

1. Prime number verification


Problem:
Checking if a given number is Prime or not
Java Program:
Prime or not using for loop:

import java.util.Scanner;
class myProgram {
public static void main(String[] arg) {
int num, i, count = 0;
Scanner s = new Scanner(System.in);
System.out.println("Enter a number");
num = s.nextInt();
for (i = 2; i < num; i++) {
if (num % i == 0) {
count++;
break;
}
}
if (count == 0)
System.out.println("\nIt is a Prime number");
else
System.out.println("\nIt is not a Prime number");
}
}

Output:
Prime or not using while loop:

import java.util.Scanner;
class myProgram {
public static void main(String[] arg) {
{
int num, i = 2, count = 0;
Scanner s = new Scanner(System.in);
System.out.println("Enter a number:");
num = s.nextInt();
while (i <= num / 2) {
if (num % i == 0) {
count++;
break;
}
i++;
}
if (count == 0)
System.out.println("\nIt is a Prime number");
else
System.out.println("\nIt is not a Prime number");
}
}
}

Output:
2. Armstrong number Verification:
Problem:
Checking if a given number is Armstrong or not
Java Program:

import java.util.Scanner;
class Armstrong
{
public static void main(String[] args)
{
int num, temp, totalDigit=0, res=0, rem;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Number: ");
num = s.nextInt();
for(temp=num; num>0; num /= 10)
totalDigit++;
for(num=temp; num>0; num /= 10)
{
rem = num%10;
res = res + (int) Math.pow(rem, totalDigit);
}
if(res==temp)
System.out.println("\nArmstrong Number");
else
System.out.println("\nNot an Armstrong Number.");
}
}

Output:
3. Fibonacci series generation 0,1,1,2,3,5,8,13,21,...
Problem:
To find the Fibonacci series
Java Program:

import java.util.Scanner;

class Main {
public static void main(String[] args) {
int p = 0, q = 1, r, i = 0;
System.out.print(p + " " + q);
while (i < 10) {
r = p + q;
p = q;
q = r;
System.out.print(" " + r);
i++;
}
}
}

Output:
4. Factorial Computation
Problem:
To get the computation of factorial
Java Program:

import java.util.Scanner;
class Factorial
{
public static void main(String[] args)
{
int num, i, fact=1;
Scanner s = new Scanner(System.in);
System.out.print("Enter a Number: ");
num = s.nextInt();
for(i=num; i>=1; i--)
{
fact = fact*i;
}
System.out.println("\nFactorial Result = " +fact);
}
}

Output:
5. Finding the Biggest among three numbers
Problem:
To find the biggest number among the given 3 number
Java Program:

import java.util.Scanner;
class Bigger
{
public static void main(String[] args)
{
int a, b, c, big;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the First Number: ");
a = scan.nextInt();
System.out.print("Enter the Second Number: ");
b = scan.nextInt();
System.out.print("Enter the Third Number: ");
c = scan.nextInt();
if(a>b && a>c)
big = a;
else if(b>a && b>c)
big = b;
else
big = c;
System.out.println("\nBiggest = " +big);
}
}

Output:
6. Print the following pattern
Problem:
To print a pattern using the input
Input: 4
Java Program:

import java.util.Scanner;

class Pattern
{
public static void main(String[] args)
{
int row, i, j;
Scanner s = new Scanner(System.in);

System.out.print("Enter the Number of Lines (Row): ");


row = s.nextInt();

for(i=0; i<row; i++)


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

Output:

You might also like