Showing posts with label Computer Sciemce 10th Notes - Computer Sciemce - Short / Detailed Questions Answers. Show all posts
Showing posts with label Computer Sciemce 10th Notes - Computer Sciemce - Short / Detailed Questions Answers. Show all posts

FUNCTIONS:Chapter # 05: COMPUTER SCIENCE 10TH – DETAILED QUESTION ANSWERS

 COMPUTER SCIENCE 10TH – DETAILED QUESTION ANSWERS

Chapter # 05: FUNCTIONS

Q.1: What do you know about functions?

Ans:
INTRODUCTION TO FUNCTIONS
A function is a block of code that performs a particular task. It is also called a method or a sub-routine or a procedure, etc. There are some situations when we need to write a particular block of code more than once in our program. This may lead to bugs and irritation for the programmer. C++ language provides an approach in which you need to declare and define a group of statements once and that can be called and used whenever required. This saves both time and space. Every C++ program has at least one function, which is main(), and all the programs can define additional functions.

Q.2: Write down the advantages of functions.

Ans:
ADVANTAGES OF FUNCTIONS
There are some advantages of using functions.

  1. The complexity of the entire program can be divided into simple subtasks, and function subprograms can be written for each subtask.
  2. Functions help us to avoid unnecessary repetition of codes. It helps in code reusability.
  3. Functions can have inputs and outputs and can process information.
  4. The functions are short, easier to write.
  5. The function are understandable and can be debugged.

Q.3: Define the types of functions.

Ans:
TYPES OF FUNCTIONS
There are two types of functions in C++ programming:

  1. Predefined / Built-in functions
  2. User-defined Functions

Q.4: What is predefined or built-in function?

Ans:
PREDEFINED/BUILT-IN FUNCTIONS
The built-in functions are standard library functions to handle tasks such as mathematical computations, I/O processing, string handling, etc. These functions are defined in the header file and don't need to declare and define. The definitions of most common functions are found in the cmath and cstdlib libraries.

Q.5: Describe user-defined functions.

Ans:
USER-DEFINED FUNCTIONS
C++ allows programmers to define functions to do a task relevant to their programs. Such functions created by the user are called user-defined functions. These functions need declaration and definition. A user can create as many user-defined functions as needed.

Q.6: Write down the major elements of user-defined functions.

Ans:
The user-defined function consists of two parts:

  1. Function declaration (Function prototype)
  2. Function definition

Q.7: Define function declaration.

Ans:
FUNCTION DECLARATION/PROTOTYPE
The declaration of a function is called its prototype. Using a function in programs requires that we have to declare the function first. It is declared before the main() function.

The general structure of the function prototype is:

return_datatype function_name(arguments)

Example:

cpp

int heading(void);
  • return type: Specifies the type of value the function will return.
  • function name: The name of the function.
  • parameters (arguments): The inputs to the function (if any).
  • semicolon: The prototype ends with a semicolon because it is a statement.

It has four main components. These are:

  1. Return data type
  2. Name of the function
  3. Arguments (parameters)
  4. Statement terminator

RETURN DATA TYPE
The return data type of the function is the type of return value. If the function does not return a value, the type is defined as void.

FUNCTION NAME
The function name is any identifier followed by parentheses without any spaces in between.

ARGUMENTS/PARAMETERS
The arguments or parameters come inside the parentheses, preceded by their types and separated by commas. If the function does not use any arguments, the word void is used inside the parentheses.

STATEMENT TERMINATOR
Semicolon (;) is used as a statement terminator at the end of function declaration or prototype.

FUNCTION DEFINITION
A function definition is the function itself. Function definition consists of a function header, a function body, and a code block. The definition begins with a header which is exactly the same as the function prototype except it must not be terminated with a semicolon (;). It has three main components: return type of the function, name of the function, and arguments/parameters. The body of the function includes statements enclosed in braces and is defined before or after the main function.

HEADER
int heading(void)

  • return type
  • function name
  • parameters (arguments)
  • No semicolon (;) after header

BODY

cpp

