assignment1
assignment1
QUESTION 1:
Write a Java program to accept the user name of the user and then print welcome
message.
INPUT:
import java.util.*;
public class Question1
{
public static void main(String args[])
{
String s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name");
s=sc.nextLine();
System.out.println("Welcome "+s);
}
}
QUESTION 2:
Write a program in Java to accept two numbers from the user and then add and
print the sum.
INPUT:
import java.util.*;
public class Question2
{
public static void main(String args[])
{
Scanner input =new Scanner(System.in);
System.out.print("Enter number1: ");
int num1=input.nextInt();
System.out.println("Enter number2: ");
int num2=input.nextInt();
int sum= num1+num2;
System.out.println( );
System.out.println("Sum: "+sum);
}
}
QUESTION 3:
Write a program in Java to convert temperature from Celsius to Fahrenheit scale.
INPUT:
import java.util.*;
public class Question3
{
public static void main(String args[])
{
float Fahrenheit,Celsius;
Celsius=15;
Fahrenheit=((Celsius*9)/5)+32;
System.out.println( );
System.out.println("Fahrenheit="+Fahrenheit);
}
}
QUESTION 4:
Write a program in Java to find the area and perimeter of a circle where radius is
taken from user.
INPUT:
import java.util.*;
public class Question4
{
public static void main(String args[])
{
int r;
System.out.println("Enter a number for radius:");
Scanner sc=new Scanner(System.in);
r=sc.nextInt();
System.out.println("Radius="+r);
double perimeter,area;
area=3.14*r*r;
perimeter=2*3.14*r;
System.out.println("Perimeter of circle= "+perimeter);
System.out.println("Area of circle= "+area);
}
}
QUESTION 5:
Write a program in Java to check whether a number is prime or not.
INPUT:
import java.util.*;
public class Question5
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a,j;
System.out.println("Enter a number: ");
a=sc.nextInt();
for(j=2;j<=a;j++)
{
if(a%j==0)
break;
}
if(j<a)
System.out.println(a+" is a prime number");
else
System.out.println(a+" is not a prime number");
}
}
QUESTION 6:
Write a program in Java to check whether a year is leap year or not.
INPUT:
import java.util.*;
public class Question6
{
public static void main(String args[])
{
int year;
Scanner sc=new Scanner(System.in);
System.out.println("Enter year: ");
year=sc.nextInt();
boolean leap= false;
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
leap=true;
else
leap=false;
}
else
leap=true;
}
else
leap=false;
if(leap)
System.out.println(year+" is a leap year.");
else
System.out.println(year+"is not a leap year.");
}
}
QUESTION 7:
Write a program in Java to generate the prime numbers between a range (user
input).
INPUT:
import java.util.*;
public class Question7
{
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("Enter upper bound of the interval: ");
b = sc.nextInt();
System.out.printf("Prime numbers between %d and %d are: ",a, b);
for (i = a; i <= b; i++)
{
if (i == 1 || i == 0)
continue;
flag = 1;
for (j = 2; j <= i / 2; ++j)
{
if (i % j == 0)
{
flag = 0;
break;
}
}
if (flag == 1)
System.out.println(i);
}
}
}
QUESTION 8:
Write a program in Java to print n-th Fibonacci number where n is the user input.
[Fin(n)=Fib (n-1) + Fib(n-2)]
INPUT:
import java.util.*;
public class Question8
{
static int fib(int n)
{
int f[] = new int[n + 1];
int i;
f[0] = 0;
if (n > 0)
{
f[1] = 1;
INPUT:
/* binary to decimal */
public class Question10
{
public static int getDecimal(int binary)
{
int decimal = 0;
int n = 0;
while(true)
{
if(binary == 0)
{
break;
} else
{
int temp = binary%10;
decimal += temp*Math.pow(2, n);
binary = binary/10;
n++;
}
}
return decimal;
}