C Language

Multiply two integers in C language|in 6ways

Multiply two integers in C language|6ways

In this tutorial, we will discuss the Multiply two integers in C language

In this post, we are going to learn how to find multiplication of two numbers via different 6 ways in C language

Multiply two integers

Multiply two integers – standard method

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1=10,num2=15,product;
    product=num1*num2;
    printf("product of %d and %d : %d",num1,num2,product);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

product of 10 and 15 : 150

 

Approach

  • Integer variable num1 and num2 both are declared and initialized
  • the num1 and num2 both are multiplied together and the value assigned to the integer variable product
  • finally the program is displayed the output using printf() function

 

Multiply two integers – using user input

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2,product;
    printf("Enter two numbers: ");
    scanf("%d %d",&num1,&num2);
    product=num1*num2;
    printf("product of %d and %d : %d",num1,num2,product);
    getch();
    return 0;
}

 

When the above code is executed, it produces the following result

Enter two numbers:25
30
product of 25 and 30: 750

 

Approach

  • Integer variable num1 and num2 both are declared,
  • The program takes input from the user
  • Then the user enters the input value for num1 and num2
  • The program will read the input using scanf() function and store the variables in num1, num2.
  • The num1 and num2 both are multiplied together and the value assigned to the integer variable product
  • Finally the program is displayed the output using printf() function

 

Multiply two integers – using user defined function

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int multiply(int,int),num1,num2,product;
    printf("Enter the first number!\n");
    scanf("%d",&num1);
    printf("Enter the second number!\n");
    scanf("%d",&num2);
    product=multiply(num1,num2);
    printf("Product of %d and %d is: %d",num1,num2,product);
    getch();
    return 0;
}

int multiply(int n1,int n2)
{
    return(n1*n2);
}

When the above code is executed, it produces the following result

Enter the first number!
45
Enter the second number!
50
Product of 45 and 50 is: 2250

 

Approach

  • Declare a function named as multiply() with two int parameters
  • Declare num1,num2 and product.
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • The program will read the input using scanf() function and store the variables in num1, num2 respectively
  • Define the function (multiply())for find product
  • Call the function to produce output
  • Finally, the program displays the result of multiplication using printf() function

 

Multiply two integers – using recursive function

Program 4

#include <stdio.h>
#include <stdlib.h>
int multiply(int,int);
int main()
{
    int num1,num2,product;
    printf("Enter the first number!\n");
    scanf("%d",&num1);
    printf("Enter the second number!\n");
    scanf("%d",&num2);
    product=multiply(num1,num2);
    printf("Product of %d and %d is: %d",num1,num2,product);
    getch();
    return 0;
}

int multiply(int n1,int n2)
{
    if(n2==1)
        return n1;
    else
        return(n1+multiply(n1,n2-1));
}


When the above code is executed, it produces the following result

Enter the first number!
35
Enter the second number!
70
Product of 45 and 50 is: 2450

 

Approach

  • Declare a function named as multiply() with two int parameters
  • Declare num1,num2 and product.
  • The program takes input from the user
  • Then the user enters the input value for num1 and num2
  • the program will read the input using scanf() function and store the variables in num1, num2 respectively
  • define the recursive function (multipy())for find multiplication recursively
  • Call the function to produce output
  • finally, the program displays the result of multiplication using printf() function

 

Multiply two integers – using pointer

Program 5

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2,*ptr1,*ptr2,multiple;

    //read two user input integer values for variable num1,num2
    printf("Enter first number\n");
    scanf("%d",&num1);

    printf("Enter second number\n");
    scanf("%d",&num2);

    ptr1=&num1;
    ptr2=&num2;
    //assign address of variable num1, num2 to pointers ptr1 and ptr2

    multiple=*ptr1 * *ptr2;
    printf("Product of the numbers %d and %d= %d",num1,num2,multiple);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter first number!
15
Enter second number!
22
Product of the numbers 15 and 22 : 330

 

Approach

  • Integer variable num1 and num2 both are declared,
  • Integer pointer variable *ptr1 and *ptr2 both are declared,
  • The program takes input from the user
  • Then the user enters the input value for num1 and num2
  • The program will read the input using scanf() function and store the variables in num1, num2.
  • Addresses of num1 and num2 are assigned to variable ptr1 and ptr2 respectively
  • The pointer variables ptr1 and ptr2 both are use to multiply given numbers together and the value is assigned to the integer variable of multiple
  • Finally the program is displayed the output using printf() function

 

Multiply two integers – without multiplication operator

Program 6

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2;//declaring two integer variable
    int product=0;//declaring and initializing product as zero

    printf("Enter two integer : \n");
    scanf("%d%d",&num1,&num2);
    //read the two input values from the user

    while(num2 != 0){
        product+=num1;
        num2--;
    }
    printf("\nProduct of two numbers= %d",product);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter two integer:
12
13
product of two numbers=156

 

Suggested for you

Data type in C language

Variable in C language

Operator in C anguage

While loop in C language

 

Similar post

Find product of two numbers in Java language|5ways

Find product of two numbers in C++ language|6ways

Find product of two numbers in Python language|4ways

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.