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!");
}
}
Here hello is a package & Hello is the class name . Remember to run the file using Hello.java
In Python:
print("Hello World!")
In C:
#include <stdio.h>
int main(void){
printf("Hello World!");
}
In C++:
#include <iostream>
using namespace std;
int main(){
cout<<"Hello World!";
return 0;
}
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);
}
}
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}")
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.
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;
}
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);
}
}
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}")
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;
}
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;
}
Top comments (0)