{ // Statements; return 0; }

Q.8: Define function calling.
Ans:
FUNCTION CALL
A user-defined function is called from the main program simply by using its name, including the parentheses which follow the name. The parentheses are necessary so that the compiler knows you are referring to a function.

GENERAL SYNTAX

cpp

function_name();

Q.9: Define function arguments.
Ans:
FUNCTION PASSING ARGUMENTS OR PARAMETERS
Sending data to a function is called passing arguments. It is basically sending variables, constants, or expressions whose value are needed by the function. Actual values that are passed to the function as arguments with the function call statement are known as actual arguments. These values are received in variables of the header of the function definition. These receiving variables or arguments are called formal arguments.

Q.10: Define function return value.
Ans:
RETURNING VALUE FROM FUNCTIONS
In C++, when a function completes its execution, it can return a value to the calling function using the return keyword. The return type must be defined with the function header in the declaration and definition.

Q.11: Write down the differences between function definition and function call.
Ans:
DIFFERENCES BETWEEN FUNCTION DEFINITION AND FUNCTION CALL

FUNCTION DEFINITIONFUNCTION CALL
The function definition is the part of the function where it is actually defined.Invoke the code of the function; this is called a function call.
A user-defined function may define before or after the main function.A user-defined function is called from the main program simply by using its name.
Syntax: data_type function_name(arguments) { statements; }Syntax: variable_name = function_name(arguments);

Example:

cpp
int sum(int a, int b) { int c; c = a + b; return c; }

Example (Function Call):

cpp

z = sum(x, y);

Q.12: Define function types based on arguments and return value.
Ans:
DIFFERENT WAYS TO USE USER-DEFINED FUNCTION BASED ON ARGUMENTS AND RETURN TYPE

Functions can be used in four variations in C++ based on arguments and return value from the functions:

  • No return value and no passing arguments

    cpp

    void function_name(void);
  • Return value but no passing arguments

    cpp

    int/float/char function_name(void);
  • No return value but passing arguments

    cpp

    void function_name(int, float, char);
  • Return value and passing arguments

    cpp

    int/float/char function_name(int, float, char);

Q.13: Write a program using predefined functions.
Ans:
EXAMPLE OF PREDEFINED FUNCTIONS

cpp

#include<iostream> #include<cmath> using namespace std; int main() { int sq; cout << "Enter an integer to find square root: "; cin >> sq; cout << "\nThe Square root of a given integer is: " << sqrt(sq); return 0; }

Q.14: Write a program using user-defined functions.
Ans:
EXAMPLE OF USER-DEFINED FUNCTIONS

cpp

#include<iostream> using namespace std; // function prototype int add(int a, int b); int main() { int x, y, sum; cout << "Enter 1st number: "; cin >> x; cout << "Enter 2nd number: "; cin >> y; sum = add(x, y); cout << "The sum of two numbers is = " << sum << "\n"; return 0; } // function definition
int add(int a, int b) { int c; c = a + b; return (c); }

Q.15: Write down the differences between predefined function and user-defined function.
Ans:
DIFFERENCES BETWEEN PREDEFINED AND USER-DEFINED FUNCTIONS

PREDEFINED FUNCTIONUSER-DEFINED FUNCTION
It is also called built-in functions or library functions.These are called end-user functions created by programmer.
It cannot be edited or modified.It can be edited or modified by programmer.
Function definition is not required because definition is a part of compiler.Function declaration and definition are needed in user-defined function.
Example: pow(), sqrt(), getche(), etc.Example: add(), int sum(); float avg(float a, float b), etc.

Q.16: Define variables in C++.
Ans:
VARIABLES IN USER-DEFINED FUNCTIONS
In C++ structured programming, there are two types of variables used:

  1. Local variable
  2. Global variable

Q.17: What is a local variable? Also write its example.
Ans:
LOCAL VARIABLES
Local variable is defined as a type of variable declared within a programming block or functions. It can only be used inside the function or code block in which it is declared. The local variable exists until the block of the function is under execution. After that, it will be destroyed automatically.

LOCAL VARIABLES EXAMPLE

cpp

#include<iostream> using namespace std; int main() { int b; // local variable cout << "Enter an integer: "; cin >> b; cout << "\nThe given integer is: " << b; return 0; }

Q.18: What is a global variable? Also write its example.

Ans:
GLOBAL VARIABLES
A global variable in the program is a variable defined outside the subroutine or function. It has a global scope, meaning it holds its value throughout the lifetime of the program. Hence, it can be accessed throughout the program by any function defined within the program.

GLOBAL VARIABLES EXAMPLE

cpp

#include<iostream> using namespace std; int add(int a); int b = 10; // Global Variable int main() { int x, sum; // Local Variables cout << "Enter 1st number: "; cin >> x; sum = add(x); cout << "The sum of two numbers is = " << sum << "\n"; return 0; } // function definition
int add(int a) { int c; // Local Variable c = a + b; return (c); }

CONTROL STRUCTURE-Chapter # 04- COMPUTER SCIENCE 10TH – DETAILED QUESTION ANSWERS

 COMPUTER SCIENCE 10TH – DETAILED QUESTION ANSWERS

CONTROL STRUCTURE
Chapter # 04


Q.1: What are control structures?

Ans:
CONTROL STRUCTURES / STATEMENTS
Control structures control the flow of execution in a program or function. Control structures are used to repeat any block of code, transfer control to a specific block of code, and make a choice by selection. There are three basic control structures in C++ programming:

  1. Selection / Decision Making Control Structure
  2. Loops / Iteration Control Structure
  3. Jumps

Q.2: Define selection or decision-making control structure and name its types.

Ans:
SELECTION / DECISION-MAKING CONTROL STRUCTURES / STATEMENTS
The selection control structure allows a number of conditions, which lead to a selection of one out of several alternatives. There are three types of selection control structures:

  1. If selection structure / statement
  2. If-else selection structure / statement
  3. Switch selection structure / statement

Q.3: Define if selection structure with syntax, flow diagram, and example.

Ans:

1. if SELECTION STATEMENT
An if statement is a conditional statement that tests a particular condition. Whenever that condition evaluates as true, it performs an action, but if it is not true, then the action is skipped.

SYNTAX:

cpp

if (condition) { Statement(s); }

FLOW DIAGRAM:

  • A decision is made based on a condition:
    • If True, the Statement(s) are executed.
    • If False, it goes directly to the End IF.

if SELECTION STATEMENT EXAMPLE:

cpp

#include<iostream> using namespace std; int main() { int num; cout << "Enter an integer number: "; cin >> num; if (num > 0) { cout << "You entered a positive integer: " << num << "\n"; } return 0; }

OUTPUT:

bash

Enter an integer number: 10 You entered a positive integer: 10

Q.4: Define nested if selection structure with syntax and example.

Ans:
NESTED if STATEMENT
A condition can be written as deeply as needed within the body of another statement. This is called a nested if statement.

SYNTAX: The general syntax of a nested if statement is:

cpp

if (condition 1) { if (condition 2) { statements; } }

NESTED if STATEMENT EXAMPLE:

cpp

#include<iostream> using namespace std; int main() { int exp, status; cout << "Enter experience: "; cin >> exp; cout << "\nEnter status: "; cin >> status; if (exp >= 4) { if (status >= 2) { cout << "\nBonus Given to Employee" << "\n"; } } return 0; }

OUTPUT:

mathematica

Enter experience: 6 Enter status: 3 Bonus Given to Employee

Q.5: Define if-else selection structure with syntax, flow diagram, and example.

Ans:
if-else SELECTION STATEMENT
An if-else selection structure performs a certain action when the condition is true and a different action when the condition is false.

SYNTAX:

cpp

if (condition) { statement(s); } else { statement(s); }

Flow Diagram:

  • If the condition is True, it executes the If Body.
  • If the condition is False, it executes the Else Body.
  • The program then continues to the statement just below the if-else structure.

if-else SELECTION STATEMENT EXAMPLE:

cpp

#include<iostream> using namespace std; int main() {

(Continued on the next page or image.)

int number; cout << "Enter an integer: "; cin >> number; if (number >= 0) { cout << "The number is a positive integer: " << number << "\n"; } else { cout << "The number is a negative integer: " << number << "\n"; } cout << "This line is always printed."; return 0; }

OUTPUT:

vbnet

Enter an integer: -5 The number is a negative integer: -5 This line is always printed.

Q.6: Define else-if selection structure with example.

Ans:
else-if SELECTION STATEMENT
Nested if-else statements test for multiple conditions by placing if-else statements inside if-else statements. When a condition is evaluated as true, the corresponding statements are executed, and the rest of the structure is skipped. This structure is also referred to as the if-else-if ladder.

else-if SELECTION STATEMENT EXAMPLE:

cpp

#include<iostream> using namespace std; int main() { int per; cout << "\nEnter your percentage: "; cin >> per; if (per >= 80) {

(Continued on the next page or image.)

cout << "Your grade is A+."; } else if (per >= 70) { cout << "Your grade is A."; } else if (per >= 60) { cout << "Your grade is B."; } else if (per >= 50) { cout << "Your grade is C."; } else { cout << "Failed."; } return 0; }

