To find the product of two numbers using C, C++ & Java we have two ways :-
1. Hardcode values in the code.
2. Take input from user.
1. Hardcode values in the code using C.
#include<stdio.h>
int main()
{
int n1=2,n2=3,product=0;
product=n1*n2;
printf("The product of two number is : %d ",product);
return 0;
}
1. Hardcode values in the code using C++.
#include <iostream>
using namespace std;
int main()
{
int n1=2, n2=3, product=0;
product = n1*n2;
cout<<"\nThe product of two numbers : "<<product;
return 0;
}
1. Hardcode values in the code using Java.
public class ProductOfNumbers
{
public static void main(String args[])
{
int n1=2, n2=3 ,product=0;
product= n1*n2;
printf("The product of two number is : "+product);
}
}
2. Take input from user using C
#include<stdio.h>
int main()
{
int n1,n2,product=0;
printf("Enter 2 Number : ");
scanf("%d%d",&n1,&n2);
product=n1*n2;
printf("The product of two numbers n1 and n2 is : %d ",product);
return 0;
}
2. Take input from user using C++
#include <iostream>
using namespace std;
int main()
{
int n1, n2, product=0;
cout<<"Enter any two numbers : ";
cin>>n1>>n2;
product = n1*n2;
cout<<""The product of two numbers n1 and n2 is : "<<product;
return 0;
}
2. Take input from user using Java
import java.util.Scanner;
public class ProductOfNumbers
{
public static void main(String args[])
{
int n1, n2, dif=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
n1 = sc.nextInt();
System.out.print("Enter the second number: ");
n2 = sc.nextInt();
dif = n1*n2;
System.out.println("The product of two numbers n1 and n2 is : " +product);
}
}
Comments
Post a Comment