0% found this document useful (0 votes)
49 views5 pages

Java Programming Exercises

This document contains 3 Java programs written by Ricky Sabharwal for their CSE1007 - Java Programming course. The first program calculates BMI based on user-input weight in kg and height in cm. The second program generates the Fibonacci series up to a number entered by the user. The third program prints all prime numbers within a range of numbers input by the user.

Uploaded by

ricky sabharwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views5 pages

Java Programming Exercises

This document contains 3 Java programs written by Ricky Sabharwal for their CSE1007 - Java Programming course. The first program calculates BMI based on user-input weight in kg and height in cm. The second program generates the Fibonacci series up to a number entered by the user. The third program prints all prime numbers within a range of numbers input by the user.

Uploaded by

ricky sabharwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

17BCE0159

Ricky Sabharwal
CSE1007 – Java Programming

Q1. Find BMI of a person by getting weight and height in kg and cm respectively from
user. [Formula BMI = kg/m2]

import java.util.Scanner;
public class Bmi_17BCE0159 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Input weight in kilogram: ");
double weight = sc.nextDouble();
System.out.System.out.println();("Input height in cm: ");
double height = sc.nextDouble();
height=height/100;
double BMI = weight / (height * height);
System.out.println(BMI + " kg/m2");
}
}
Q2. Generate Fibonacci Series by getting n value from user.

import java.util.*;

public class Fibo_17BCE0159


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = 1,b=1;
int c;
if(n==1)
System.out.print(a+" ");
if(n==2)
System.out.print(a+" "+b);
System.out.print(a+" "+b+" ");
for(int i=2;i<n;i++)
{
c=a+b;
System.out.print(c+" ");
a=b;
b=c;
}
}
}
Q3. Print all prime numbers in a given range.

import java.util.*;
public class Prime_17BCE0159{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a, b, i, j,flag;
System.out.printf( "Enter lower bound of the interval: ");
a = sc.nextInt();
System.out.printf( "\nEnter upper bound of the interval: ");
b = sc.nextInt();
System.out.printf("\nPrime numbers between %d and %d are: ", a, b);
if (a == 1) {
System.out.println(a);
a++;
if (b >= 2) {
System.out.println(a);
a++;
}
}
if (a == 2)
System.out.println(a);
if (a % 2 == 0)
a++;
for (i = a; i <= b; i = i + 2) {
flag = 1;
for (j = 2; j * j <= i; ++j) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1)
System.out.println(i);
}

}
}

You might also like