17BCE0159 Ricky Sabharwal CSE1007 - Java Programming
17BCE0159 Ricky Sabharwal CSE1007 - Java Programming
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.*;
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);
}
}
}