To find the difference between 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,dif=0;
dif=n1-n2;
printf("The difference of two number is : %d ",dif);
return 0;
}
1. Hardcode values in the code using C++.
#include <iostream>
using namespace std;
int main()
{
int n1=2, n2=3, dif=0;
dif = a-b;
cout<<"\nThe difference of two numbers : "<<dif;
return 0;
}
1. Hardcode values in the code using Java.
public class DiffrenceOfNumbers
{
public static void main(String args[])
{
int n1 = 2, n2 = 3, dif=0;
dif = n1 + n2;
printf("The difference of two number is : %d ",dif);
}
}
2. Take input from user using C
#include<stdio.h>
int main()
{
int n1,n2,dif=0;
printf("Enter 2 Number : ");
scanf("%d%d",&n1,&n2);
dif=n1+n2;
printf("The difference of two number : %d ",dif);
return 0;
}
2. Take input from user using C++
#include <iostream>
using namespace std;
int main()
{
int n1, n2, dif=0;
cout<<"Enter any two numbers : ";
cin>>n1>>n2;
dif = n1 - n2;
cout<<"\nThe difference of 1st three numbers : "<<dif;
return 0;
}
2. Take input from user using Java
import java.util.Scanner;
public class DiffrenceOfNumbers
{
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 difference of two numbers n1 and n2 is: " +dif);
}
}
Comments
Post a Comment