OUTPUT:

csharp

Enter your percentage: 70 Your grade is A

Q.7: Define switch statement with syntax, flow diagram, and example.

Ans:
SWITCH STATEMENT
A switch statement is a control statement that allows selection of only one choice among many given choices. The expression in a switch evaluates to return an integer or character value, which is then compared to the values present in different cases. It executes the block of code that matches the case value. If there is no match, then the default block is executed (if present).

SYNTAX:

cpp

switch (variable) { case constant 1:

(Continued on the next page or image.)

statement(s); break; } case constant 2: { statement(s); break; } default: { statement(s); break; } }

FLOW DIAGRAM

  • Switch (Conditional Expression)
    • Checks Case Condition 1:
      • If True, executes Statement 1 and break.
      • If False, checks Case Condition 2.
    • Checks Case Condition 2:
      • If True, executes Statement 2 and break.
      • If False, continues to the next case.
    • Checks Case Condition n:
      • If True, executes Statement n and break.
      • If False, goes to the Default.
    • Default:
      • Executes Default Statement if no case matches.

After the switch case, control goes to the Statement just below Switch Case.

SWITCH STATEMENT EXAMPLE:

cpp

#include<iostream> using namespace std; int main() { char op; float num1, num2; cout << "Enter an operator (+, -, *, /): "; cin >> op; cout << "Enter two numbers: " << "\n"; cin >> num1 >> num2; switch (op) { case '+': cout << num1 << " + " << num2 << " = " << num1 + num2; break; case '-': cout << num1 << " - " << num2 << " = " << num1 - num2; break; case '*': cout << num1 << " * " << num2 << " = " << num1 * num2; break; case '/': cout << num1 << " / " << num2 << " = " << num1 / num2; break; default: cout << "Error! The operator is not correct"; } return 0; }

