[go: up one dir, main page]

0% found this document useful (0 votes)
10 views14 pages

assignment1

The document contains a series of Java programming assignments that include tasks such as printing a welcome message, adding two numbers, converting temperatures, calculating the area and perimeter of a circle, checking for prime numbers, determining leap years, generating prime numbers within a range, calculating Fibonacci numbers, and converting between binary and decimal. Each question is accompanied by sample code demonstrating the solution. The assignments are designed to help users practice basic programming concepts in Java.

Uploaded by

Aditya Raj
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)
10 views14 pages

assignment1

The document contains a series of Java programming assignments that include tasks such as printing a welcome message, adding two numbers, converting temperatures, calculating the area and perimeter of a circle, checking for prime numbers, determining leap years, generating prime numbers within a range, calculating Fibonacci numbers, and converting between binary and decimal. Each question is accompanied by sample code demonstrating the solution. The assignments are designed to help users practice basic programming concepts in Java.

Uploaded by

Aditya Raj
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/ 14

ASSIGNMENT 1

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;

for (i = 2; i <= n; i++)


{

f[i] = f[i - 1] + f[i - 2];


}
}
return f[n];
}
public static void main(String args[])
{
int n;
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number: ");
n=sc.nextInt();
System.out.println("n-th Fibonacci number= "+fib(n));
}
}
QUESTION 10:
Write a program in Java to convert a Binary Number to Decimal and Decimal to
Binary.

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;
}

public static void main(String args[])


{
System.out.println("Decimal of 1010 is: "+getDecimal(1010));
System.out.println("Decimal of 10101 is: "+getDecimal(10101));
System.out.println("Decimal of 11111 is: "+getDecimal(11111));
}
}
/* decimal to binary */
public class Question10b
{
public static void toBinary(int decimal)
{
int binary[] = new int[40];
int index = 0;
while (decimal > 0)
{
binary[index++] = decimal % 2;
decimal = decimal / 2;
}
for (int i = index - 1; i >= 0; i--)
{
System.out.print(binary[i]);
}
System.out.println();
}

public static void main(String args[])


{
System.out.println("Binary of 10 is: ");
toBinary(10);
System.out.println("Binary of 21 is: ");
toBinary(21);
System.out.println("Binary of 31 is: ");
toBinary(31);
}
}

You might also like