Tuesday, July 14, 2009

Please help me convert this C program to JAVA soure code(program):?

the output of this given program is:





1


1 1


1 2 1


1 3 3 1


1 4 6 4 1


15 10 10 5 1


1 6 15 20 15 6 1


1 7 21 35 35 21 7 1


1 8 28 56 70 56 28 8 1


1 9 36 84 126 126 84 36 9 1





and here is the C source code(program)of the output above! please help me convert this C source code to JAVA source code(program) completely, with the same output as from the above.





#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





long ncr(int n,int r){ /*function to calculate nCr*/


int i=1;


long ncr=1;





while (n%26gt;r){


ncr=ncr*n/i;


n=n-1;


i=i+1;


}


return ncr;


}


void main()


{


clrscr();


int n,i,j,k;


printf("How many rows?");scanf("%d",%26amp;n);





for(i=1;i%26lt;=n;i++){


for (k=1;k%26lt;=n-i;k++) printf(" ");


for(j=0;j%26lt;=i-1;j++){ /* print terms */


printf("%d ",ncr(i-1,j)); /* of one */


} /* row */


printf("\n\n"); /* Goto next row */


}





getch();


}

Please help me convert this C program to JAVA soure code(program):?
// u can not use n like u did, even in C or C++, i corrected it by using x instead


private long ncr(int n, int r)


{


int i=1; int x=n;


long ncr=1;


while (x%26gt;r)


{


ncr=ncr*x/i;


x=x-1;


i=i+1;


}


return ncr;


}





public static void main(String arg[ ])


{


int n,i,j,k;


System.Out.Println("How many rows?"); // i forgot if the O and P are caps or not


int n=System.in.readline();





for (i=1; i %26lt;= n; i++)


for (k=1;k%26lt;=n-i;k++) System.out.println();


for(j=0;j%26lt;=i-1;j++){ /* print terms */


System.out.println("%d ",ncr(i-1,j)); /* of one */


} /* row */


System.out.print("\n\n"); /* Goto next row */


}


}

cosmos

No comments:

Post a Comment