I am new to java and have this course work to complete can you assist me with the codes pleas

I need to write a program that helps me perform basic math tasks, working out factorials, powers and primes. Start by printing the menu

Welcome to the Math Calculator
Pick the problem you want help with.
a) factorial
b) powers
c) prime number
q) Quit
Enter your choice:
and read in their choice as a character.
If the user selects (a), I need to prompt the user for a number

Enter the factorial: this is the factorial you are to work out. You obtain the factorial value by multiplying every number between 1 and X together, e.g.

factorial of 4 is 1 * 2 * 3 * 4
when you should print the answer in the form:-

The factorial of X is XXXXX

if the user selects (b), you are to prompt the user for 2 numbers, the value and the power to raise it to.

Enter the value:
Enter the power:

So 2 to the power of three, or 2 cubed is

2 * 2 * 2

and then print the answer in the form a to the power of b is c

If the user enters c you are to prompt them to enter a number

Enter a number to prime check:

and then test to see if is a prime, prime numbers are those which are only divisible by 1 and themselves

e.g. 3, 5, 7, 11, 13
you are then to print

x is prime or x is not prime depending.

If the user enters q you are to quit the program using System.exit(0);

Recommended Answers

All 6 Replies

You will need a pretty good understanding of how to use I/O in java to do this assignment. Take a look at the java API in the java.io package and I'm sure there are some tutorials on the sun website about I/O. The rest of the program is just a series of if-else statements.

Have a try and if you are still having problems post some of your code for us to have a look at. We can then try to steer you in the right direction. :)


factorialof 5

public class afact

public static void main (String args[])
int k=5,ans;
for(int i=4;i>0;i--)



can anyone help me to construct the code:
i dont know how to make the code of entering the operator ,the function of the operator when it was input and the output of the result .
limit of the result is 500,

Enter First Number:2
Enter Second Number:6
A=+ || a=+ S=- || s=-
M=* || m=* D=/ || d=/
Choose Operator:A ||a

commented: Posting in the wrong areas is one thing, but the post is unclear and thus useless. I'd like to help yet can't answer something I can't understand. +0
Member Avatar

micial30: Please don't post your problem in someone else's thread.

Kreative: Please show us your work or at least demonstrate you have put thought into doing it.

Kreative, your program is relatively simple...
The hardest bit is getting to know how to work with java really.
The easiest way is to create submethods:
One for each calculation, so start by creating a class (preferably with a logical name and build up the methods). You also need to choose if your working with whole numbers... Because it completely changes your code.
Here's a base:

public class MyCalculator
public static int fact(int n)
Here you just use a while or a for loop. The for is generally easier:
int result=1;
for(int i=1;i<=n;i++)
return result;
public static double power(double d, int n)
//Here it's the same idea: (although there already is a method in Math called Math.pow("base","power")
I'm not going to type it, but the easiest way would be an if to see if the power is positive or negative (n>0 || n<0) and then a loop for the calculations.
// for the prime number you have to make a loop which starts at 2 and then you take the number which was given as an argument and use % which returns the rest of your devision. If the rest is equal to 0 it's divisible, otherwise not.

//Quitting should be part of your main method, I'd use a boolean to verify if the user wants to continue. So you use:
boolean check=true;
//your main here.

//In your main however you need a scanner to take in the next line typed. The syntax looks like this:
Scanner sc = new Scanner(System.in); //System.in is your java default inputstream.
then you use sc.next() to acquire the next char typed.
after that you have to convert it to an integer or double if necessary.

Hope this helps you on your way!

OP is from 2007 guys, Unless he is terribly slow, I would assume he has gone past this problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.