Entry Level C++ Problems: Multiplication Table

So you’re asked to create a C++ script that’ll multiplay numbers between 1 and 10 by each other and themselves to create a 10×10 multiplication table.

This is a simple nested loop problem. We need to take a number and multiply it by numbers from 1 to 10. This begins with number 1 which gets multiplied by 1, 2, 3, 4, 5, 6 ,7, 8, 9, 10, then number 2 which gets multiplied by 1, 2, 3 ,4, 5, 6, 7, 8, 9, 10… and so on. First loop will need to start at i=1 and go all the way up until i<=10. Then you need another loop that is nested within the ‘i’ loop that’ll multiply ‘i’ by ‘j’ 10 times starting from j=1 all the way up until j<=10. That’s the core of the program. Below I added some if statements that’ll make the output easier to read.

#include <iostream>

int main()
{

	std::cout << "	  Multiplication Table" << std::endl;
	for (int k = 1; k <= 40; k++)
	{
		std::cout << "-";
	}

	std::cout << std::endl;

	for (int i = 1; i <= 10; i++)
	{
		for (int j = 1; j <= 10; j++)
		{
			if (j * i < 10)
			{
				std::cout << i * j << "  |";
			}
			else if (j * i > 99)
			{
				std::cout << i * j << "|";
			}
			else
			{
				std::cout << i * j << " |";
			}

		}
		std::cout << std::endl;

		for (int k = 1; k <= 40; k++)
		{
			std::cout << "-";
		}

		std::cout << std::endl;
	}

	return 0;
}

Leave a Reply

Discover more from NMKN Studio

Subscribe now to keep reading and get access to the full archive.

Continue reading