Давайте научимся печатать Hello World!
на Java, Python , C & C++:
В Java:
package hello;
public class Hello {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
Здесь hello — это пакет & Hello — имя класса. Не забудьте запустить файл с именем Hello.java
В Python:
print("Hello World!")
На C:
#include <stdio.h>
int main(void){
printf("Hello World!");
}
На C++:
#include <iostream>
using namespace std;
int main(){
cout<<"Hello World!";
return 0;
}
Суммирование и вычитание
Давайте сложим 25 и 99 и выведем результат. Также вычтем 99 из 25. Таким образом, результат будет выглядеть следующим образом
В 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);
}
}
В 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}")
На 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: %dn",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.
На 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;
}
Посмотрите на cout . Здесь мы использовали << для добавления различных типов значений. Например, «The result is: » — это строка & result — целое число.
Ввод
Получим следующий вывод:
В 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);
}
}
В 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}")
В python по умолчанию все переменные являются строковыми переменными. Поскольку мы принимаем числовые данные, нам нужно преобразовать их в целочисленные. А затем сложить их.
В 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;
}
На 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;
}