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