cut out even numbers - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

cut out even numbers

rootroot Posts: 103Member
i have this java program to calculate factors up to 20. i need to modify it so that even numbers are not calculated. whats the best method??
thanks, ed.



public class fator {

public static void main(String[] args) {
int factValue;
int i;

System.out.println("Java Factor Program by Edwin Rhodes");
System.out.println("Copyright (C) Edwin Rhodes 2006-2007");
System.out.println("All Rights Reserved.");
System.out.println("
");

for ( i=1; i<21; i++ ) {
factValue = fator.fact(i); // factValue contains factorial of i.
System.out.print("The Value For Factor: ");
System.out.print(i);
System.out.print("is: ");
System.out.print(factValue);
System.out.print("
");
}


}

public static int fact(int someInt) {
int i = 1;
if (someInt > 1)
i = someInt*fact(someInt-1);
return i; // Returns value as result of function
}
}


// End of java program
//

Comments

  • zibadianzibadian Posts: 6,349Member
    : i have this java program to calculate factors up to 20. i need to modify it so that even numbers are not calculated. whats the best method??
    : thanks, ed.
    :
    :
    :
    : public class fator {
    :
    : public static void main(String[] args) {
    : int factValue;
    : int i;
    :
    : System.out.println("Java Factor Program by Edwin Rhodes");
    : System.out.println("Copyright (C) Edwin Rhodes 2006-2007");
    : System.out.println("All Rights Reserved.");
    : System.out.println("
    ");
    :
    : for ( i=1; i<21; i++ ) {
    : factValue = fator.fact(i); // factValue contains factorial of i.
    : System.out.print("The Value For Factor: ");
    : System.out.print(i);
    : System.out.print("is: ");
    : System.out.print(factValue);
    : System.out.print("
    ");
    : }
    :
    :
    : }
    :
    : public static int fact(int someInt) {
    : int i = 1;
    : if (someInt > 1)
    : i = someInt*fact(someInt-1);
    : return i; // Returns value as result of function
    : }
    : }
    :
    :
    : // End of java program
    : //
    :
    :
    Add an if-statement to the loop, which checks if i is divisable by 2 (hint: use modulo).
  • rootroot Posts: 103Member
    hello i need to modify this program to alternate between skipping the factorial of one even and one odd number ie: 1! 3! 6! ETC

    // Start of java program
    // by edwin rhodes
    //


    public class fator {

    public static void main(String[] args) {
    int factValue;
    int i;

    System.out.println("Java Factor Program by Edwin Rhodes");
    System.out.println("Copyright (C) Edwin Rhodes 2006-2007");
    System.out.println("All Rights Reserved.");
    System.out.println("
    ");

    for ( i=1; i<21; i++ ) {
    factValue = fator.fact(i); // factValue contains factorial of i.
    System.out.print("The Value For Factor: ");



    if ( ( i % 2 ) == 0 )
    {
    System.out.println("Even!");
    }

    else
    {

    System.out.print(i);
    System.out.print("is: ");
    System.out.print(factValue);
    System.out.print("
    ");
    }




    }


    }

    public static int fact(int someInt) {
    int i = 1;
    if (someInt > 1)
    i = someInt*fact(someInt-1);
    return i; // Returns value as result of function
    }
    }


    // End of java program
    //


  • zibadianzibadian Posts: 6,349Member
    : hello i need to modify this program to alternate between skipping the factorial of one even and one odd number ie: 1! 3! 6! ETC
    :
    : // Start of java program
    : // by edwin rhodes
    : //
    :
    :
    : public class fator {
    :
    : public static void main(String[] args) {
    : int factValue;
    : int i;
    :
    : System.out.println("Java Factor Program by Edwin Rhodes");
    : System.out.println("Copyright (C) Edwin Rhodes 2006-2007");
    : System.out.println("All Rights Reserved.");
    : System.out.println("
    ");
    :
    : for ( i=1; i<21; i++ ) {
    : factValue = fator.fact(i); // factValue contains factorial of i.
    : System.out.print("The Value For Factor: ");
    :
    :
    :
    : if ( ( i % 2 ) == 0 )
    : {
    : System.out.println("Even!");
    : }
    :
    : else
    : {
    :
    : System.out.print(i);
    : System.out.print("is: ");
    : System.out.print(factValue);
    : System.out.print("
    ");
    : }
    :
    :
    :
    :
    : }
    :
    :
    : }
    :
    : public static int fact(int someInt) {
    : int i = 1;
    : if (someInt > 1)
    : i = someInt*fact(someInt-1);
    : return i; // Returns value as result of function
    : }
    : }
    :
    :
    : // End of java program
    : //
    :
    :
    :
    [code]
    if (i % 2 <> 0) {
    factValue = fact(i);
    } else {
    factValue = -1;
    }
    [/code]
Sign In or Register to comment.