0% found this document useful (0 votes)
2 views3 pages

PR3 Methods 2

The document contains a Java program that includes methods for calculating factorials, checking prime numbers, displaying the first 50 prime numbers, and calculating the sum and average of user-inputted numbers. The main method orchestrates these functionalities by prompting the user for input and displaying the results. The program demonstrates basic mathematical operations and user interaction through the console.

Uploaded by

Sarthak Anandkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

PR3 Methods 2

The document contains a Java program that includes methods for calculating factorials, checking prime numbers, displaying the first 50 prime numbers, and calculating the sum and average of user-inputted numbers. The main method orchestrates these functionalities by prompting the user for input and displaying the results. The program demonstrates basic mathematical operations and user interaction through the console.

Uploaded by

Sarthak Anandkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Code:

package [Link];
import [Link];

public class MethodPrograms {

//Method to find factorial


public static long factorial(int n)
{
long fact = 1;
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
return fact;
}

// Method to check prime number


public static boolean isPrime(int num) {
if (num < 2)
return false;

for (int i = 2; i <= [Link](num); i++) {


if (num % i == 0)
return false;
}
return true;
}

// Method to display first 50 prime numbers


public static void displayPrimes() {
int count = 0, number = 2;

[Link]("First 50 Prime Numbers:");

while (count < 50) {


if (isPrime(number)) {
[Link](number + " ");
count++;
}
number++;
}
[Link]();
}

// Method to calculate sum and average


public static void sumAndAverage(Scanner sc) {
[Link]("Enter how many numbers: ");
int n = [Link]();

double sum = 0;

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


[Link]("Enter number " + i + ": ");
sum += [Link]();
}

double avg = sum / n;

[Link]("Sum = " + sum);


[Link]("Average = " + avg);
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);

// Factorial
[Link]("Enter number for factorial: ");
int num = [Link]();
[Link]("Factorial = " + factorial(num));

// Prime numbers
displayPrimes();

// Sum and average


sumAndAverage(sc);

[Link]();
}
}
Output:
Enter number for factorial: 4
Factorial = 24
First 50 Prime Numbers:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107
109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223
227 229
Enter how many numbers: 5
Enter number 1: 5
Enter number 2: 3
Enter number 3: 4
Enter number 4: 2
Enter number 5: 11
Sum = 25.0
Average = 5.0

You might also like