vendredi 29 juillet 2016

How can I input an if statement inside a while loop?

import java.util.Scanner;
public class ex11 
{
static Scanner type=new Scanner(System.in);
public static void main(String args[])
{
    int fact=1;
    System.out.println("Enter a natural number ");
    int num=type.nextInt();

    int i=1;
    while(i<=num)
    {
        fact*=i;
        i++;    
    }
    System.out.println("Factorial of number " + num + " is " + fact);
}

}

/* I'm trying to place a conditional statement inside of the while loop. The condition to test for would be that of, if num is a negative number, S.O.P.("You entered a negative #"); in other words, if(num<0) S.O.P.("You entered a negative #"); however it doesn't print it properly. */

Aucun commentaire:

Enregistrer un commentaire