Saturday, April 12, 2014

X^Y (X raised to Y) – C Program

Problem Question


Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another.

Explanation of Problem


Our program shall accept 2 numbers from the user, and find the value of one number raised to the power of another. For simplicity, we are making it to be the first number raised to the second.

Code


#include <stdio.h>
/*X raised to Y.c*
*@Language: ANSI C*
*@Compiler: GNU GCC*
*@IDE: CodeBlocks 12.11*
*@Author: Toxifier*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 12-04-2014**/
int main()
{
  int num1, num2, product = 1;
  printf("\n\nPlease enter 2 numbers: ");
  scanf("%d%d", &num1, &num2);
  printf("\n\nCALCULATING.......\n\n");
  while(num2 > 0)
  {
    product = product * num1;
    num2--;
  }
  printf("\nAnswer is: %d\n", product);
  system("pause");
  return 0;
}

Explanation of Code


#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H

int main() -> The entry point of the program where the execution starts. This function has to named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.

printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.

scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.

while(num2 > 0) {
product = product * num1;
num2--;
}
Here we have the main part of the program where the calculation takes place. We multiply the value of the variable ‘product’ with the value of the variable ‘num1’ and store the result in the variable ‘product’. We then decrement the value of variable ‘num2’. We iterate this procedure in a while loop until num2 becomes 0. This way we have the value of (num1^num2) stored in the variable ‘product’ which we then display on the screen.

system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.

Output(s)





5 comments:

  1. Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another. please coded in c#

    ReplyDelete
    Replies
    1. using System;

      public class XtoY
      {
      public static void Main()
      {
      string inputNum1, inputNum2;
      Console.WriteLine("Enter two numbers: ");
      inputNum1 = Console.ReadLine();
      inputNum2 = Console.ReadLine();
      int num1 = int.Parse(inputNum1);
      int num2 = int.Parse(inputNum2);
      int product = 1;
      while (num2 > 0)
      {
      product *= num1;
      num2--;
      }
      Console.WriteLine("\n\nThe Answer is: " + product);
      }
      }

      Delete
    2. i have this code.. how to change it in C# langauge!! Please help

      int main()
      {
      int n, sum_p=0, sum_n=0, sum_z=0;
      char choice;

      do
      {
      cout<<"Enter number ";
      cin>>n;

      if(n>0)
      sum_p++;
      else if(n<0)
      sum_n++;
      else
      sum_z++;

      cout<<"Do you want to Continue(y/n)? ";
      cin>>choice;

      }while(choice=='y' || choice=='Y');


      cout<<"Positive Number :"<<sum_p<<"\nNegative Number :"<<sum_n<<"\nZero Number :"<<sum_z;


      return 0;
      }

      Delete
  2. i have this code.. how to change it in C# langauge!! Please help

    int main()
    {
    int n, sum_p=0, sum_n=0, sum_z=0;
    char choice;

    do
    {
    cout<<"Enter number ";
    cin>>n;

    if(n>0)
    sum_p++;
    else if(n<0)
    sum_n++;
    else
    sum_z++;

    cout<<"Do you want to Continue(y/n)? ";
    cin>>choice;

    }while(choice=='y' || choice=='Y');


    cout<<"Positive Number :"<<sum_p<<"\nNegative Number :"<<sum_n<<"\nZero Number :"<<sum_z;


    return 0;
    }


    ReplyDelete
    Replies
    1. using System;

      public class NumberOfNumbers
      {
      public static void Main()
      {
      string choice;
      int sum_p = 0, sum_n = 0, sum_z = 0, num1 = 0;
      do
      {
      Console.WriteLine("Enter number: ");
      num1 = Convert.ToInt32(Console.ReadLine());
      if(num1 > 0)
      {
      sum_p++;
      }
      else if (num1 < 0)
      {
      sum_n++;
      }
      else
      {
      sum_z++;
      }
      Console.WriteLine("Do you want to Continue(y/n)?");
      choice = Console.ReadLine();
      }
      while (choice.Equals("y") || choice.Equals("Y"));
      Console.WriteLine("Positive Number: " + sum_p + "\nNegative Number: " + sum_n + "\nZero Number: " + sum_z);
      }
      }

      Delete

Need help?