Posts

Showing posts from August, 2017

C Program to Calculate the Power of a Number

Image
Example on how to calculate the power of a number if the exponent is an integer. Also, you will learn to compute the power using pow() function. C Program to Calculate the Power of a Number To understand this example, you should have the knowledge of following C programming topics: C Program to Reverse a Number C Program to Check Whether a Number is Positive or Negative The program below takes two integers from the user (a base number and an exponent) and calculates the power. For example:  In case of 2 3 2 is the base number 3 is the exponent And, the power is equal to 2*2*2 Example #1: Power of a Number Using while Loop #include <stdio.h> int main() {     int base, exponent;     long long result = 1;     printf("Enter a base number: ");     scanf("%d", &base);     printf("Enter an exponent: ");     scanf("%d", &exponent);     while (exponent != 0)     {      

C Program to Reverse a Number

Image
Example to reverse an integer entered by the user. This problem is solved using while loop in this example. C Program to Reverse a Number To understand this example, you should have the knowledge of following C programming topics: C Program to Find Factorial of a Number C Program to Generate Multiplication Table Example: Reverse an Integer #include <stdio.h> int main() {     int n, reversedNumber = 0, remainder;     printf("Enter an integer: ");     scanf("%d", &n);     while(n != 0)     {         remainder = n%10;         reversedNumber = reversedNumber*10 + remainder;         n /= 10;     }     printf("Reversed Number = %d", reversedNumber);     return 0; } Output Enter an integer: 2345 Reversed Number = 5432 This program takes an integer input from the user. Then the while loop is used until  n != 0 is false. In each iteration of while loop, the remainder when  n