Challenge Solution

Ok, this is what I came up with for this one.

#include <stdio.h>

int main(int argc, const char * argv[])
{
int number=5;
int numberSquared=number*number;
printf(""%d" sqaured is “%d”.\n", number, numberSquared);
return 0;
}

I like your solution, it’s a lot cleaner than mine. I wanted to keep practicing with functions, so I have this mess:

#include <stdio.h>

int squareFunction(int initialNumber)
{
int squaredNumber = initialNumber * initialNumber;
return squaredNumber;
}

int main(int argc, const char * argv[])
{
int firstNumber = 5;
int finalNumber = squareFunction(firstNumber);
printf("%d squared is %d!\n\n\n", firstNumber, finalNumber);
return 0;
}

I put the number I want to square (5) as it’s own variable because I couldn’t see any way to print it in the printf function if I just called squareFunction(5).

Hey thought I’d throw mine in, too.

I wanted to work on functions as well, but this is another thing I found out just by playing with printf().

[code]#include <stdio.h>
int squareRoot(int number)
{
int squared = number * number;
return squared;
}
int main(int argc, const char * argv[])
{
int whatiwant = 5;
printf("“5 squared is %d”", squareRoot(whatiwant));

}[/code]

hey, throwing mine into the mix - an extension of what TheRojo has done but i’ve changed the printf statement to adapt to whatiwant - so if I change the whatiwant integer, the printf statement will show me the squared number I;m looking for and the output. Exactly the same really but just added %d and whatiwant to the printf output

#include <stdio.h>

int squareRoot(int number)
{
int squared = number * number;
return squared;
}
int main(int argc, const char * argv[])
{
int whatiwant = 70;
printf(""%d" squared is “%d”\n", whatiwant, squareRoot(whatiwant));
}

I built off of what you folks already did and got the first number (5) to automatically fill in and have the quotes as specified.

#include <stdio.h>
int squareFormula(int figure)
{
int squared = figure * figure;
return squared;
}
int main(int argc, const char * argv[])
{
int example = 5;
printf(""%d" squared is “%d”", example, squareFormula(example));
return 0;
}

I tried to keep mine as small as possible, performing the squaring in the printf call. That’s what I love about programming - lots of different ways to do the same thing!

#include <stdio.h>

int main(int argc, const char * argv[])
{
int a = 9;
printf(""%d" squared is “%d”\n", a, (a*a));
return 0;
}

I also wanted to practice more with functions. Here’s my take:

[code]#include <stdio.h>

// Write a program that computes and displays the square of integer.

int squareIt(int originalNumber)
{
int result = originalNumber * originalNumber;
return result;
}

int main(int argc, const char * argv[])
{
int originalNumber = 5;
int squaredNumber = squareIt(originalNumber);

printf("\"%d\" squared is \"%d\".\n", originalNumber, squaredNumber);

return 0;

}
[/code]

Using scanf()

#include <stdio.h>

int Square(int numberInput)
{
    int squaredNumber = numberInput * numberInput;
    return squaredNumber;
}

int main(int argc, const char * argv[])
{
    int toBeSquared;
    
    printf("Which number would you like to square?\n");
    scanf("%d", &toBeSquared);
    
    int Squared = Square(toBeSquared);
    
    printf("%d squared is %d.\n", toBeSquared, Squared);
    
    
    return 0;
}

Hi guys,

I am really new to programing and this is the way that I did it. is it right? it just seem too simple compared to most answers.

#include <stdio.h>

int main(int argc, const char * argv[])
{

int base = 5;

int square = base * base;

printf("\"%d\" squared is \"%d\".\n", base, square);
return 0;

}

Good as gold.

Next time, try posting your Code by using the code tags like this:

#include <stdio.h>

int Squared (const int);

int main (int argc, const char * argv[])
{
   int base = -13;
   printf ("\"%d\" squared is \"%d\".\n", base, Squared (base));

   return 0;
}

int Squared (const int v)
{
   return v * v;
}

Hey guys,

Any idea what is a way to make the function that you made that squares a number reusable?
Thanks.

