Some Java programs part 4
16. Write a program that will read 10 numbers from the user, and then print the first number, the sum of the first 2 numbers, first 3 numbers, and so on up to the sum of 20 numbers.
/*Time : 5:00pm * Author: Anik das*/ import java.util.Scanner; public class Faraz16{ public static void main ( String [] args ){ int c=1, sum=0, number; Scanner input = new Scanner ( System.in ); while (c<=10){ System.out.println("Enter Number #"+c); number = input.nextInt(); sum=sum+number; System.out.println("Sum Of 1st "+c+" Number(s) is : "+sum); c++; } } }
17. Write a program that reads a list of numbers, and prints out the product of all the numbers read. You may assume that the user first inputs the total number of numbers. For example, if the first input is 4, then the program has to read in four numbers from the user, and print out the product of these four numbers. Please do not assume anything other than what you have been directly told, and what you know to be mathematical facts.
/*Time : 5:08pm * Author: Anik das*/ import java.util.Scanner; public class Faraz17{ public static void main ( String [] args ){ int c=1, goon=1, number; Scanner input = new Scanner ( System.in ); System.out.println("Enter Total Number Of your Numbers"); int n = input.nextInt(); while (c<=n){ System.out.println("Enter Number #"+c); number = input.nextInt(); goon=goon*number; c++; } System.out.println("Product Of your "+n+" Number(s) Is : "+goon); } }
18. Write a program that print the following sequence of values in loops:
-10, -5, 0, 5, 10, 15, 18, 27, 36, 45, 54
/* Time: 5:18pm * Author: Anik Das * Question: Write a program that print the following sequence of values in loops: * -10, -5, 0, 5, 10, 15, 18, 27, 36, 45, 54*/ public class Faraz18{ public static void main ( String [] args ){ int n = -10; while (n<=15){ System.out.println(n); n=n+5; } n=18; while (n<=54){ System.out.println(n); n=n+9; } } }
19. Write a program that reads a number N, and prints out the sum of all odd numbers from 1 to N inclusive. For instance, if the input is 6, the output for the program should be 9.
/* time : 5:28pm * Author: Anik Das * Question: Write a program that reads a number N, * and prints out the sum of all odd numbers from 1 to N inclusive. * For instance, if the input is 6, the output for the program should be 9.*/ import java.util.Scanner; public class Faraz19{ public static void main ( String [] args ){ Scanner input = new Scanner ( System.in); int n, sum=0, c=1; System.out.println("Enter Your Desired Number"); n = input.nextInt(); while (c<=n){ if (c%2!=0){ sum = sum+c; } c++; } System.out.println("Sum Of the ODD Number(s) from 1 to "+n+" Is : "+sum); } }
20. Write a program that reads marks of ten courses and prints the maximum, minimum and average of those ten marks.
/*Time: 5.20pm * Author: Anik Das*/ import java.util.Scanner; public class Faraz20{ public static void main (String [] args){ Scanner input = new Scanner(System.in); int a=0, max=0, min=0, c=1, d=2; System.out.println("Please enter Mark for Course 1"); a= input.nextInt(); max=a; min=a; while (c<=9){ System.out.println("Please enter Mark for Course "+d); a= input.nextInt(); if (a>=max){ max=a; c=c+1; d=d+1; }else{ if (a<=min){ min=a; c=c+1; d=d+1; }else{ c=c+1; d=d+1; } } } System.out.println("MAXIMUM MARK : "+max); System.out.println("MINIMUM MARK : "+min); } }