Final
Final
The output is :
The output is :
The output is :
Sum = 55
case '+':
result = number1 + number2;
System.out.print(number1 + "+" + number2 + " = " + result);
break;
// performs subtraction between numbers
case '-':
result = number1 - number2;
System.out.print(number1 + "-" + number2 + " = " + result);
break;
// performs multiplication between numbers
case '*':
result = number1 * number2;
System.out.print(number1 + "*" + number2 + " = " + result);
break;
// performs division between numbers
case '/':
result = number1 / number2;
System.out.print(number1 + "/" + number2 + " = " + result);
break;
default:
System.out.println("Invalid operator!");
break;
}
}
}
This program is working as calculator it will ask the user to enter an operator and enter two
numbers and the program will solve the equation using switch method.
The output is :
1, 2, 3, 4, 5, 6,
Write a Java program that calculates the sum of 5 positive numbers entered by the user. If
the user
enters a negative number or zero, it is skipped from the calculation. You need to use FOR
LOOP & IF Statement.
import java.util.Scanner;
public class sum {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sum = 0;
if (num <= 0) {
continue; // this will skip the loop if the condition is not fulfilled
}
sum += num;
}
first we have declared an array, then we have assigned length to it, then use the loop to give
every element in the array value, it will be from 0 to 9 because i starts from 0 and end at
anArray.length – 1, inside the loop the phrase System.out.print(anArray[i] + " " will print out
the element value followed by space, and the output will be:
0123456789
2. What is the output of the following code:
class MultidimensionalArray {
// create a 2d array
int[][] a = {
{7},
};
System.out.println(data);
}
}
}
}
The output is :
1
-2
3
-4
-5
6
9
7