vendredi 18 septembre 2015

Java addition with power of a number program wrong result

In my program Logic is like:--

Input      Addition with      Output(result)
2            3                   5
3            3+4                10
4            3+4+4              15
5            3+4+4+4            20
6            3+4+4+4+4          25

So, I have made:--

import java.util.Scanner;

public class Addition {

           public static void main( String[] args) {
              @SuppressWarnings("resource")

            Scanner s = new Scanner(System.in);

              int result=0;

              System.out.print("Enter a number: ");

              int inputNumber = s.nextInt();

              if(inputNumber==2){
                  result = inputNumber+3; 

              }

              else{
                  Addition c=new Addition();


                      int j = inputNumber-2;

                      int power=c.pow(4,j);



                      result = inputNumber+3+power;


              }
              System.out.print(result);  

           }
          int pow(int c, int d)
             {       
                      int n=1;
                      for(int i=0;i<d;i++)
                      {
                               n=c*n;
                      }

                    return n;
             } 
}

In this program I am getting result:--

 Input               Output(result)
    2                        5
    3                       10
    4                       23
    5                       72

why? What Am I doing wrong??



from Newest questions tagged java - Stack Overflow http://ift.tt/1MuMvD0
via IFTTT

Aucun commentaire:

Enregistrer un commentaire