You are on page 1of 6

(21BCE7761) K.

Narendranath Reddy
Java Assignment - 1

QUESTION:
1. A milk carton can hold 3.78 liters of milk. Each morning, a dairy farm ships
cartons of milk to a local grocery store. The cost of producing one liter of milk
is Rs. 50, and the profit of each carton of milk is Rs. 7. Write a java program
that does the following:
a. Prompts the user to enter the total amount of milk produced in the morning.
b. Outputs the number of milk cartons needed to hold milk.
c. Outputs the cost of producing milk.
d. Outputs the profit for producing milk.
Code : MAM YOU CAN COPY THIS CODE ,IT IS NOT A SCREENSHOT
import java.util.Scanner;
public class beginner {
public static void main(String[] args) {
//a. Prompts the user to enter the total amount of milk produced in
the morning.
System.out.println("enter the total amount of milk produced : ");
Scanner sc = new Scanner(System.in);
double milk_p = sc.nextDouble();
//b. Outputs the number of milk cartons needed to hold milk.
double milk_cartons = (int)(milk_p / 3.78);
System.out.print("No of cartons = ");
System.out.println(milk_cartons);
//c. Outputs the cost of producing milk.
double cost = milk_p * 50;
System.out.print("Cost of producing milk = ");
System.out.println(cost);
//d. Outputs the profit for producing milk.
double profit = milk_cartons * 7;
System.out.print("Profit = ");
System.out.println(profit);
}
}
OUTPUT :
(21BCE7761) K.Narendranath Reddy
Java Assignment - 1
2.
Code:
import java.util.Scanner;
import java.util.prefs.BackingStoreException;
public class beginner{
public static void main(String[] args) {
Scanner sin=new Scanner(System.in);
System.out.print("enter basic salary :");
int basic=sin.nextInt();
int gross;
int da;
int home;
int instaneous;
int travel;
int one_day;
if(basic>15000){
da=(basic*10/100);
travel=(basic*5/100);
home=(basic*7/100);;
instaneous=1500;
gross=da+travel+home+instaneous+basic;
System.out.print("gross salary :");
System.out.println(gross);}
else if(basic>10000 && basic<=15000){
da=(basic*7/100);
travel=(basic*3/100);
home=(basic*4/100);
one_day=(basic+da)/30;
instaneous=30*one_day;
gross=da+travel+home+instaneous+basic;
System.out.print("gross salary :");
System.out.print(gross);}
else if(basic<=10000){
travel=(basic*3/100);
one_day=basic/30;
instaneous=20*one_day;
gross=travel+instaneous+basic;
System.out.print("gross salary :");
System.out.println(gross);}

}
}
OUTPUT:
(21BCE7761) K.Narendranath Reddy
Java Assignment - 1
3. Write a java program to find the sum of the Exponential series given below
using recursion. Take x and n value from the user. 1+𝑥1!+𝑥22!+𝑥33!+⋯+𝑥𝑛𝑛!
CODE:
import java.util.Scanner;

public class beginner {


static double factorial(double n,double x){
double sum=1;
double fac =1;
double a;
if (x == 0) {
return 1; }
else
for(double i=1;i<=n;i++){
a=Math.pow(x,i);
fac=fac*i;
sum=sum+(a/fac);}
return sum;
}

public static void main(String[] args) {


Scanner sin=new Scanner(System.in);
System.out.print("enter the x value - ");
double b=sin.nextDouble();
System.out.print("enter the n value - ");
double d=sin.nextDouble();

System.out.println("sum of series "+factorial(d,b));


}
}
OUTPUT:
(21BCE7761) K.Narendranath Reddy
Java Assignment - 1
4. Write a java program to check if a number is perfect square or not.
CODE:
import java.util.Scanner;

public class beginner{


public static void main(String[] args) {
Scanner sin=new Scanner(System.in);
System.out.print("enter the number -");
int n=sin.nextInt();
int a;
a=(int) (Math.sqrt(n));
if(a*a==n) System.out.print(n+"--> "+ "perfect square");
else
System.out.println(n+"-->"+ "not a perfect square");

}
}

OUTPUT:
(21BCE7761) K.Narendranath Reddy
Java Assignment - 1
5. Write a java program to print and count the number of Krishnamurthy in a
range between a and b where a=100,b=400 using function int krisno(n,a,b). A
Krishnamurthy number is a number whose sum of the factorial of digits is equal to
the number itself.
CODE:
import java.net.SocketTimeoutException;
import java.sql.SQLOutput;
import java.util.Scanner;
public class beginner {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.println("enter the value of a =");
int a = sc.nextInt();
System.out.println("enter the value of b =");
int b = sc.nextInt();
System.out.println(krisno(a,b));
}
public static int krisno(int a,int b){
int modnum;
int c = 0;
int sum ;
for (int i=a+1;i<b;i++){
sum = 0;
modnum = i;
while(modnum>0){
sum = sum + fact(modnum%10);
modnum = modnum/10;

}
if(sum ==i)
c = c+1;
}
return c;
}
public static int fact(int m){
int fac = 1;
if(m==1 && m==0)
return 1;
else{
for(int i=1;i<=m;i++)
fac = fac*i;
return fac;
}

}
}
(21BCE7761) K.Narendranath Reddy
Java Assignment - 1
output:

You might also like