0% found this document useful (0 votes)
47 views

C Style Calculate The Power of A Number, Reverse A Number, Count Number of Digits

This document contains code snippets for 4 different C programs: 1. A program that uses a while loop to calculate powers of a base number. 2. A program that uses the pow() function to calculate powers of a base number. 3. A program that reverses an integer by extracting its digits. 4. A program that counts the number of digits in a number.

Uploaded by

Animal Cares
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

C Style Calculate The Power of A Number, Reverse A Number, Count Number of Digits

This document contains code snippets for 4 different C programs: 1. A program that uses a while loop to calculate powers of a base number. 2. A program that uses the pow() function to calculate powers of a base number. 3. A program that reverses an integer by extracting its digits. 4. A program that counts the number of digits in a number.

Uploaded by

Animal Cares
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// Power of a Number Using the while Loop

#include <stdio.h>
int main() {
int base, exp;
long double result = 1.0;
printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter an exponent: ");
scanf("%d", &exp);

while (exp != 0) {
result *= base;
--exp;
}
printf("Answer = %.0Lf", result);
return 0;
}

// Power Using pow() Function

#include <math.h>
#include <stdio.h>

int main() {
double base, exp, result;
printf("Enter a base number: ");
scanf("%lf", &base);
printf("Enter an exponent: ");
scanf("%lf", &exp);

// calculates the power


result = pow(base, exp);

printf("%.1lf^%.1lf = %.2lf", base, exp, result);


return 0;
}

// Reverse an Integer

#include <stdio.h>

int main() {

int n, reverse = 0, remainder;

printf("Enter an integer: ");


scanf("%d", &n);

while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}

printf("Reversed number = %d", reverse);

return 0;
}
// Program to Count the Number of Digits

#include <stdio.h>
int main() {
long long n;
int count = 0;
printf("Enter an integer: ");
scanf("%lld", &n);

// iterate at least once, then until n becomes 0


// remove last digit from n in each iteration
// increase count by 1 in each iteration
do {
n /= 10;
++count;
} while (n != 0);

printf("Number of digits: %d", count);


}

You might also like