Table of Contents
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
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