This is what I had for the challenge question:

[code]#include <stdio.h>

void squareIt(int number)
{

int squared = number * number;
printf("\"%d\" squared is \"%d\" \n", number, squared);

}

int main(int argc, const char * argv[])
{
squareIt(5);
return 0;

}[/code]

One option to create a more reusable function would be to rewrite the function with the help C’s pow function. Or you could write your own pow function.

My solution was like:

[code]#include <stdio.h>

int main(int argc, const char * argv[]) {

// Define number to be squared and log it to the user.
int square = 5;
printf("I am going to square %d.\n", square);

// Square number and log result to the user.
int squared = square * square;
printf("\"%d\" squared is \"%d\".\n", square, squared); 

return 0;

}
[/code]

Any suggestions for improvement? :slight_smile:

This is my answer: :wink:

#include <stdio.h>

int makeSquare(int number)

{
int makeSquaredNumber = number * number;
return makeSquaredNumber;
}

int main(int argc, const char * argv[])
{

int numberToSquare = 5;
int squaredNumber = makeSquare(numberToSquare);
printf("\"%d\" is the square of \"%d\".\n", numberToSquare, squaredNumber);

}

Console:

“5” is the square of “25”.
Program ended with exit code: 0

Am I the only one who used a void?



#include <stdio.h>

void squareInteger (int number){
    float square = number * number;
    printf("%d squared is %.2f \n", number, square);
    
}


int main(int argc, const char * argv[]) {
    
    squareInteger(5);
    return 0;
}

[quote]Am I the only one who used a void?

void squareInteger (int number){ float square = number * number; printf ("%d squared is %.2f \n", number, square); }
[/quote]
Nothing wrong with that.

However, you can give your function a more informative name to indicate precisely what it does:

void printSquareOfInteger (int number) {
    float square = number * number;
    printf ("%d squared is %.2f \n", number, square);
}

[Become a competent programmer faster than you can imagine: pretty-function.org]

Code:

[code]// Chapter 6 - Strings
// Squarer

#include <stdio.h>

int calculateSquare(int number){

int squareNumber = number*number; // local variable squareNumber
return squareNumber;

}

int main(int argc, const char * argv[]) {

int number = 5;
int squaredNumber = calculateSquare(number);
printf("\"%d\" squared is \"%d\"\n", number,squaredNumber );

return 0;

}[/code]

Console Output:

[quote]“5” squared is "25"
Program ended with exit code: 0[/quote]

If you think it can be improved, I welcome any suggestions. Thanks.

Managed this on my first attempt and I wanted to get some practice with functions as well.

It works fine but just wondering if I should have reiterated ‘int’ type in my function definition, where I put my parameter?

As it is I have ‘int squareOfInteger(number)’. Should I have ‘int’ in front of ‘number’ also?

[code]#include <stdio.h>

int squareOfInteger(number)
{
int square = number * number;
return square;

}

int main(int argc, const char * argv[]) {
int myNumber = 5;
printf(""%d" squared is “%d”.\n", myNumber, squareOfInteger(myNumber));
return 0;
}[/code]

[quote=“Nocturnal”]Code:

[code]// Chapter 6 - Strings
// Squarer

#include <stdio.h>

int calculateSquare(int number){

int squareNumber = number*number; // local variable squareNumber
return squareNumber;

}

int main(int argc, const char * argv[]) {

int number = 5;
int squaredNumber = calculateSquare(number);
printf("\"%d\" squared is \"%d\"\n", number,squaredNumber );

return 0;

}[/code]

Console Output:

[quote]“5” squared is "25"
Program ended with exit code: 0[/quote]

If you think it can be improved, I welcome any suggestions. Thanks.[/quote]

Hey Nocturnal, looks good. I think you could have avoided writing the second line inside your main function by just calling your calculateSquare function with an argument in the printf statement.
Just a suggestion though, I’m at same level as yourself and hoping for feedback on mine as well! Let me know if you spot anything!

Doing that would not be wrong, but it would be not wise.