Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,673 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,184 people online right now. Registration is fast and FREE... Join Now!




Help in dealing with loops. Beginning C in college

 
Reply to this topicStart new topic

Help in dealing with loops. Beginning C in college, Topic is about iterative statements (Count controlled loops)

biboy_0916
post 14 Jul, 2007 - 11:14 PM
Post #1


New D.I.C Head

*
Joined: 16 Jun, 2007
Posts: 4


My Contributions


There is something wrong with my solution, but i can't figure out. The instruction is:

"Write a program that will compute for a raised to x (x as a's exponent) given real value a and positive integer x."

And here's my code:
CODE

#include <stdio.h>

main()
{
  int nNum, nExp, i;

  printf("Enter a real number: ");
  scanf("%d", &nNum);
  printf("Enter a positive integer for the exponent: ");
  scanf("%d", &nExp);

  for (i = 0; i <= nExp; i++)
  {
    nNum *= nNum;
  }

printf("%d", nNum);

}


Thanks in advance.

This post has been edited by biboy_0916: 14 Jul, 2007 - 11:16 PM
User is offlineProfile CardPM

Go to the top of the page

PennyBoki
post 15 Jul, 2007 - 05:26 AM
Post #2


system("revolution");

Group Icon
Joined: 11 Dec, 2006
Posts: 2,009



Thanked 5 times

Dream Kudos: 500

Expert In: Java,C++,C

My Contributions


Hi, try this code:
Try to understand what I did in that code that is different that what you did in yours. Feel free to post again if you run into some problems again. Enjoy smile.gif

CODE
#include <stdio.h>

main()
{
  int nNum, nNum1, nExp, i;

  printf("Enter a real number: ");
  scanf("%d", &nNum);
  printf("Enter a positive integer for the exponent: ");
  scanf("%d", &nExp);
  nNum1=nNum;

  for (i = 0; i < nExp-1; i++)
  {
    nNum1 *= nNum;
  }

printf("\n%d\n", nNum1);
return 0;

}
User is offlineProfile CardPM

Go to the top of the page

biboy_0916
post 21 Jul, 2007 - 11:58 PM
Post #3


New D.I.C Head

*
Joined: 16 Jun, 2007
Posts: 4


My Contributions


Gee... thanks. I'm having a hard time formulating algorithms in dealing with loops. biggrin.gif icon_up.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:19AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month