Unit 1 (Introduction)
1. Java program to generate the prime numbers from 1 to N
import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.println("Enter the range: ");
Scanner sc = new Scanner(System.in);
int number =Integer.parseInt(sc.nextLine());
for (int num = 2; num <= number; num++)
boolean isPrime = true;
for (int i=2; i <= num/2; i++)
if ( num % i == 0)
isPrime = false;
break;
}
}
if ( isPrime == true )
System.out.println(num);
2. Java program to find the roots of a quadratic equation
import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.println("Enter the first coefficient: ");
Scanner sc = new Scanner(System.in);
double a = Integer.parseInt(sc.nextLine());
System.out.println("Enter the second coefficient: ");
double b = Integer.parseInt(sc.nextLine());
System.out.println("Enter the third coefficient: ");
double c = Integer.parseInt(sc.nextLine());
if(a!=0.0){
double d = (b*b)-(4*a*c);
if (d==0.0){
System.out.println("The roots are real and equal.");
double r = -b/(2*a);
System.out.println("The roots are "+ r+"and"+ r);
}else if(d>0.0){
System.out.println("The roots are real and distinct.");
double r1 = (-b+(Math.sqrt(d)))/(2*a);
double r2 = (-b-(Math.sqrt(d)))/(2*a);
System.out.println("The root1 is: "+ r1);
System.out.println("The root2 is: "+ r2);
}else{
System.out.println("The roots are imaginary.");
double rp = -b/(2*a);
double ip = Math.sqrt(-d)/(2*a);
System.out.println("The root1 is: "+ rp+ "+ i"+ip);
System.out.println("The root2 is: "+ rp+ "- i"+ip);
}else{
System.out.println("Not a quadratic equation.");
3. Java program to display the sum of n numbers using an
array
import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.println("How many numbers: ");
Scanner sc = new Scanner(System.in);
int num = Integer.parseInt(sc.nextLine());
int[] numbers = new int[num];
int sum=0;
for(int n=0;n<num;n++){
System.out.println("Enter number: ");
int x = Integer.parseInt(sc.nextLine());
numbers[n] = x;
}
for(int i = 0; i < numbers.length; i++) {
sum = sum + numbers[i];
System.out.println("Sum of all the elements of the array: " + sum);
4. Java program to implement linear search
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int[] numbers = {4,2,7,1,8,3,6};
int flag = 0;
System.out.println("Enter the number to be found out: ");
Scanner sc = new Scanner(System.in);
int x = Integer.parseInt(sc.nextLine());
for(int i=0;i<numbers.length;i++){
if (x==numbers[i]){
System.out.println("Successful search, the element is found at position "+ i);
flag = 1;
break;
if(flag==0){
System.out.println("Oops! Search unsuccessful");
5. Java program to check whether the given number is even
or odd
import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.println("Enter a number: ");
Scanner sc = new Scanner(System.in);
int number =Integer.parseInt(sc.nextLine());
int x = number%2;
if(x==0){
System.out.println("The number is Even");
else{
System.out.println("The number is Odd");
Output:
6. Java program to convert the temperature in Degree
Centigrade to Fahrenheit
import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.println("Enter temperature in Centigrade: ");
Scanner sc = new Scanner(System.in);
int c =Integer.parseInt(sc.nextLine());
float f = ((9*c)/5)+32;
System.out.println("Temperature in Fahrenheit is: "+f);
Output:
7. Java program to find the area of a triangle whose three sides
are given
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter the 1st side:");
int a= sc.nextInt();
System.out.println("Enter the 2nd side:");
int b= sc.nextInt();
System.out.println("Enter the 3rd side:");
int c= sc.nextInt();
if((a+b)>c && (a+c)>b && (b+c)>a)
double s=(a+b+c)/2.0;
double area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("Area of Triangle is: " + area);
else
System.out.println("Area of the triangle does not exist");
}
}
Output:
8. Java program to find out the average of a set of integers
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the count of numbers: ");
float count = Integer.parseInt(sc.nextLine());
int i = 0;
float sum = 0;
for(i=0;i<count;i++){
System.out.println("Enter an integer: ");
int x = Integer.parseInt(sc.nextLine());
sum = sum + x;
}
float avg = sum/count;
System.out.println("The average is: "+avg);
For Java Programs
https://pythonistaplanet.com/java-programming-exercises-with-solutions/