Object

Write a program to print the value of a number raised to power of another.

Algorithm

  1. Declare integer variables num, pow, i and value.
  2. Set value = 1.
  3. Read num and pow.
  4. Set i = 1.
  5. Repeat WHILE i <= pow
    • value = value * num.
    • Write “num *”.
    • i++.
    • [End of WHILE loop.]
  6. Write “= value”.
  7. Exit.

Flowchart

C++ Source Code

// program 51
#include<iostream>
using namespace std;

int main()
{
  int num, pow, i, value = 1;
  cout << "Enter any number: ";
  cin >> num;
  cout << "Enter the power: ";
  cin >> pow;

  i = 1;
  while (i <= pow)
  {
    cout << num << " * ";
    value = value * num;
    i++;
  }
  cout << "\b\b= " << value;
  return 0;
}

C Source Code

/*program 51*/
#include<stdio.h>

int main()
{
  int num, pow, i, value=1;
  printf("Enter any number: ");
  scanf("%d", &num);
  printf("Enter the power: ");
  scanf("%d", &pow);
  i = 1;
  while(i<=pow)
     {
     printf("%d * ", num);
     value = value * num;
     i++;
     }
  printf("\b\b= %d", value);
  return 0;
}

Output

Enter any number: 5
Enter the power: 4
5 * 5 * 5 * 5 = 625

Design a site like this with WordPress.com
Get started