0% found this document useful (0 votes)
10 views1 page

Syntax Mapping: Python to C/C++/Java

The document provides a comparison of syntax and examples for common programming concepts in Python, C, C++, and Java. It includes mappings for print statements, variable declarations, control structures, loops, functions, arrays, and string operations. Additionally, it presents example problems such as summing an array and checking for prime numbers in each language.

Uploaded by

gowtham1671s
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Syntax Mapping: Python to C/C++/Java

The document provides a comparison of syntax and examples for common programming concepts in Python, C, C++, and Java. It includes mappings for print statements, variable declarations, control structures, loops, functions, arrays, and string operations. Additionally, it presents example problems such as summing an array and checking for prime numbers in each language.

Uploaded by

gowtham1671s
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python → C / C++ / Java Syntax Mapping &

Examples
Concept Python C C++ Java

Print print(x) printf("%d", x); cout << x; [Link](x);

Input int x = int(input()) scanf("%d", &x); cin >> x; Scanner sc = new Scanner([Link])

Variable a = 10 int a = 10; int a = 10; int a = 10;

if x>5:
...
else:
If-Else ... if(x>5) { } else { } if(x>5) { } else { } if(x>5) { } else { }

For loop range for i in range(5): for(int i=0;i<5;i++){} for(int i=0;i<5;i++){} for(int i=0;i<5;i++){}

While loop while x>0: while(x>0){} while(x>0){} while(x>0){}

def add(a,b):
Function return a+b int add(int a,int b){ return int
a+b;
add(int
} a,int b){ return int
a+b;
add(int
} a,int b){ return a+b; }

Array arr = [1,2,3] int arr[3] = {1,2,3}; int arr[3] = {1,2,3}; or vector<int>
int[] arr =arr={1,2,3};
{1,2,3};

String length len(s) strlen(s) [Link]() [Link]()

Append to list [Link](5) arr[size++] = 5; arr.push_back(5); [Link](5);

Example Problems

Example 1 – Sum of Array


Python C C++ Java

int s = 0; int s = 0; int s = 0;


s=0 for(int i=0;i<n;i++){ for(int i=0;i<n;i++){ for(int i=0;i<n;i++){
for x in arr: s += arr[i]; s += arr[i]; s += arr[i];
s += x } } }

Example 2 – Check Prime


Python C C++ Java

int isPrime(int n){ bool isPrime(int n){ boolean isPrime(int n){


def isPrime(n): if(n < 2) return 0; if(n < 2) return false; if(n < 2) return false;
if n < 2: return False for(int i=2;i*i<=n;i++){ for(int i=2;i*i<=n;i++){ for(int i=2;i*i<=n;i++){
for i in range(2, int(n**0.5)+1): if(n % i == 0) return 0; if(n % i == 0) return false; if(n % i == 0) return false;
if n % i == 0: } } }
return False return 1; return true; return true;
return True } } }

You might also like