Table of contents
1.
Introduction
2.
Understanding Multiplication in C
3.
Structure of a C Program
4.
Using the ‘for’ loop
4.1.
Problem Statement
4.2.
Algorithm
4.2.1.
Implementation
4.3.
C
4.4.
Complexity Analysis 
5.
Frequently Asked Questions
5.1.
What are the operators?
5.2.
How many operators does C have?
5.3.
What is the difference between ++i and i++ in C?
5.4.
What does -> mean in C?
5.5.
What is %d in C programming?
6.
Conclusion
Last Updated: Jul 3, 2024
Easy

C program to multiply two numbers using plus operator

Author Amit Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will write a C program to multiply two numbers using the plus operator. To reach this goal, we need to have information about the loops in C. 

C program to multiply two numbers

A loop statement allows us to execute a statement or group of statements multiple times.

Given two numbers as input from the user, we have to multiply them without using arithmetic operators like +. For this program, we are going to perform the multiplication of two numbers by repetitive addition. In other words, X * Y is the same as X + X + X … (Y times).

You can also read about dynamic array in c and C Static Function.

Also see - Strong number in c

Understanding Multiplication in C

  • In C, multiplication is performed using the '*' operator.
     
  • Syntax example: a * b calculates the product of variables 'a' and 'b'.
     
  • Manage data types carefully to avoid overflow or loss of precision.
     
  • Integer multiplication handles whole numbers, while floating-point handles decimals.
     
  • Always declare and initialize variables before use to prevent undefined behavior.
     
  • Understanding C's handling of data types and arithmetic operations ensures accurate program execution.

Structure of a C Program

The structure of a typical C program in bullet points:

  • Include Directives: Begin with #include directives to include necessary header files (e.g., <stdio.h> for input/output functions).
     
  • Global Declarations: Declare global variables or constants if needed.
     
  • Function Declarations: Optionally, declare function prototypes for functions defined later in the code.
     
  • Main Function: Every C program must have a main() function which serves as the entry point. It returns an integer value indicating the status of program execution (0 for successful execution, non-zero for errors).
     
  • Variable Declarations: Declare local variables used within the main() function or other functions.
     
  • Executable Statements: Write executable statements which define the logic and operations of the program.
     
  • Function Definitions: Define any additional functions if required, including their return type, name, and parameters.
     
  • Return Statement: End the main() function with a return statement indicating the program's exit status.

Using the ‘for’ loop

A 'for' loop can be understood as a control structure that is in repetition, which allows us to efficiently write a loop which needs to be executed a specific number of times.

So, let us see how we can multiply two numbers using the plus operator using the ‘for’ loop.

Let’s see the algorithm first.

Problem Statement

The problem statement is that we are given two integers, X and Y. We have to multiply X and Y using the plus operator.

Sample Input

5

6

Sample Output:

30

Explanation: 

We are going to add X+X+X…(Y times).

So, we will add 5 to itself 6 times. 

5+5+5+5+5+5 = 30

Algorithm

  1. Take two numbers (X, Y) as inputs from the user.
     
  2. Take an integer type variable for storing the result.
     
  3. Initialize result with 0.
     
  4. Calculate the result using the ‘for’ loop by adding the number to the result. 
     
  5. Display the result.

 

Implementation

  • C

C

#include <stdio.h>
int main()
{
   int x, y;
   int result= 0, i;

   printf("Enter the first number: ");
   scanf("%d",&x);
 
   printf("\nEnter the second number: ");
   scanf("%d",&y);

   for(i=1;i<=y;i++) {
       result += x;
   }
 
   printf("\nMultiplication of %d and %d is: %d\n" , x, y, result);
   return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output:

you can practice by yourself with the help of an online editor.

Complexity Analysis 

Time Complexity: O(X) or O(Y), where X and Y are the first and second number respectively. 

Space Complexity: Constant space.

Also Read, Binary to Hex Converter and  Tribonacci Series

Frequently Asked Questions

What are the operators?

In mathematics and generally sometimes in computer programming, an operator is a character that represents an action; for example, x is an arithmetic operator that represents multiplication. In computer programs, one of the most familiar sets of operators, the Boolean operators, is used to work with true/false values.

How many operators does C have?

There are five main arithmetic operators in 'C.' They are '+' for additions, '-' for subtraction, '*' for multiplication, '/' for division, and '%' for the remainder after integer division. This '%' operator is also known as the modulus operator.

What is the difference between ++i and i++ in C?

'i++' means that when the code is executing, it will first read it and do the increment (i = i + 1) after it has been read. ++i means that when the code is executing, it will first do i=i+1 and then read it.

What does -> mean in C?

Operation: The -> operator in C or C++ gives the value held by variable_name to structure or union variable pointer_name. Difference between Dot(.) and Arrow(->) operator: The Dot(.) operator is used to normally access members of a structure or union.

What is %d in C programming?

In C programming language, %i and %d are known as format specifiers where %i specifies the type of variable as integer and %d specifies the type variable as a decimal. In general terms, there is almost no difference in printf() function output while printing a number using %d or %i, but using scanf, the difference occurs.

Conclusion

In this article, we have studied how to multiply two numbers using the plus operator. We discussed two ways to do that. We hope that this article has provided you with the help to enhance your knowledge regarding programming in C language and if you would like to learn more, check out our articles on jump statements in C/C++bitwise operators in C/C++Short int in C Programming and C vs C++.

Read about Bitwise Operators in C here.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available, Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Live masterclass