Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]

Similar presentations


Presentation on theme: "Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]"— Presentation transcript:

1 Computer Programming Lab 9

2 Exercise 1

3 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int sumOfPositive=0; int sumOfNegative=0,sum=0; double count=0; System.out.print( "Enter an integer, the input ends with 0:"); int number=input.nextInt(); if (number==0) System.out.println( "No numbers entered except 0 " ); else{

4 while(number!=0) { count++; sum+=number; if(number>0) sumOfPositive++; else sumOfNegative++; number=input.nextInt(); } System.out.println( "The number of positives is " + sumOfPositive); System.out.println( "The number of negatives is " + sumOfNegative); System.out.println( "The total is " +sum); System.out.println( "The average is " +sum/count); } }

5 Exercise 2

6 Source Code package javaapplication4; import java.util.Scanner; public class JavaApplication4 { public static void main(String[ ] args) { Scanner input = new Scanner(System.in); System.out.print( "Enter positive integer greater than or equal 2: " ); int number= input.nextInt(); System.out.println( "The factors of “ +number+ " are :" ); int factor=2; do{ if(number%factor==0) { System.out.printf( "%d, ",factor); number/=factor; } else factor++; }while(factor<=number); }}

7 Exercise 3

8 Source Code : package exercise3; import java.util.Scanner; public class Exercise3 { public static void main(String[] args) { Scanner input=new Scanner (System.in); System.out.print( "Enter the number of students: " ); int numOfStudent= input.nextInt(); System.out.print( "Enter student first name only: " ); String student1= input.next(); System.out.print( "Enter a student score: " ); double score1= input.nextDouble();

9 for(int i=0 ; i<numOfStudent-1 ; i++) { System.out.print( "Enter student first name only: " ); String student= input.next(); System.out.print( "Enter a student score: " ); double score= input.nextDouble(); if(score>score1) { student1=student; score1=score; } System.out.println( "Top student " +student1+" 's score is " +score1); }


Download ppt "Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]"

Similar presentations


Ads by Google