You are on page 1of 22

BASIC PROGRAMMING

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

import java.util.Scanner; import java.util.Scanner;

public class ForExample public class ForExample


{ {
public static void main(String[] args) public static void main(String[] args)
{ {
Scanner input = new Scanner(System.in); Scanner input = new Scanner(System.in);
int num, sum; sum = 0; int num, sum; sum = 0;

System.out.println("Enter a positive integer: “); System.out.println("Enter a positive integer: “);


num = input.nextInt(); num = input.nextInt();

int count = 1; int count = 1


while(count <= num) do
{ {
sum += count; // sum = sum + count; sum += count;
count++; count++;
} } while(count <= num);

System.out.println ("Sum = “ +sum); System.out.println ("Sum = “ +sum);


} }
} }
Java Nested For Loop
• If we have a for loop inside the another loop, it is known as nested for loop.
• The inner loop executes completely whenever outer loop executes.

public class NestedForExample


{
public static void main(String[] args)
{
//loop of i
for(int i=1;i<=3;i++)
{
//loop of j
for(int j=1;j<=3;j++)
{
System.out.println(i+" "+j);
} //end of j
} //end of i
}
}
ARRAY
• Consider a situation where we need to store five integer numbers.
• If we use programming's simple variable and data type concepts, then we need five variables of
int data type and the program will be as follows −

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

// access each array elements


System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Fourth Element: " + age[3]);
System.out.println("Fifth Element: " + age[4]);
}
}
public class TryProg
{
public static void main(String[] args)
{
int [ ] age = {12, 4, 5, 2, 5}; // create an array
Output:
System.out.println("Accessing Elements of Array:");
// access each array elements using for loop Accessing Elements of Array:
for(int i = 0; i < age.length; i++) 12
{ 4
System.out.println(age[i]); 5
} 2
} 5
}
FUNCTION / METHOD
• A function is a block of code that performs a specific task
• two types of function:
1. Standard Library Functions: Predefined in C++/Java
2. User-defined Function: Created by users

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!"; }

return 0; // method definition


} private static void myMethod()
{
// function definition System.out.println("Printing from inside myMethod()!");
void myMethod() }
{ }
cout<<"Printing from inside myMethod()!") <<endl;
}
Return Value From Method
C++ JAV
public class SquareMain
#include <iostream>
using namespace std; { A
public static void main(String[] args)
int main( ) {
{ int result;
int result; // call the method and store returned value
// call the method and store returned value result = square();
result = square(); System.out.println("Squared value of 10 is: " + result);
cout<<"Squared value of 10 is: “<< result; }
}
public static int square()
int square() {
{ // return statement
// return statement return 10 * 10;
return 10 * 10; }
} }
Method Accepting Arguments and Returning
Value
C++ JAV
#include <iostream> public class TestProg
using namespace std; { A
public static void main(String[] args)
int main() {
{ int result, n = 3;
int result, n = 3; result = square(n);
result = square(n); System.out.println("Square of 3 is: " + result);
cout<<"Square of 3 is: " <<result <<endl; n = 4;
n = 4; result = square(n);
result = square(n); System.out.println("Square of 4 is: " + result);
cout<<"Square of 4 is: " <<result; }
return 0; // method
} public static int square(int i)
// method {
int square(int i) return i * i;
{ }
return i * i; }
}

You might also like