Practice Program
Solution: through for loop
void main()
{
int a,n,power = 1;
int i;
printf("Enter the number:");
scanf("%d",&a);
printf("Enter the Power: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
power = power*a;
}
printf("%d raise to the power of %d is %d",a,n,power);
}
void main() { int a,n,power = 1; int i; printf("Enter the number:"); scanf("%d",&a); printf("Enter the Power: "); scanf("%d",&n); for(i=1;i<=n;i++) { power = power*a; } printf("%d raise to the power of %d is %d",a,n,power); }
Output1
Enter the number:5
Enter the Power: 5
2 raise to the power of 5 is 32.
Enter the Power: 5
2 raise to the power of 5 is 32.
Output2
Enter the number:5
Enter the Power: 3
5 raise to the power of 3 is 125.
Enter the Power: 3
5 raise to the power of 3 is 125.
Output3
Enter the number:3
Enter the Power: 5
3 raise to the power of 5 is 243.
Enter the Power: 5
3 raise to the power of 5 is 243.
Solution : through while loop
void main()
{
int a,n,power = 1;
int i;
printf("Enter the number:");
scanf("%d",&a);
printf("Enter the Power: ");
scanf("%d",&n);
i=1;
while(i<=n)
{
power = power*a;
i++;
}
printf("%d raise to the power of %d is %d",a,n,power);
}
void main() { int a,n,power = 1; int i; printf("Enter the number:"); scanf("%d",&a); printf("Enter the Power: "); scanf("%d",&n); i=1; while(i<=n) { power = power*a; i++; } printf("%d raise to the power of %d is %d",a,n,power); }
Solution: from do-while loop
void main()
{
int a,n,power = 1;
int i;
printf("Enter the number:");
scanf("%d",&a);
printf("Enter the Power: ");
scanf("%d",&n);
i=1;
do
{
power = power*a;
i++;
}while(i<=n)
printf("%d raise to the power of %d is %d",a,n,power);
}
void main() { int a,n,power = 1; int i; printf("Enter the number:"); scanf("%d",&a); printf("Enter the Power: "); scanf("%d",&n); i=1; do { power = power*a; i++; }while(i<=n) printf("%d raise to the power of %d is %d",a,n,power); }
defining a function 'powers' which will return a raise to power n.
int powers(int a,int n)
{
int pow=1,i;
for(i=1; i<=n; i++)
{
pow = pow * a;
}
return pow;
}
int powers(int a,int n) { int pow=1,i; for(i=1; i<=n; i++) { pow = pow * a; } return pow; }
Using above define function 'powers':
void main()
{
int a,n,power;
printf("Enter the number:");
scanf("%d",&a);
printf("Enter the Power: ");
scanf("%d",&n);
power = powers(a,n);
printf("%d raise to the power of %d is %d",a,n,power);
}
void main() { int a,n,power; printf("Enter the number:"); scanf("%d",&a); printf("Enter the Power: "); scanf("%d",&n); power = powers(a,n); printf("%d raise to the power of %d is %d",a,n,power); }
Output1
Enter the number:5
Enter the Power: 5
2 raise to the power of 5 is 32.
Enter the Power: 5
2 raise to the power of 5 is 32.
Using above define function 'powers':
void main()
{
int a,n,power;
printf("Enter the number:");
scanf("%d",&a);
printf("Enter the Power: ");
scanf("%d",&n);
power = powers(a,n);
printf("%d raise to the power of %d is %d",a,n,power);
}
void main() { int a,n,power; printf("Enter the number:"); scanf("%d",&a); printf("Enter the Power: "); scanf("%d",&n); power = powers(a,n); printf("%d raise to the power of %d is %d",a,n,power); }
Output2
Enter the number:5
Enter the Power: 3
5 raise to the power of 3 is 125.
Enter the Power: 3
5 raise to the power of 3 is 125.
Using above define function 'powers':
void main()
{
int a,n,power;
printf("Enter the number:");
scanf("%d",&a);
printf("Enter the Power: ");
scanf("%d",&n);
power = powers(a,n);
printf("%d raise to the power of %d is %d",a,n,power);
}
void main() { int a,n,power; printf("Enter the number:"); scanf("%d",&a); printf("Enter the Power: "); scanf("%d",&n); power = powers(a,n); printf("%d raise to the power of %d is %d",a,n,power); }
Output3
Enter the number:3
Enter the Power: 5
3 raise to the power of 5 is 243.
Enter the Power: 5
3 raise to the power of 5 is 243.
0 Comments
Post a Comment