Like Tree2Likes
  • 2 Post By Bayint Naung

Program tha calculates the square and cube of the numbers from 1 to 10.

This is a discussion on Program tha calculates the square and cube of the numbers from 1 to 10. within the C Programming forums, part of the General Programming Boards category; / * Program tha calculates the square and cube of the numbers from 1 to 10. This is my code ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Unhappy Program tha calculates the square and cube of the numbers from 1 to 10.

    / * Program tha calculates the square and cube of the numbers from 1 to 10.
    This is my code so far, it is instructed to only use the if statement (no else, while, for, etc).
    And use the \t to print the bellow table, any hints???? thanks!! */

    Code:
    
    number   square   cube
    0          0        0
    1          1        1
    2          4        8
    3          9        27
    4          16       64
    5          25       125
    6          36       216
    7          49       343
    8          64       512
    9          81       729
    10         100     1000
    
    I've achieved so far the following code:
    
    #include <stdio.h>
    #include <conio.h>
    
    main()
    
    {
    
    	int num, squ, cub;
    
    	num = 0;
    
    	squ = num * num;
    
    	cub = num * num * num;
    
    	
    	
    	printf("\nnumber\tsquare\tcube\n\n");
    
    	printf("%d\t%d\t%d\n\n", num, squ, cub);
    
    	getch();
    
    	return 0;
    }
    Any suggestions will be appreciated!!!!!!

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    I am a complete newbie, maybe more so than you are but I can tell right off the bat you need an if loop and some kind of increment operator (++) to go along with it in order to print that table as it is shown

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First off ... you are doing your calculations before you know the values of the variables. The order of execution in a C file is top to bottom... it won't go back and look for it...

    Second, I would seriously question why you can't use loops. With a for() loop... it would take about 5 or 6 lines of code...

    In fact, the only way to do this without while() do-while() or for() is to use goto() and that is a seriously depricated practice. C has loops for just that reason...

    If you can post the original assignment, that would help...

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    Or make a pair of lookup tables which gives all the squares and cubes of 1 through 10.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by whiteflags View Post
    Or make a pair of lookup tables which gives all the squares and cubes of 1 through 10.
    Still needs a loop of some kind to print them...

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    All I can say is, unless you're expected to use the pow function and employ senseless use of code duplication, you've misunderstood your requirements.
    Go back and make sure that what you think you're not allowed to use is exactly what you are not allowed to use.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    Quote Originally Posted by CommonTater View Post
    Still needs a loop of some kind to print them...
    Not really, but like iMalc says, code duplication will happen.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Recursion!
    stahta01 and iMalc like this.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Hi guys, I got it!! Thanks for your help and suggestions, here is the code if somebody needs help.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    main()
    
    {
    
    	int num0, squ0, cub0, num1, squ1, cub1, num2, squ2, cub2, num3, squ3, cub3, num4, squ4, cub4, 
        num5, squ5, cub5, num6, squ6, cub6, num7, squ7, cub7, num8, squ8, cub8, num9, squ9, cub9, 
        num10, squ10, cub10;
    
    	num0 = 0;
    	num1 = 1;
    	num2 = 2;
    	num3 = 3;
    	num4 = 4;
    	num5 = 5;
    	num6 = 6;
    	num7 = 7;
    	num8 = 8;
    	num9 = 9;
    	num10 =10;
    	
    	printf("\nnumber\tsquare\tcube\n\n");
    
    	squ0 = num0 * num0;
    
    	cub0 = num0 * num0 * num0;
    	
    	printf("%d\t%d\t%d\n\n", num0, squ0, cub0);
    
    	squ1 = num1 * num1;
    
    	cub1 = num1 * num1 * num1;
    	
    	printf("%d\t%d\t%d\n\n", num1, squ1, cub1);
    	
    
    	squ2 = num2 * num2;
    
    	cub2 = num2 * num2 * num2;
    	
    	printf("%d\t%d\t%d\n\n", num2, squ2, cub2);
    
    	squ3 = num3 * num3;
    
    	cub3 = num3 * num3 * num3;
    	
    	printf("%d\t%d\t%d\n\n", num3, squ3, cub3);
    
    	squ4 = num4 * num4;
    
    	cub4 = num4 * num4 * num4;
    	
    	printf("%d\t%d\t%d\n\n", num4, squ4, cub4);
    
    	squ5 = num5 * num5;
    
    	cub5 = num5 * num5 * num5;
    	
    	printf("%d\t%d\t%d\n\n", num5, squ5, cub5);
    	
    	squ6 = num6 * num6;
    
    	cub6 = num6 * num6 * num6;
    	
    	printf("%d\t%d\t%d\n\n", num6, squ6, cub6);
    	
    	squ7 = num7 * num7;
    
    	cub7 = num7 * num7 * num7;
    	
    	printf("%d\t%d\t%d\n\n", num7, squ7, cub7);
    	
    	squ8 = num8 * num8;
    
    	cub8 = num8 * num8 * num8;
    	
    	printf("%d\t%d\t%d\n\n", num8, squ8, cub8);
    	
    	squ9 = num9 * num9;
    
    	cub9 = num9 * num9 * num9;
    	
    	printf("%d\t%d\t%d\n\n", num9, squ9, cub9);
    	
    	squ10 = num10 * num10;
    
    	cub10 = num10 * num10 * num10;
    	
    	printf("%d\t%d\t%d\n\n", num10, squ10, cub10);
    
    	
    	getch();
    
    	return 0;
    }
    By the way, no if was really neccesary!! Because the number range was limited.
    Last edited by joelro79; 03-29-2011 at 03:22 PM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could just do this without all of the separate calculation lines, and a single variable:
    Code:
    v = 0
    printf("%d\t%d\t%d\n\n", ++v, v*v, v*v*v); /* ten times */

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Quote Originally Posted by quzah View Post
    You could just do this without all of the separate calculation lines, and a single variable:
    Code:
    v = 0
    printf("%d\t%d\t%d\n\n", ++v, v*v, v*v*v); /* ten times */

    Quzah.
    Isn't the order in which parameters are evaluated unspecified? Doesn't that mean that v*v could happen first, then ++v, then v*v*v, giving incorrect results? Or does ++v create some sequence point that prevents this from happening?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I was going to split the increment up onto its own line, but I was thinking I'd be OK on that one. You could be right on that.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    I just read a thread yesterday how some compilers evaluate the arguments right to left. Must have been on another forum. So yes, you'd need to break it.

    Oh and I've liked the recursion option because the funny part is that in order to prevent infinite recursion you'd basically still need an if-statement. Nice try though!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Actually, according to the OP, if is the only thing s/he can use. But just for kicks, here's an if-free recursive version:
    Code:
    #include <stdio.h>
    
    int powers(int n)
    {
        return (n < 0) || (powers(n-1) && printf("%d\t%d\t%d\n", n, n*n, n*n*n));
    }
    
    int main(void)
    {
        printf("number\tsquare\tcube\n");
        powers(10);
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Help. square and cube a user inputed number
    By Beedee5889 in forum C Programming
    Replies: 5
    Last Post: 03-11-2010, 12:53 PM
  2. square of 2 numbers
    By raveen1001 in forum C Programming
    Replies: 17
    Last Post: 10-10-2009, 04:49 AM
  3. for loops using square roots, square, Cube
    By lotf in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 03:29 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21