DEV Community

Shahriyar Al Mustakim Mitul
Shahriyar Al Mustakim Mitul

Posted on

0 to Advance of Java, Python & C, C++ : Part 1

Let's learn how to print Hello World! in Java, Python , C & C++:

In Java:

package hello;

public class Hello {
    public static void main(String[] args){
        System.out.println("Hello World!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Here hello is a package & Hello is the class name . Remember to run the file using Hello.java

In Python:

print("Hello World!")
Enter fullscreen mode Exit fullscreen mode

In C:

#include <stdio.h>

int main(void){
    printf("Hello World!");
}
Enter fullscreen mode Exit fullscreen mode

In C++:

#include <iostream>
using namespace std;

int main(){
    cout<<"Hello World!";
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Summation & Subtraction

Let's add 25 & 99 and print the result. Also, subtract 99 from 25. So, the output will be
The summation of a & b is: 124
The subtraction of b & a is: 74
In Java:

public class run {
    public  static void main(String[ ] args){
        int a=25; //declaring an int variable a
        int b=99;  //declaring an int variable b
        int sum=a+b; //Summation of a& b
        int sub=b-a; //Subtraction of b & a
        System.out.println("The summation of a & b is: "+sum);
        System.out.println("The subtraction of b & a is: "+sub);
    }

}

Enter fullscreen mode Exit fullscreen mode

In Python:

a=25 #declating a variable which has a value 25
b=99 #declating a variable which has a value 99
sum=a+b
sub=b-a
print(f"The summation of a & b is:  {sum}") #printing the value
print(f"The subtraction of b & a is: {sub}")
Enter fullscreen mode Exit fullscreen mode

In C:

#include <stdio.h>
int main(void){
    int a,b,sum,sub;//Declaring integer variables
    a=25;
    b=99;
    sum=a+b;
    sub=b-a;

    printf("The summation of a & b is:  %d\n",sum); //printing the result .
    // %d is a format specifier & means that  an integer is to be output in decimal

    printf("The subtraction of b & a is: %d",sub);
}



%d is a format specifier for integers. %f works for float & double . %c works for character value.

Enter fullscreen mode Exit fullscreen mode

In C++:

#include <iostream>
using namespace std;

int main() {
    int a, b, sum, sub;
    a = 25;
    b = 99;
    sum = a + b;
    sub = b - a;
    cout << "The summation of a & b is: " << sum<<"\n";
    cout << "The subtraction of b & a is: " << sub;

}
Enter fullscreen mode Exit fullscreen mode

Look at the cout . Here we used << to add different types of values . For example, "The result is: " is a string & result is an integer.

Input

Let's have this output:
Enter a number:
120
Enter another number:
45
The summation of 120 & 45 is : 165

In java:


import java.util.Scanner; //import this to scan something

public class run {
    public  static void main(String[ ] args){
        Scanner scr = new Scanner(System.in); //Creating scr which will be used to take input
        System.out.println("Enter a number: ");//Printing
        int a=scr.nextInt(); //declaring an integer variable called "a".
        System.out.println("Enter another number: ");
        int b=scr.nextInt();


        int result=a+b;
        System.out.println("The summation of "+a+" & "+b+"  is : "+result);



    }

}

Enter fullscreen mode Exit fullscreen mode

In python:

a=int(input("Enter a number: \n"))
b=int(input("Enter another number: \n"))
result=a+b
print(f"The summation of {a} & {b}  is : {result}")

Enter fullscreen mode Exit fullscreen mode

In python by default all the variable are string variables . As we are taking numerical inputs, we need to convert it to integer .And then add them.

In C:

#include <stdio.h>
int main(void){
    int a,b,result;
    printf("Enter a number: \n");
    scanf("%d",&a);
    printf("Enter another number: \n");
    scanf("%d",&b);
    printf("The summation of %d & %d  is : %d",a,b,a+b);
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

In C++:

#include <iostream>
using namespace std;

int main() {
    int a,b;
    cout<<"Enter a number: \n";
    cin>>a;
    cout<<"Enter another number: \n";
    cin>>b;
    cout<<"The summation of "<<a<<" & "<<b<<" is: "<<a+b;
    return 0;


}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)