[C/C++] Program to Raise a Number “n” to Positive Power “x”

C Code

#include<stdio.h>
#include<conio.h>

void main()
{
  int x,n,result=1;
  clrscr();
  printf("Enter Number \"n\": ");
  scanf("%d",&n);
  printf("\nEnter Power to Raise \"x\": ");
  scanf("%d",&x);
  if(x > 0)
  {
    for(int i=0;i<=x;i++)
      result=x*n;
    printf("n Power x = %d",result);
  }
  else
  {
    printf("x must be a positive number");
  }
  getch();
}

C++ Code

#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
  int x,n,result=1;
  cout << "Enter Number \"n\": ";
  cin >> n;
  cout << "\nEnter Power to Raise \"x\": ";
  cin >> x;
  if(x > 0)
  {
    for(int i=0;i<=x;i++)
      result=x*n;
    cout << "n Power x = " << result;
  }
  else
  {
    cout << "x must be a positive number";
  }
  getch();
  return 0;
}

 

Categories: C Language,C Programs,Computer Science

Tags: ,,

1 Comment

  1. #include
    #include
    #include
    using namespace std;
    int main() {
    int x,n,result=1;
    cout<>n;
    cout<>x;

    for(int i=1;i<=x;i++)
    {
    result=n*result;

    }cout<<n<<" ^ "<<x<<" = "<<result;

    getch();
    }

    Reply

Leave A Reply

Your email address will not be published.

×