Professional Documents
Culture Documents
INPUT OUTPUT
C++ JAV
#include <iostream>
using namespace std;
import java.util.*;
A
public class TestProg
int main() {
{ public static void main(String[ ] args)
int age; {
Scanner insert = new Scanner(System.in);
cout << "Enter your age:";
cin >> age; System.out.print("Enter your age: ");
int name = insert.nextInt();
cout << "\nYour age is: " << age;
System.out.println("Your age is: " +age);
return 0; }
} }
OUTPUT
Enter your age: 27
Your age is 27
Get float, double and String Input in JAVA
LOOPING
• In computer programming, loops are used to repeat a block of code.
• For example, let's say we want to show a message 100 times. Then
instead of writing the print statement 100 times, we can use a loop.
• That was just a simple example; we can achieve much more efficiency
and sophistication in our programs by making effective use of loops.
• There are 3 types of loops.
1. for loop
2. while loop
3. do…while loop
for looping
C++ JAV
#include <iostream>
using namespace std;
A
public class ForExample
{
int main() public static void main(String[] args)
{ {
for (int i = 1; i <= 5; i++) for(int i=1;i<=5;i++)
{ {
cout << "Hello World! " << endl; System.out.println(“Hello World!);
} }
}
return 0; }
}
//Java program to find the sum of first n natural //numbers
// C++ program to find the sum of first n natural //numbers
// positive integers such as 1,2,3,...n are known as
// positive integers such as 1,2,3,...n are known as //natural
//natural numbers
numbers
import java.util.Scanner;
#include <iostream>
using namespace std;
public class ForExample
int main()
{
{
public static void main(String[] args)
int num, sum = 0;
{
Scanner input = new Scanner(System.in);
cout << "Enter a positive integer: ";
int num, sum = 0;
cin >> num;
System.out.print("Enter a positive integer: “);
for (int count = 1; count <= num; count++)
num = input.nextInt();
{
sum += count; // sum = sum + count;
for (int count = 1; count <= num; count++)
}
{
sum += count; //sum = sum + count;
cout << "Sum = " << sum << endl;
}
return 0;
System.out.println ("Sum = “ +sum);
}
}
}
while looping do while looping
//Java program to find the sum of first n natural //numbers //Java program to find the sum of first n natural //numbers
// positive integers such as 1,2,3,...n are known as // positive integers such as 1,2,3,...n are known as
//natural numbers //natural numbers
int number1;
int number2;
int number3;
int number4;
int number5;
number1 = 10;
number2 = 20;
number3 = 30;
number4 = 40;
number5 = 50;
System.out.println ("number1 = ” +number1);
System.out.println ("number2 = ” +number2);
System.out.println ("number3 = ” +number3);
System.out.println ("number4 = ” +number4);
System.out.println ("number5 = ” +number5);
Declaration Arrays
• To create an array variable in C++/Java, a programmer specifies the type of the elements and the
number of elements to be stored in that array.
C++ JAV
int x [6]; or
A or
int x[ ] = new int [6]; int [ ] x = new int [6];
or
int x[ ]; int [ ] x;
x = new int [6]; x = new int [6];
Initialize Arrays in Java
• In Java, we can initialize arrays during declaration. For example,
• In the Java array, each memory location is associated with a number. The number is known as an
array index. We can also initialize arrays in Java, using the index number. For example,
Array Initialization (C++)
Access Elements in Array
• each element in an array is associated with a number.
• The number is known as an array index.
• We can access elements of an array by using those indices.
public class TryProg
{
public static void main(String[] args)
{
int [ ] age = {12, 4, 5, 2, 5}; // create an array
C++ JAV
#include <iostream>
#include <cmath>
public class TryProg
{
A
using namespace std; public static void main(String[] args)
{
int main( ) // using the sqrt() method
{ System.out.print("Square root of 4 is: " + Math.sqrt(4));
// using the sqrt() method }
cout<< "Square root of 4 is: " <<sqrt(4)); }
}
C++ JAV
#include <iostream>
using namespace std;
public class newProg
{
A
public static void main(String[] args)
int main() {
{ System.out.println("About to encounter a method.");
cout<<"About to encounter a method.“ <<endl;
myMethod(); // method call
myMethod(); // function call
System.out.println("Method was executed successfully!");
cout<<"Method was executed successfully!"; }