Skill Lab Manual
Skill Lab Manual
Experiment No.: - 01
********************************************************************
Theory: -
C++ is a compiled language. For a program to run, its source text has to be processed by
a compiler, producing object files, which are combined by a linker yielding an executable
program. A C++ program typically consists of many source code files (usually simply called
source files).
Procedure: -
1. In this program, the user is asked to enter two integers.
2. These two integers are stored in
variables first_number and second_number respectively.
3. The variables are added using the + operator and stored in the sum variable.
4. Finally, sum is displayed on the screen.
Conclusion: -
In this program, user is asked to enter two integers. Then, the sum of those two
integers is stored in a variable and displayed on the screen.
Subject In-charge
Program: -
#include <iostream.h>
#include<conio.h>
using namespace std;
void main()
{
// prints sum
cout << first_number << " + " << second_number << " = " << sum;
getch();
}
Output: -
Enter two integers:
4
5
4 + 5 = 9
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]
Experiment No.: - 02
********************************************************************
Theory: -
The given task is to take an integer as input from the user and print that
integer in C++ language.
In below program, the syntax and procedures to take the integer as input
from the user is shown in C++ language.
Steps:
Syntax:
• For an integer value, the X is replaced with type int. The syntax of cin method
becomes as follows then:
Syntax:
cin >> variableOfIntType;
Syntax:
cout << variableOfXType;
where << is the insertion operator.
The data needed to be displayed on the screen is inserted in the
standard output stream (cout) using the insertion operator (<<).
• For an integer value, the X is replaced with type int. The syntax of cout() method
becomes as follows then:
Syntax:
cout << variableOfIntType;
Procedure: -
1. In This program asks the user to enter a number.
2. When the user enters an integer, it is stored in variable number using cin.
3. Then it is displayed on the screen using cout.
Conclusion: -
Hence, the integer value is successfully read and printed.
Subject In-charge
Program: -
#include <iostream.h>
#include<conio.h>
using namespace std;
void main()
{
getch();
}
Output: -
Enter the integer: 23
Experiment No.: - 03
********************************************************************
Theory: -
Method 1:
To perform swapping in Method 1, three variables are used.
The contents of the first variable is copied into the temp variable. Then, the contents of
second variable is copied to the first variable.
Finally, the contents of the temp variable is copied back to the second variable which
completes the swapping process.
Method 2:
The output of this method is the same as the first program above.
1. Start.
2. Read a number in num1.
3. Read a number in num2.
4. Declare a variable temp.
5. Assign temp with num1.
6. Assign num1 with num2.
7. Assign num2 with temp.
8. Print num1 and num2.
9. Stop.
*Method 2:
1. Start.
2. Read a number in num1.
3. Read a number in num2.
4. Assign num1 with num2+num1.
5. Assign num2 with num1-num2.
6. Assign num1 with num1-num2.
7. Print num1 and num2.
8. Stop.
Conclusion: -
Hence, program contains two different techniques to swap numbers in C
programming. The first program uses temporary variable to swap numbers, whereas
the second program does not use temporary variables.
Subject In-charge
Program: -
#include <iostream.h>
#include<conio.h>
using namespace std;
void main()
{
// METHOD - 1
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping " << a <<" and "<< b ;
// METHOD - 2
a = 44;
b = 55;
cout << "\n\nMethod - 2: \n";
cout << "\nBefore swapping " << a <<" and "<< b ;
a = a + b;
b = a - b;
a = a - b;
cout << "\nAfter swapping " << a <<" and "<< b ;
getch();
}
Output: -
Method - 1:
Before swapping 45 and 54
After swapping 54 and 45
Method - 2:
Before swapping 44 and 55
After swapping 55 and 44
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]
Experiment No.: - 04
Experiment Title: - Write C++ Program to Check Whether Number is Even or Odd.
********************************************************************
Theory: -
In computer programming, we use the if...else statement to run one block of code under
certain conditions and another block of code under different conditions.
C++ if Statement
if (condition) {
// body of if statement
}
C++ if...else
The if statement can have an optional else clause. Its syntax is:
if (condition) {
// block of code if condition is true
}
else {
// block of code if condition is false
}
The if...else statement is used to execute a block of code among two alternatives. However, if
we need to make a choice between more than two alternatives, we use the if...else
if...else statement.
The syntax of the if...else if...else statement is:
if (condition1) {
// code block 1
}
else if (condition2){
// code block 2
}
else {
// code block 3
}
Here,
// outer if statement
if (condition1) {
// statements
// inner if statement
if (condition2) {
// statements
}
}
Notes:
• We can add else and else if statements to the inner if statement as required.
• The inner if statement can also be inserted inside the outer else or else if statements (if
they exist).
• We can nest multiple layers of if statements.
Procedure: -
1. An if..else statement is used to check whether n % 2 == 0 is true or not.
2. If this expression is true, n is even.
3. Else, n is odd.
Conclusion: -
Here, if...else statement is used to check whether a number entered by the user is even or
odd.
Subject In-charge
Program: -
#include <iostream.h>
#include<conio.h>
using namespace std;
void main()
{
int n;
clrscr();
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
getch();
}
Output: -
Enter an integer: 23
23 is odd.
Enter an integer: 58
58 is even.
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]
Experiment No.: - 05
Experiment Title: - Write C++ Program to Find Largest Number Among Three
Numbers.
********************************************************************
Theory: -
To find the largest of the three number
• if
• if else
• nested if else statements
are used.
Here we write program using nested if else statement.
Flowchart: -
Procedure: -
1. Start
2. Read the three numbers to be compared, as A, B and C.
3. Check if A is greater than B.
Conclusion: -
Here, we learn to find the largest number among three numbers using nested if else
statements.
Subject In-charge
Program: -
#include <iostream.h>
#include<conio.h>
using namespace std;
void main()
{
int a, b, c;
clrscr();
cout << "Enter the three numbers a, b & c" <<endl;
cin >> a >> b >> c;
if(a >= b)
{
if(a >= c)
{
cout << "The Largest Among Three Numbers is : " << a << endl;
}
else
{
cout << "The Largest Among Three Numbers is : " <<c << endl;
}
}
else
{
if(b >= c)
{
cout << "The Largest Among Three Numbers is : " <<b << endl;
}
else
{
cout << "The Largest Among Three Numbers is : " <<c << endl;
}
}
getch();
}
Output: -
Enter the three numbers a, b & c
75 200 134
The Largest Among Three Numbers is : 200
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]
Experiment No.: - 06
Experiment Title: - Write C++ Program to Create a simple class and object.
********************************************************************
Theory: -
The main purpose of C++ programming is to add object orientation to the C programming
language and classes are the central feature of C++ that supports object-oriented programming
and are often called user-defined types.
A class is used to specify the form of an object and it combines data representation and methods
for manipulating that data into one neat package. The data and functions within a class are called
members of the class.
4 Copy Constructor
The copy constructor is a constructor which creates an object by initializing it with an
object of the same class, which has been created previously.
5 Friend Functions
A friend function is permitted full access to private and protected members of a class.
6 Inline Functions
With an inline function, the compiler tries to expand the code in the body of the function
in place of a call to the function.
7 this Pointer
Every object has a special pointer this which points to the object itself.
Conclusion: -
Here, we learn how to create a class, create object of that class and calling the
member function in main function.
Subject In-charge
Program: -
#include <iostream.h>
#include<conio.h>
using namespace std;
class Hello
{
public:
void sayHellow( )
{
cout<< “Hello World” << endl;
}
};
void main()
{
Hello h;
h.sayHello( );
getch();
}
Output: -
Hello World
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]
Experiment No.: - 07
********************************************************************
Theory: -
Adding or finding the sum of two numbers in Java is one of the fundamental aspects in
Java. The given two numbers will be added and the sum will be displayed. The size of the data
type must be kept in mind while adding the two numbers. If the size of the answer exceeds the
size of the data type, overflow occurs.
Procedure: -
1. Declare and initialize two variables to be added.
2. Declare another variable to store the sum of numbers.
3. Apply mathematical operator (+) between the declared variable and store the
result.
Conclusion: -
This is the easiest way to find the sum of two numbers in Java. We will initialise and
declare the value in the program itself. Here the input is not taken from the user.
Subject In-charge
Program: -
Output: -
Experiment No.: - 08
Experiment Title: - Write Java Program to Accept marks from user, if Marks
********************************************************************
Theory: -
Java Scanner
Scanner class in Java is found in the java.util package. Java provides various ways to read
input from the keyboard, the java.util.Scanner class is one of them.
The Java Scanner class breaks the input into tokens using a delimiter which is whitespace
by default. It provides many methods to read and parse various primitive values.
The Java Scanner class is widely used to parse text for strings and primitive types using a
regular expression. It is the simplest way to get input in Java. By the help of Scanner in Java, we
can get input from the user in primitive types such as int, long, double, byte, float, short, etc.
The Java Scanner class extends Object class and implements Iterator and Closeable
interfaces.
4 The Java Scanner class provides nextXXX() methods to return the type of value such as
nextInt(), nextByte(), nextShort(), next(), nextLine(), nextDouble(), nextFloat(), nextBoolean(),
etc. To get a single character from the scanner, you can call next().charAt(0) method which
returns a single character.
To get the instance of Java Scanner which reads input from the user, we need to pass the input
stream (System.in) in the constructor of Scanner class. For Example:
To get the instance of Java Scanner which parses the strings, we need to pass the strings in the
constructor of Scanner class. For Example:
SN Constructor Description
10) Scanner(Path source, String charsetName) It constructs a new Scanner that produces
values scanned from the specified file.
Java Scanner Class Methods
23) boolean nextBoolean() It scans the next token of the input into a
boolean value and returns that value.
Conclusion: -
Here we learn Scanner class to read marks entered from user. Also we declare
result about “Pass” or “Fail”.
Subject In-charge
Program: -
Output: -
Experiment No.: - 09
********************************************************************
Theory: -
Java If-else Statement
In Java if statement is used to test the condition. It checks boolean condition: true or false.
There are various types of if statement in Java.
o if statement
o if-else statement
o if-else-if ladder
o nested if statement
Java if Statement
The Java if statement tests the condition. It executes the if block if condition is true.
Syntax:
1. if(condition){
2. //code to be executed
3. }
Java if-else Statement
The Java if-else statement also tests the condition. It executes the if block if condition is
true otherwise else block is executed.
Syntax:
1. if(condition){
2. //code if condition is true
3. }else{
4. //code if condition is false
5. }
Java if-else-if ladder Statement
The if-else-if ladder statement executes one condition from multiple statements.
Syntax:
1. if(condition1){
2. //code to be executed if condition1 is true
3. }else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. else{
11. //code to be executed if all the conditions are false
12. }
The nested if statement represents the if block within another if block. Here, the inner if
block condition executes only when outer if block condition is true.
Syntax:
1. if(condition){
2. //code to be executed
3. if(condition){
4. //code to be executed
5. }
6. }
Procedure: -
1. Three numbers stored in variables a, b and c respectively.
2. Then, to find the largest, the following conditions are checked using if else
statements
3. If a is greater or equals to b,
a. and if a is greater or equals to c, a is the greatest.
b. else, c is the greatest.
4. Else,
Conclusion: -
Here we learn to find the largest among three numbers using nested if..else
statement in Java.
Subject In-charge
Program: -
Output: -
Experiment No.: - 10
********************************************************************
Theory: -
Java do-while Loop
The Java do-while loop is used to iterate a part of the program repeatedly, until the specified
condition is true. If the number of iteration is not fixed and you must have to execute the loop at
least once, it is recommended to use a do-while loop.
Java do-while loop is called an exit control loop. Therefore, unlike while loop and for loop, the
do-while check the condition at the end of loop body. The Java do-while loop is executed at least
once because condition is checked after loop body.
Syntax:
1. do{
2. //code to be executed / loop body
3. //update statement
4. }while (condition);
1. Condition: It is an expression which is tested. If the condition is true, the loop body is executed
and control goes to update expression. As soon as the condition becomes false, loop breaks
automatically.
Example:
i <=100
2. Update expression: Every time the loop body is executed, the this expression increments or
decrements loop variable.
Example:
i++;
Note: The do block is executed at least once, even if the condition is false.
Conclusion: -
Here we learn to find the sum of first 10 even numbers using do-while loop in
Java.
Subject In-charge
Program: -
Output: -
C:\Users\visha\OneDrive\Desktop\Java Prog>javac javaexp4.java
Experiment No.: - 11
********************************************************************
Theory: -
Java while Loop
Java while loop is a control flow statement that allows code to be executed
repeatedly based on a given Boolean condition. The while loop can be thought of as a
repeating if statement. While loop in Java comes into use when we need to repeatedly
execute a block of statements. The while loop is considered as a repeating if
statement. If the number of iterations is not fixed, it is recommended to use the while
loop.
Syntax:
while (test_expression)
// statements
update_expression;
Example:
i <= 10
Example:
i++;
Conclusion: -
Here we learn to display Multiplication table of 15 using while loop in Java.
Subject In-charge
Program: -
Output: -
C:\Users\visha\OneDrive\Desktop\Java Prog>javac javaexp5.java
Experiment No.: - 12
Experiment Title: - Write Java Program to Display the sum of elements of arrays.
********************************************************************
Theory: -
Java Arrays
Java array is an object which contains elements of a similar data type. Additionally, the
elements of an array are stored in a contiguous memory location. It is a data structure where we
store similar elements. We can store only a fixed set of elements in a Java array.
Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd
element is stored on 1st index and so on.
Unlike C/C++, we can get the length of the array using the length member. In C/C++, we
need to use the sizeof operator.
In Java, array is an object of a dynamically generated class. Java array inherits the Object
class, and implements the Serializable as well as Cloneable interfaces. We can store primitive
values or objects in an array in Java. Like C/C++, we can also create single dimensional or
multidimensional arrays in Java.
Moreover, Java provides the feature of anonymous arrays which is not available in C/C++.
Advantages
o Code Optimization: It makes the code optimized, we can retrieve or sort the data
efficiently.
o Random access: We can get any data located at an index position.
Disadvantages
o Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its
size at runtime. To solve this problem, collection framework is used in Java which grows
automatically.
1. arrayRefVar=new datatype[size];
We can declare, instantiate and initialize the java array together by:dempt
We can also print the Java array using for-each loop. The Java for-each loop prints the
array elements one by one. It holds an array element in a variable, then executes the body of
the loop.
1. for(data_type variable:array){
2. //body of the loop
3. }
Conclusion: -
Here we learn to calculate sum of elements of array in Java.
Subject In-charge
Program: -
Output: -
C:\Users\visha\OneDrive\Desktop\Java Prog>javac javaexp6.java