Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another by using pow() method present in math.h library.
Source Code:One Number Raised To Another using pow(): C Program
#include < stdio.h >
#include < math.h >
int main()
{
int base, exponent;
printf("Enter base value\n");
scanf("%d", &base);
printf("Enter exponent value\n");
scanf("%d", &exponent);
printf("%d to the power of %d is %2f\n",
base, exponent, pow(base, exponent));
return 0;
}
Output: Enter base value 25 Enter exponent value 3 25 to the power of 3 is 15625.000000
Note: pow() method is present in math.h library, so we include it at the top of our c source code.
In this program, we ask the user to input values for base and exponent. We use while loop in this program. The while loop is executed “exponent” times. And inside while loop we multiply base value and output the result.
Source Code:One Number Raised To Another: C Program
#include < stdio.h >
int main()
{
int base, exponent, res = 1, temp;
printf("Enter the base\n");
scanf("%d", &base);
printf("Enter the exponent\n");
scanf("%d", &exponent);
temp = exponent;
while(exponent)
{
res = res * base;
exponent--;
}
printf("%d to the power of %d is %d\n", base, temp, res);
return 0;
}
Output: Enter the base 5 Enter the exponent 4 5 to the power of 4 is 625
Note: 1. We are initializing value of variable res to 1 in order to make sure it doesn’t have garbage value in it. If it was an addition operation we could have assigned it a initial value of 0. But in above program we are doing multiplication. So multiplying any number with 0 will give 0 as result. So we initialize the value of variable res to 1.
2. We modify the value of variable exponent in while loop. So to preserve the user entered value for exponent, we take another variable temp and store the original number(exponent value) entered by the user.
Write a program to print out all Armstrong numbers or Narcissistic number between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number.
For Example: 407 = (4*4*4)+(0*0*0)+(7*7*7) 407 = (64) + (0) + (343) 407 = 407 Hence, 407 is a Armstrong number.
Nested While Loop In this program we are using nested while loop to check for Armstrong numbers from 1 to 500.
Outer while loop loops from 1 to 500, by incrementing value of count by 1. Inner while loop checks every value of count to determine if the value is a Armstrong number or not.
Logic for Finding Armstrong Number
We’ve explained the logic to find Armstrong number in detail, in our previous video tutorial. Kindly visit C Program to Check Armstrong Number
C Program to print Armstrong Numbers between 1 and 500
Source Code: C Program to print Armstrong Numbers between 1 and 500
#include < stdio.h >
int main()
{
int num, count = 1, rem, sum;
while(count <= 500)
{
num = count;
sum = 0;
while(num)
{
rem = num % 10;
sum = sum + (rem * rem * rem);
num = num / 10;
}
if(count == sum)
{
printf("%d is a Armstrong number\n", count);
}
count++;
}
return 0;
}
Output: 1 is a Armstrong number 153 is a Armstrong number 370 is a Armstrong number 371 is a Armstrong number 407 is a Armstrong number
Number of Iterations In Nested Loops Number of iterations will be equal to the number of iterations in the outer loop multiplied by the number of iterations in the inner loop.
For every single iteration of the outer while loop, the inner while loop completes its iterations.
A loop inside another loop is called a nested loop. Consider a nested loop where the outer loop runs x times and consists of another loop inside it. The inner loop runs y times. Then, the total number of times the inner loop runs during the program execution is x*y times.
Lets write a C program to check whether user entered number is Armstrong or not.
Armstrong number: is a number that is equal to the sum of cubes of its individual digits.
Example: If user input the number 371. It’s individual digits are 3, 7 and 1. Lets cube each digit: 33 + 73 + 13 = 27 + 343 + 1 = 371.
The user entered number 371 is equal to the sum of cube of its individual digits. So 371 is a Armstrong number.
Logic
If user enters number as 371. 1. Lets use modular division on that number. 371 % 10 = 1; 2. Lets cube the result(reminder) and store it inside a variable sum. (1*1*1) = 1. So sum = 1. 3. Divide the number(371) by 10. 371 / 10 = 37. (Remember, when you divide a number by integer number, it returns only the integer part of the result).
Now, sum = 1, number = 37.
Lets repeat step 1, 2 and 3. 1. Lets use modular division on that number. 37 % 10 = 7; 2. Lets cube the result(reminder) and store it inside a variable sum. (7*7*7) = 343. So sum = 1 + 343. 3. Divide the number(37) by 10. 37 / 10 = 3.
Now, sum = 344, number = 3.
Lets repeat the steps 1,2 and 3 one more time. 1. Lets use modular division on that number. 3 % 10 = 3; 2. Lets cube the result(reminder) and store it inside a variable sum. (3*3*3) = 27. So sum = 344 + 27. 3. Divide the number(3) by 10. 3 / 10 = 0.
Now, sum = 371, number = 0;
Since number is 0, which means false, the while loop exits.
Next using if-else we check if the user entered number is equal to the sum of cubes of its individual digits.
#include < stdio.h >
int main()
{
int num, rem, sum = 0, temp;
printf("Enter an integer number\n");
scanf("%d", &num);
temp = num;
while(num)
{
rem = num % 10;
sum = sum + (rem * rem * rem);
num = num / 10;
}
if(temp == sum)
{
printf("%d is armstrong number\n", temp);
}
else
{
printf("%d is not armstrong number\n", temp);
}
return 0;
}
Output 1: Enter an integer number 371 371 is armstrong number
Output 2: Enter an integer number 563 563 is not armstrong number