You must write one Main class containing a main method. For each question, excep
ID: 670941 • Letter: Y
Question
You must write one Main class containing a main method. For each question, except the last, you must write another method in your Main class. Your main method must show each of your others methods running on some sensible example values. Comment your methods appropriately. Submit your code, together with the results of running it on sensible example values. Write a method that takes an integer argument N (assume N is positive), and returns the smallest positive integer, M, such that 1+2+3+...+M > N. ( PLEASE USE JAVA)
Explanation / Answer
import java.util.Scanner;
public class Main{
public static int smallPI(int n){
int m=0,i=1;
for(i=1;i<=n;i++){
m=m+i;
if(m>n){
break;
}
}
return i;
}
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number: ");
int n=scan.nextInt();
System.out.println("Smallest positive integer such that 1+2+3...+m>n: "+smallPI(n));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.