OUTPUT:

mathematica

Enter an operator: + Enter two numbers: 10 15 10 + 15 = 25

Q.8: Write down the difference between if-else and switch statement.

DIFFERENCES BETWEEN if-else & SWITCH STATEMENT

if-elseSWITCH
If-else statement is used to select among two alternatives.The switch statement is used to select among multiple alternatives.
If-else can have values based on constraints.Switch can have values based on user choice.
Float, double, char, int and other data types can be used in if-else condition.Only int and char data types can be used in switch block.
It is difficult to edit the if-else statement, if the nested if-else statement is used.It is easy to edit switch cases as they are recognized easily.

Q.9: What are loops or iteration and define its types?

LOOP / ITERATION CONTROL STRUCTURE

Iteration or loop in computer programming is a process wherein a set of instructions or structures are repeated in a sequence a specified number of times until a condition is true. When the set of instructions is executed again, it is called an iteration. A loop statement allows us to execute a statement or a group of statements multiple times.

C++ provides the following types of loops to handle looping requirements:

  1. for loop
  2. while loop
  3. do-while loop

Q.10: Describe for loop with syntax, flow diagram and example.

for LOOP

A for loop is a repetition or iteration control structure that repeats a statement or block of statements for a specified number of times. The for-loop statement includes the initialization of the counter, the condition, and the increment. The for loop is commonly used when the number of iterations is known.

SYNTAX

The general syntax of for loop is:

cpp

