0% found this document useful (0 votes)
16 views4 pages

Exercise

The document outlines five programming exercises in Java, including calculating the sum and average of three numbers, converting Fahrenheit to Celsius, generating five random numbers, creating a guessing game, and using an enhanced for loop to process five numbers. Each exercise is accompanied by sample Java code demonstrating the required functionality. The solutions utilize basic Java constructs such as loops, conditionals, and user input handling.

Uploaded by

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

Exercise

The document outlines five programming exercises in Java, including calculating the sum and average of three numbers, converting Fahrenheit to Celsius, generating five random numbers, creating a guessing game, and using an enhanced for loop to process five numbers. Each exercise is accompanied by sample Java code demonstrating the required functionality. The solutions utilize basic Java constructs such as loops, conditionals, and user input handling.

Uploaded by

nattybra2007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exercise

1. Write a program that accepts three numbers from the keyboard and display its sum and
average?
2. Write a program that accept temperature in Fahrenheit from the keyboard and display its
equivalent temperature in Celsius? Formula: tempc = (tempf - 32)*5/9;
3. Write a program that display 5 random numbers from 3 to 10.
4. Write a java program that accepts your name from the keyboard to start guessing a 10
random number in the range 1 up to 10. Then the program should display the message
“WINNER” with the scored values by including the accepted user name if the user scored
greater than 5; otherwise, the message “LOSER” will be display with the scored values by
displaying the name of the user. further the program should display the list of random
number to the user.
5. Write program that accepts 5 number from the keyboard and display each individual
element, sum and average of the numbers using enhanced for loop
Solution:

1.
import [Link];
public class SumAverage {
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
double n1,n2,n3,sum=0.0,average;
[Link]("Enter three numbers:");
n1=[Link]();
n2=[Link]();
n3=[Link]();
sum=n1+n2+n3;
average=sum/3;
[Link]("Sum:"+sum+" Average:"+average);
}
}
2.
import [Link];
public class TemperatureConversion {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
double tempf,tempc;
[Link]("Enter Temperature in fahreneit:");
tempf=[Link]();
tempc=(tempf-32)*5/9;
[Link]("Temperature in Celsius:"+tempc);
}
}
3. //to compute the random number in some range use the
formula:(([Link]()*(Max-Min+1)+Min)
public class FiveRandomNumber {
public static void main(String[] args) {
int rn;
for (int i=1;i<=5;i++)
{ rn=(int)([Link]()*(8)+3);
[Link](rn);
}
}
}
4. // in the program in each iteration of the loop the random numbers are generated
import [Link];
public class GuessGame {
public static void main(String[] args) {
[Link]("WELCOME TO THIS GUESS GAME ");
[Link]("===========================");
Scanner sc=new Scanner([Link]);
String player_name;
[Link]("Enter your Name to play the game:");
player_name=[Link]();
int randlist[]=new int[10];
int counter=0,random_num,user_num;
for (int i=0;i<10;i++)
{[Link]("Enter a Number to Guess in the range 1 to 10:");
user_num=[Link]();
random_num=(int)([Link]()*(10)+1);
randlist[i]=random_num;
if(user_num==random_num)
counter++;
}
if(counter>=5)
{
[Link](player_name+"Congradualtion you Are the Winner");
[Link]("Your Score is:"+counter);
}
else
{[Link](player_name+"Sorry you are the LOSER");
[Link]("your score is:"+counter);
}
[Link]("Here are the list of random Numbers generated by the
computer");
for(int i=0;i<=9;i++)
[Link](randlist[i]+" ");

}
}
5.
import [Link];
public class Enhanedloop {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int num[]=new int[5];
[Link]("Enter 5 numbers:");
double sum=0.0;
for(int x:num)
{ x=[Link]();
sum=sum+x;
}
double average=sum/5;

[Link]("Sum:"+sum+"Average="+average);

}
}

You might also like