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

Final

Uploaded by

Khaled Al zaky
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)
16 views5 pages

Final

Uploaded by

Khaled Al zaky
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/ 5

1.

Class Add Two Integers


public class AddTwoIntegers {
public static void main(String[] args) {
int first = 10;
int second = 20;
System.out.println("Enter two numbers: " + first + " " + second);
int sum = first + second;
System.out.println("The sum is: " + sum);
}
}

The output is :

Enter two numbers: 10 20

The sum is: 30

2. Class Multiply Two Numbers


public class MultiplyTwoNumbers{
public static void main(String[] args){
float first = 1.5f;
float second = 2.0f;
float product = first * second;
System.out.println("The product is: " + product);
}
}

The output is :

The product is: 3.0

3. Class for Sum of Natural


public class SumNatural{
public static void main(String[] args){
int num = 10, sum = 0;
for(int i = 1; i <= num; ++i)
{
// sum = sum + i;
sum += i;
}
System.out.println("Sum = " + sum);
}
}

The output is :

Sum = 55

1. Explain the main work/function of the following


program: [10 Marks]
import java.util.Scanner;
class Main {
public static void main(String[] args) {
char operator;
Double number1, number2, result;
// create an object of Scanner class
Scanner scanner = new Scanner(System.in);
System.out.print("Enter operator (either +, -, * or /): ");
// ask user to enter operator
operator = scanner.next().charAt(0);
System.out.print("Enter number1 and number2 respectively: ");
// ask user to enter numbers
number1 = scanner.nextDouble();
number2 = scanner.nextDouble();
switch (operator) {
// performs addition between numbers

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.

2. Find out the output of the following code:


class Main {

public static void main(String[] args) {

int [] numbers = {1, 2, 3, 4, 5, 6};

int [] positiveNumbers = numbers; // copying arrays

for (int number: positiveNumbers) {

System.out.print(number + ", ");


}
}
}

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;

for (int i = 1; i <= 5; i++) {


System.out.print("enter positive value: ");
int num = input.nextInt(); // allow the user to enter value

if (num <= 0) {
continue; // this will skip the loop if the condition is not fulfilled
}

sum += num;
}

System.out.println("result of summation is: " + sum);


}
}

Find out the output of the following code, and


explain your answer. [10 Marks]
public class Array {
public static void main(String[] args) {
int[] anArray; // declare an array of integers

anArray = new int[10]; // create an array of integers

// assign a value to each array element and print


for (int i = 0; i < anArray.length; i++) {
anArray[i] = i;
System.out.print(anArray[i] + " ");
}
System.out.println();
}
}

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 {

public static void main(String[] args) {

// create a 2d array

int[][] a = {

{1, -2, 3},

{-4, -5, 6, 9},

{7},

};

// first for...each loop access the individual array

// inside the 2d array

for (int[] innerArray: a) {

// second for...each loop access each element inside the row

for(int data: innerArray) {

System.out.println(data);
}
}
}
}

The output is :
1
-2
3
-4
-5
6
9
7

You might also like