for (initialization; test condition; increment/decrement) { // Statements }

Flow Diagram:

The flow diagram illustrates the execution of the for loop:

  1. Condition is checked:

    • If true, the statement is executed, followed by increment/decrement.
    • If false, the loop ends.
  2. The process repeats until the condition is false.

for Loop Example:

cpp

#include<iostream> using namespace std; int main() { int i; for (i = 1; i <= 10; i++) { cout << i << " "; } return 0; }

OUTPUT:


1 2 3 4 5 6 7 8 9 10

Q.11: Describe while loop with syntax, flow diagram, and example.

Ans. While Loop

The while loop allows the programmer to specify that an action is to be repeated while some conditions remain true. It is used when the exact number of loop repetitions before the loop execution begins are not known.

Syntax:

vbnet

The general syntax of a while loop is: while(condition) { statement(s); }

Flow Diagram:

  1. Initialization
  2. Check condition:
    • If true, execute the code inside the body of the while loop.
    • If false, end the loop.

while Loop Example:

cpp

#include<iostream> using namespace std; int main() { int i = 1; while (i <= 10) { cout << i << " "; i++; } return 0; }

Q.12: Describe do while loop with syntax, flow diagram, and example.

Ans. do while Loop

A do-while loop is similar to a while loop, except that a do-while loop is guaranteed to execute at least one time, even if the condition fails for the first time. Unlike for and while loops, which test the loop condition at the start of the loop, the do-while loop checks its condition at the end of the loop.

Syntax:

vbnet

The general syntax of a do-while loop is: do { statement(s); } while(condition);

Flow Diagram:

  1. Initialization
  2. Execute Code Inside Body of While Loop
  3. Check condition:
    • If true, go back to the code inside the loop.
    • If false, end the loop.

do while Loop Example

cpp

#include<iostream> using namespace std; int main() { int i = 1; do { cout << i << " "; i++; } while (i <= 10); return 0; }

OUTPUT


1 2 3 4 5 6 7 8 9 10

Q.13: Describe nested loop with example.

Ans. Nested Loops

When one for, while or do while loop is existed within another loop, they are said to be nested. The inside loop is completely repeated for each repetition of the outside loop.

Nested Loop Example

cpp

#include<iostream> using namespace std; int main() { int row, column; for (row = 1; row <= 5; row++) { for (column = 1; column <= 3; column++) { cout << " * "; } } return 0; }

OUTPUT

markdown

* * * * * * * * * * * * * * *

Q.14: Write down the differences between for, while, and do while loop.

for loopwhile loopdo while loop
1. It is a pre-test loop because the condition is tested at the start of the loop.1. It is a pre-test loop because the condition is tested at the start of the loop.1. It is a post-test loop because the condition is tested at the end of the loop, which executes the loop at least once.
2. It is known as an entry-controlled loop.2. It is known as an entry-controlled loop.2. It is known as an exit-controlled loop.
3. If the condition is not true the first time, then the control will never enter the loop.3. If the condition is not true the first time, then the control will never enter the loop.3. Even if the condition is not true for the first time, the control will enter the loop at least once.
4. There is no semicolon after the condition in the syntax of the for loop.4. There is no semicolon after the condition in the syntax of the while loop.4. There is a semicolon after the condition in the syntax of the do while loop.
5. Initialization and updating are part of the syntax.5. Initialization and updating are not part of the syntax.5. Initialization and updating are not part of the syntax.

Q.15: What are jump statements?

Ans. Jump Statements

These statements change the normal execution of the program and jump over a specific part of the program.

Following are the jump statements used in C++:

  • break
  • continue
  • goto
  • return
  • exit()
  • Q.16: Define break statement with example.

    Ans. break STATEMENT
    The break statement allows you to exit a loop or a switch statement from any point within its body, bypassing its normal termination expression. It can be used within any C++ structure.

    break EXAMPLE

    cpp

    #include<iostream> using namespace std; int main() { int count; for(count = 1; count <= 100; count++) { cout << count; if(count == 10) break; } return 0; }

    Q.17: Define continue statement with example.

    Ans. continue STATEMENT
    The continue statement is just the opposite of the break statement. Continue forces the next iteration of the loop to take place, skipping the remaining statements of its body.

    continue EXAMPLE

    cpp

    #include<iostream> using namespace std; int main()
    cpp

    { int count; for(count = 1; count <= 10; count++) { if(count == 5) continue; cout << count; } return 0; }

    Q.18: Define goto statement with Example.

    Ans. goto STATEMENT
    In C++ programming, the goto statement is used for altering the normal sequence of program execution by transferring control to some other parts of the program.

    goto EXAMPLE

    cpp

    #include<iostream> using namespace std; int main() { float num, average, sum = 0.0; int i, n; cout << "Maximum number of inputs: "; cin >> n; for(i = 1; i <= n; i++) { cout << "Enter number " << i << ": "; cin >> num; if(num < 0.0) { // Control of the program moves to jump: goto Jump; } sum += num; } Jump: average = sum / (n);
    cout << "\n Average =" << average; return 0; }

    Q.19: Define return statement with syntax.

    Ans. return STATEMENT
    The return statement returns the flow of execution to the function from where it is called. This statement does not mandatorily need any conditional statements. As soon as the statement is executed, the flow of the program stops immediately and returns the control from where it was called.

    SYNTAX

    cpp

    return (expression / value);

    Q.20: Define exit() statement with Syntax.

    Ans. exit() STATEMENT
    The exit function, declared in <stdlib.h>, terminates a C++ program. The value supplied as an argument to exit is returned to the operating system as the program’s return code or exit code.

    SYNTAX

    cpp

    void exit(int);

    INPUT / OUTPUT HANDLING IN C++ Chapter # 03- COMPUTER SCIENCE 10TH – DETAILED QUESTION ANSWERS

     COMPUTER SCIENCE 10TH – DETAILED QUESTION ANSWERS

    INPUT / OUTPUT HANDLING IN C++
    Chapter # 03


    Q.1: Describe basic structure of C++ program.

    Ans:
    BASIC STRUCTURE OF C++ PROGRAM
    A C++ program is mainly divided into three parts:

    1. Preprocessor Directives
    2. Main Function Header
    3. Body of program / Function

    Basic structure of C++ program is given below:

    cpp
    #include<iostream> using namespace std; int main() { statements; return 0; }

    Q.2: Describe elements of basic structure.

    Ans:
    EXPLANATION OF BASIC STRUCTURE

    1. #include<iostream>
      The statement starts with # symbol, which is called a preprocessor directive. This statement tells the preprocessor to include the contents of the iostream header file in the program before compilation. This file is required for input-output statements.

    2. using namespace std;
      This statement is used to instruct the compiler to use the standard namespace. A namespace is a declarative location that provides a scope to the identifiers. Namespace std contains all the classes, objects, and functions of the standard C++ library.

    3. int main()
      This statement is a function and is used for the execution of a C++ program. int means it returns an integer type value.

    1. {
      This symbol represents the start of the main function.

    2. statements;
      Statements are instructions that perform a particular task. The statement terminator (;) is used to end every statement in a C++ program.

    3. return 0;
      This statement is used to return the value to the operating system. By default, the main function returns an integer value 0.

    4. }
      This symbol represents the end of the main function.

    Q.3: Define comments in C++. Also define its types.

    Ans:
    COMMENTS IN C++
    Comments are special remarks that help to understand different parts of the code in a C++ program. Comments are ignored by the compiler. In C++, there are two types of comments:

    1. Single Line Comment
    2. Multi Line Comment

    Q.4: Define single line comment in C++.

    Ans:
    SINGLE LINE COMMENT
    This type is used to write a single line comment. Double slash (//) is used at the start of each single line comment.

    Example:

    cpp
    // This is my first C++ program // This program displays a statement on screen #include<iostream> int main() { puts("My First C++ Program"); return 0; }

    Q.5: Define multi-line comment in C++.

    Ans:
    MULTI LINE COMMENT
    This type is used to write multi-line comments. Symbols (/* and */) are used at the start and end of comment statements.

    Example:

    cpp
    /* This is my first C++ program This program displays a statement on screen */ #include<iostream> int main() { puts("My First C++ Program"); return 0; }

    Q.6: What do you mean by input/output statement in C++?

    Ans:
    INPUT/OUTPUT STATEMENTS IN C++
    Input and output statements are used to perform input and output operations in C++. These input/output statements are stored in header files like <iostream>. At the beginning of every program, these header files must be included.

    Q.7: Define output functions or statements in C++. Define cout statement and puts statement with syntax and example in C++.

    Ans:
    OUTPUT FUNCTION / STATEMENTS IN C++
    cout STATEMENT
    cout stands for "Character Output". In C++, cout sends formatted output to standard output devices, such as the screen. The cout object is used along with the insertion operator (<<) for displaying output.

    Syntax:

    cpp
    cout << variable; or cout << expression / string;

    Example:

    cpp
    cout << "This is C++ Programming"; cout << num1;

    puts() STATEMENT
    This function is used to print the string output. After printing, a new line is automatically inserted.

    Syntax:

    cpp
    puts("string constant");

    Example:

    cpp
    puts("This is C++ Programming");

    Q.8: Define input functions or statements in C++.

    Ans:
    INPUT FUNCTION / STATEMENTS IN C++
    The following are the input functions/statements in C++:

    • cin STATEMENT
    • getchar() STATEMENT
    • getch() STATEMENT
    • getche() STATEMENT
    • gets() STATEMENT

    Q.9: Define cin statement with syntax and example in C++.

    Ans:
    cin STATEMENT
    cin stands for "Character Input". In C++, cin reads formatted data as input from the keyboard. The cin object is used along with the extraction operator (>>) to accept data from the standard input device.

    Syntax:

    cpp
    cin >> variable;

    Example:

    cpp
    #include<iostream> using namespace std; int main() { int b; cin >> b; // cin takes input in "b" variable return 0; }

    Q.10: Define getchar() statement with example in C++.

    Ans:
    getchar() STATEMENT
    The getchar() function reads the available character from the keyboard. This function reads only a single character at a time. When the user presses the key, getchar() requires the Enter key to be pressed. This function is defined in the <stdio.h> header file.

    Example:

    cpp

    #include<iostream> #include<stdio.h> using namespace std; int main() { char b; cout << "\nEnter a character:"; b = getchar(); cout << "\nInput character is " << b; return 0; }

    Q.11: Define getch() statement with example in C++.

    Ans:
    getch() STATEMENT
    The getch() function reads the character from the keyboard. This function reads only a single character and does not print it on the screen. This function takes input and does not require the Enter key to be pressed. This function is defined in the <conio.h> header file.

    Example:

    cpp
    #include<iostream> #include<conio.h> using namespace std; int main() { char ch; cout << "\nEnter a character:"; ch = getch(); cout << "\nInput character is " << ch; return 0; }

    Q.12: Define getche() statement with example in C++.

    Ans:
    getche() STATEMENT
    The getche() function reads a character from the keyboard and echoes it on the screen. It reads only a single character and displays it on the screen. This function takes input and does not require the Enter key to be pressed. This function is defined in the <conio.h> header file.

    Example:

    cpp

    #include<iostream> #include<conio.h> using namespace std; int main() { char ch; cout << "\nEnter a character:"; ch = getche(); cout << "\nInput character is " << ch; return 0; }

    Q.13: Define gets() statement with syntax and example in C++.

    Ans:
    gets() STATEMENT
    This function is used to read characters or a string input and stores them until a newline character is found. This function is defined in the <cstdio> (or <stdio.h>) header file.

    Syntax:

    cpp

    gets("variable");

    Example:

    cpp

    #include<iostream> #include<conio.h> using namespace std; int main() { char name[25]; cout << "\nEnter your name:"; gets(name); cout << "\nYour Name is " << name; return 0; }

    Q.14: What is statement terminator?

    Ans:
    STATEMENT TERMINATOR (;)
    In C++, a statement terminator is used to end the statement. Statements are terminated with a semicolon (;) symbol. Every statement in C++ must be terminated; otherwise, an error message will occur.

    Q.15: Write down in detail about escape sequences in C++.

    Ans:
    ESCAPE SEQUENCES
    Escape sequences are used to control the cursor movement on the screen by using special codes. An escape sequence is a special non-printing character consisting of the escape character (the backslash \) and a second (code) character. The list of escape sequences is given below:

    Escape SequenceExplanation with Example
    \nNewline. Positions the cursor at the beginning of the next line.
    c

    Example: `cout << "\n";` |

    | \t | Horizontal tab. Moves the cursor to the next tab stop.
    Example: cout << "\t"; | | \\ | Backslash. Inserts a backslash character in a string.
    Example: cout << "\\"; | | \a | Alert. Produces a beep sound or visible alert.
    Example: cout << "\a"; | | \b | Backspace. Moves the cursor back one position.
    Example: cout << "\b"; | | \r | Carriage Return. Moves the cursor to the beginning of the current line.
    Example: cout << "\r"; | | \' | Single Quotation. Prints an apostrophe (').
    Example: cout << "\'"; | | \" | Double Quotation. Prints a quotation mark (").
    Example: cout << "\""; |

    Q.16: What are operators? Write down the name of operators used in C++.

    Ans:
    OPERATORS IN C++
    Operators are symbols that tell the computer to execute certain mathematical or logical operations. A mathematical or logical expression is generally formed with the help of an operator. C++ programming offers a number of operators that are classified into the following categories:

    1. Arithmetic Operators
    2. Increment Operators
    3. Decrement Operators
    4. Relational Operators
    5. Logical/Boolean Operators
    6. Assignment Operators
    7. Arithmetic Assignment Operators

    Q.17: What are arithmetic operators?

    Ans:
    ARITHMETIC OPERATORS
    Arithmetic operators are used to perform mathematical operations. All operators use integer and floating-point data types except the remainder or modulus operator.

    OperatorOperationExample
    +Addition: It is used to perform addition.a + b
    -Subtraction: It is used to perform subtraction.a - b
    *Multiplication: It is used to perform multiplication.a * b
    /Division: It is used to perform division.a / b
    %Remainder or Modulus: Finds the remainder after integer division.a % b

    Q.18: Define all arithmetic operators with examples.

    Ans:
    SIMPLE CALCULATOR PROGRAM IN C++ USING ARITHMETIC OPERATORS

    cpp

    #include<iostream> #include<conio.h> #include<stdio.h> using namespace std; int main() { int a, b, add, sub, mul, rem; float div; cout << "\n\t SIMPLE CALCULATOR"; cout << "\n\t Enter the value of a: "; cin >> a; cout << "\n\t Enter the value of b: "; cin >> b; add = a + b; cout << "\n\t Addition of " << a << " and " << b << " is " << add; sub = a - b; cout << "\n\t Subtraction of " << a << " and " << b << " is " << sub; mul = a * b; cout << "\n\t Multiplication of " << a << " and " << b << " is " << mul; div = a / b; cout << "\n\t Division of " << a << " and " << b << " is " << div; rem = a % b; cout << "\n\t Remainder of " << a << " and " << b << " is " << rem; return 0; }

    OUTPUT:

    swift

    SIMPLE CALCULATOR Enter the value of a: 30 Enter the value of b: 20 Addition of 30 and 20 is 50 Subtraction of 30 and 20 is 10 Multiplication of 30 and 20 is 600 Division of 30 and 20 is 1 Remainder of 30 and 20 is 10

    Q.19: Define increment operators with example in C++.

    Ans:
    INCREMENT OPERATORS
    C++ provides the unary increment operator. It is used to increment a variable by 1. The increment operator is represented by ++ (double plus sign). The increment operators are used in two ways (postfix and prefix), summarized below:

    OperatorsExplanation
    ++a (Prefix)Increments a by 1, then uses the new value of a in the expression in which a resides.
    a++ (Postfix)Uses the current value of a in the expression in which a resides, then increments a by 1.

    EXAMPLE (PREFIX):

    cpp

    #include<iostream> using namespace std; int main() { int ch = 5; cout << "\nValue of ch is: " << ++ch; return 0; }

    OUTPUT:

    csharp

    Value of ch is 6

    EXAMPLE (POSTFIX):

    cpp

    #include<iostream> using namespace std; int main() { int ch = 5; cout << "\nValue of ch is: " << ch++; return 0; }

    OUTPUT:

    csharp

    Value of ch is 5

    Q.20: Define decrement operator in C++.

    Ans:
    DECREMENT OPERATORS
    C++ also provides the unary decrement operator. It is used to decrement a variable by 1. The decrement operator is represented by -- (double minus sign). The decrement operators are used in two ways (postfix and prefix), summarized below:

    OperatorsExplanation
    --a (Prefix)Decrements a by 1, then uses the new value of a in the expression in which a resides.
    a-- (Postfix)Uses the current value of a in the expression in which a resides, then decrements a by 1.

    Q.21: Define relational operators with example in C++.

    Ans:
    RELATIONAL OPERATORS
    Relational operators are used when we have to make comparisons. It is used to test the relation between two values. The result of comparison is True (1) or False (0). C++ programming offers the following relational operators:

    OperatorOperationsExample
    <Checks if the value on the left is less than the value on the right.a < b
    >Checks if the value on the left is greater than the value on the right.a > b
    <=Checks if the value on the left is less than or equal to the value on the right.a <= b
    >=Checks if the value on the left is greater than or equal to the value on the right.a >= b
    ==Checks the equality of two values.a == b
    !=Checks if the value on the left is not equal to the value on the right.a != b

    PROGRAM USING RELATIONAL OPERATOR IN C++

    cpp

    #include<iostream> using namespace std; int main() { int x = 20, y = 10; if (x > y) cout << "X is greater than Y"; else cout << "Y is greater than X"; return 0; }

    OUTPUT:

    csharp

    X is greater than Y

    Q.22: Define logical operators with example in C++.

    Ans:
    LOGICAL OPERATORS
    Logical operators are used when more than one condition needs to be tested, and based on that result, decisions have to be made. C++ programming offers three logical operators. They are:

    OperatorOperationsExpression
    &&Logical AND. The condition will be true if both expressions are true.1 if a == b && c == d; else 0
    ``
    !Logical NOT. The condition will be inverted; false becomes true and true becomes false.1 if !(a == 0); else 0

    PROGRAM USING LOGICAL OPERATORS IN C++

    cpp

    #include<iostream> #include<conio.h> using namespace std; int main() { int num1 = 30, num2 = 40; cout << "Logical Operators Example \n"; if (num1 <= 40 && num2 >= 40) { cout << "Num1 is less than and Num2 is greater than or equal to 40 \n"; } if (num1 >= 40 || num2 >= 40) { cout << "Num 1 or Num 2 is greater than or equal to 40 \n"; } getch(); return 0; }

    OUTPUT:

    vbnet

    Logical Operators Example Num1 is less than and Num2 is greater than or equal to 40 Num 1 or Num 2 is greater than or equal to 40

    Q.23: Write down the difference between relational and logical operators.

    Ans:
    DIFFERENCE BETWEEN RELATIONAL OPERATOR AND LOGICAL OPERATOR

    RELATIONAL OPERATOR

    • Relational operators compare any values in the form of expressions.
    • Relational operators are binary operators because they require two operands to operate.
    • Relational operators return results either as 1 (TRUE) or 0 (FALSE).

    LOGICAL OPERATOR

    • Logical operators perform logical operations on boolean values 1 (TRUE) and 0 (FALSE).
    • Logical operators are usually used to compare one or more relational expressions.
    • Logical operators also return output as 1 (TRUE) or 0 (FALSE).

    Q.24: Define assignment operator.

    Ans:
    ASSIGNMENT OPERATOR
    Assignment operators (=) are used to assign the result of an expression or a value to a variable. The associativity of assignment operators is right to left, which means the value or expression on the right is assigned to the left-side variable.

    Q.25: Define arithmetic assignment operators with examples.

    Ans:
    ARITHMETIC ASSIGNMENT OPERATOR
    Arithmetic assignment operators are a combination of arithmetic and assignment operators. This operator first performs an arithmetic operation on the current value of the variable on the left with the value on the right and then assigns the result to the variable on the left.

    OPERATORDESCRIPTION
    += (Addition-Assignment)Adds the right operand to the left and assigns the result to the left operand.
    -= (Subtraction-Assignment)Subtracts the right operand from the left and assigns the result to the left operand.
    *= (Multiplication-Assignment)Multiplies the right operand with the left and assigns the result to the left operand.
    /= (Division-Assignment)Divides the left operand by the right and assigns the result to the left operand.

    PROGRAM USING ASSIGNMENT & ARITHMETIC ASSIGNMENT OPERATORS IN C++

    cpp

    #include<iostream> #include<conio.h> using namespace std; int main() { int a = 10; cout << "Value of a using assignment operator is " << a << "\n"; a += 10; cout << "Value of a using addition assignment operator is " << a << "\n"; return 0; }
    cpp

    a -= 10; cout << "Value of a using subtraction assignment operator is " << a << "\n"; a *= 10; cout << "Value of a using multiplication assignment operator is " << a << "\n"; a /= 10; cout << "Value of a using division assignment operator is " << a << "\n"; return 0; }

    OUTPUT:

    csharp

    Value of a using assignment operator is 10 Value of a using addition assignment operator is 20 Value of a using subtraction assignment operator is 10 Value of a using multiplication assignment operator is 100 Value of a using division assignment operator is 10