Sunday, August 2, 2009

Write a C program to print the powers from 1 to n of a given integer m.?

Write a C program to print the powers from 1 to n of a given integer m. Your program must keep finding and printing the powers until a negative value is entered for m. You have to use a nested loop – the outer loop has to be a sentinel-controlled loop(sentinel value of %26lt;0) , the inner loop is a counter controlled loop that is executed n times ! Eg: Enter a number m = 2 Powers up to n = 4 The powers of 2 are : 1 2 4 8

Write a C program to print the powers from 1 to n of a given integer m.?
#include %26lt;stdio.h%26gt;


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





int main()


{


int m = 2;


int n = 4;


int i = 0;


int res = 0;





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


{


res = pow(m, i);


printf("%d ", res);


}


}
Reply:If the powers are supposed to be from "1" to n, answer for m=2 and n=4 will be 2, 4, 8, 16. For answer to be 1 2 4 8, powers should be from "0" to "n-1"


-----------------------------


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





int main()


{





int m = 0;


int n = 0;





printf( "Enter value of m: ") ;


scanf( "%d", m ) ;





printf( "Enter value of n: ") ;


scanf( "%d", n ) ;





long pwr = 1;





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


printf("%d^%d = %d\n", m, i, pwr*n);





return 0;


}

deliver flowers

No comments:

Post a Comment