Java Programming for IT 802
Java Programming for IT 802
1. Write down a program in Java to accept the two numbers from user and accept the
operator (+, -, * , / ) from user and print the resultant value. Make this program with
the help of if-else.
Solution:
import java.util.*;
public class JavaCalc {
public static void main(String[] args) {
Scanner input=new Scanner(System.in)
int fnum, snum, ans;
char sign; //THIS ASSIGNS THE CHARACTER VARIABLE + - * /
System.out.println("Welcome User,This is a Simple Calculator Created
by Victor Using the if statement");
System.out.print("Please Enter your first digit: ");
fnum=input.nextInt(); /*This receive input from user
it receive the first number*/
System.out.print("Enter the second digit: ");
snum=input.nextInt();/*This receive input from user
it receive the second number*/
System.out.print("Enter the mathematical operator to be used +-*/: ");
sign=input.next().charAt(0); //Receive input from user
if (sign == '+' ){
ans=fnum + snum;
System.out.println(fnum +" "+sign +" "+snum +"= "+" "+ ans );
}
else if(sign == '-')/* else if statement for minus sign*/
{
ans=fnum-snum;
System.out.println(fnum +" "+sign +" "+snum +"= "+" "+ ans );
}
else if(sign == '/'){
ans=fnum/snum;
System.out.println(fnum +" "+sign +" "+snum +"= "+" "+ ans );
}
else if(sign == '*'){
ans=fnum*snum;
System.out.println(fnum +" "+sign +" "+snum +"= "+" "+ ans );
}
else
System.out.println("Your Input is not correct,please check it for any
error(s).");
}
}
2. Write down a program in Java to print the number from 1 to 100 by the use of for
loop.
Solution:
public class ForLoop{
public static void main(String[ ] args) {
//Code of Java for loop
for(int i=1;i<=100;i++)
{
System.out.println(i);
}
}
}
3. Write down a program in java to show the use of switch and case in Java
Programming.
Solution:
public class Main {
public static void main(String[] args) {
int expression = 2;
// switch statement to check size
switch (expression) {
case 1:
System.out.println("Case 1");
// matching case
case 2:
System.out.println("Case 2");
case 3:
System.out.println("Case 3");
default:
System.out.println("Default case");
}
}
}
4. Write down a program in java to print number from 100 to 1 with the help of
while loop in Java Programming.
Solution:
public class Whileloop {
public static void main(String args[])
{ int i=100;
System.out.println("Output is=");
while (i>=1)
{
System.out.println(i);
i--;
}
}
}
5. Write down a program in Java to accept the two numbers from user and accept the
operator (+, -, *, / ) from user and print the resultant value. Make this program with the
help of switch and case.
Solution:
import java.util.*;
public class JavaSwtichCase {
public static void main(String args[])
{
double num1, num2;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number:");
num1 = scanner.nextDouble();
System.out.print("Enter second number:");
num2 = scanner.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
scanner.close();
double output;
switch(operator)
{
case '+':
output = num1 + num2;
break;
case '-':
output = num1 - num2;
break;
case '*':
output = num1 * num2;
break;
case '/':
output = num1 / num2;
break;
default:
System.out.printf("You have entered wrong operator");
return;
}
6. Write down a program in Java Programming to print the Pyramid with symbol.
Example: *
**
***
****
*****
Solution:
public class Pyramid {