Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Lab assignment 1:-

Question. Write Java statements that accomplish each of


the following tasks:

1. Display the message "Enter an integer: ", leaving the


cursor on the same line.

2. Assign the product of variables b and c to the int variable


a.

3. Use a comment to state that a program performs a


sample payroll calculation.

Program:

public static void main(String[] args)

System.out.print("Enter an integer:");

int a = b * c;

// A program performs a sample payroll calculation

Question 2: Write a program that inputs five numbers and


determines and prints the number of negative numbers
input, the number of positive numbers input and the number
of zeros.

Code:

import java.util.Scanner;

public class JavaExercise2.32 {

public static void main(String[] args) {

Scanner value = new Scanner (System.in);

//Declare variables
int num1;
int num2;
int num3;
int num4;
int num5;

//Reset Number of positive, negative and zeros


int numPositive = 0;
int numZero = 0;
int numNegative = 0;

//Ask users for inputs


System.out.print ("Enter your first number: ");
num1 = value.nextInt();

System.out.print ("Enter your second number: ");


num2 = value.nextInt();

System.out.print ("Enter your third number: ");


num3 = value.nextInt();
System.out.print ("Enter your fourth number: ");
num4 = value.nextInt();

System.out.print ("Enter your fifth number: ");


num5 = value.nextInt();

//Count Positve Numbers

if (num1 > 0)
numPositive = numPositive + 1;
if (num2 > 0)
numPositive = numPositive + 1;
if (num3 > 0)
numPositive = numPositive + 1;
if (num4 > 0)
numPositive = numPositive + 1;
if (num5 > 0)
numPositive = numPositive + 1;

//Count Negative Numbers

if (num1 < 0)
numNegative = numNegative + 1;
if (num2 > 0)
numNegative = numNegative + 1;
if (num3 > 0)
numNegative = numNegative + 1;
if (num4 > 0)
numNegative = numNegative + 1;
if (num5 > 0)
numNegative = numNegative + 1;

//Count Zeros

if (num1 == 0)
numZero = numZero + 1;
if (num2 == 0)
numZero = numZero + 1;
if (num3 == 0)
numZero = numZero + 1;
if (num4 == 0)
numZero = numZero + 1;
if (num5 == 0)
numZero = numZero + 1;

System.out.println ("\n");

//Displays the number of Positive, Negative and Zeros

System.out.printf ("Positive numbers = %d\n", numPositive);


System.out.printf ("Negative numbers = %d\n", numNegative);
System.out.printf ("Zero numbers = %d\n", numZero);

Question 3. write an application that calculates the squares


and cubes of the numbers from 0 to 10 and prints the
resulting values in table format, as shown below.

Code:
public class Squarecube {

/**

* @param args the command line arguments

*/

public static void main (String [] args)

int number;

int square;

int cube;

System.out.println("Number\tSquare\tCube");

for (number=1; number<10; number++);

square= number * number;//

cube= number * square;


System.out.println(number+"\t"+square+"\t"+cube);

// TODO code application logic here

You might also like