chap 2 Flashcards

1
Q

What are pre processor directives?
what are eg of directives ?
what is a header file?

A

define – Define Macros

Preprocessor Directives are instructions that are processed before compilation. it prepares/ready the code for compilation. for eg. #define PI 3.1415 here here at every occurrence of Pi will be replaced by 3.14

After this directive is processed by the preprocessor, every occurrence of PI in the code will be replaced by 3.1415.
The replacement happens before the compiler even looks at the code.

#include – File Inclusion
#ifdef / #ifndef – Check if a Macro is Defined

A header file is a file with a .h extension that contains declarations of functions, classes, and macros. The actual implementations (definitions) of these functions and classes are typically found in a corresponding .cpp file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

use ternary operator to find if the value is even or odd

A

// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;</iostream>

int main() {
bool flag = true;
(flag == true )? cout&laquo_space;“yes” : cout&laquo_space;“no!”;
int max ;
int a = 2 ;
int b = 3;
(a>b) ? max = a : max =b;
cout &laquo_space;“max = “ &laquo_space;max;

return 0; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a program to print pyramid of numbers using nested for loop

A

include <iostream></iostream>

using namespace std;

int main() {
int i, j, k;
int num = 5;

for (i = 1; i <= num; i++) {
    // Print spaces
    for (k = 1; k <= num - i; k++) {
        cout << " ";
    }

    // Print numbers
    for (j = 1; j <= i; j++) {
        cout << j;
    }

    cout << "\n";
}

return 0; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

datatypes in cpp

A

3 types built in , derived and user defined
built in : char, int, float, void
derived datatypes:
array, functions, pointers, references
user defended datatype:
structure, union, class, enum

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

size of char int float double long double

A

1,2,4,8,10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how will you use enum

A

enum weekdays{SUN, MON, TUE, WED, THUR, FRI, SAT}
Enumerators are use to attach numbers to names,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is typedef?

A

typedef is used to change the name of datatype eg:
typedef int whole;
now insted of int we can write whole
whole sum = 2 ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

type conversion

A

automatic :compiler converts data type to larger datatype

typecasting: programmer convert one data type to another datatype
by mentioning the type explicitly
int sum;
average = float(sum) /5;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

variable

A

is name given to some part of memory location

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

rvalue , lvalue

A

the value which is assigned
the var that is to be assigned

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

const (literals)

A

values cannot be changed onced assigned

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

operators in cpp

A

arithmatic
relational
logical operators
bitwize operators
special operators
escape sequence

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

difference between preincrement and post increment

A

i++ and ++i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Control statement

A

selection : if , if-else, switch, nested if

iterative looping : for, while, do-while, nested for

breaking : break, continue, goto exit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

check if the no. is odd or even

A

include<iostream></iostream>

using namespace std;

int main(){
int value = 3;
if (value%2==0){
cout«“odd”;}
else{
cout«“even”;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

menu driven program to cal the are of circle rectangle and triangle

A

include <iostream></iostream>

using namespace std;
#define PI 3.1415

int main() {
int option;
int base, height, r, length;

cout << "Select proper option from the following:\n";
cout << "Press 1 to find area of circle\n";
cout << "Press 2 to find area of square\n";
cout << "Press 3 to find area of triangle\n";
cin >> option;

switch (option) {
    case 1:
        cout << "Enter radius: ";
        cin >> r;
        float areaOfCircle = PI * r * r;
        cout << "The area of Circle is: " << areaOfCircle << endl;
        break;

    case 2:
        cout << "Enter length: ";
        cin >> length;
        float areaOfSquare = length * length;
        cout << "The area of Square is: " << areaOfSquare << endl;
        break;

    case 3:
        cout << "Enter base: ";
        cin >> base;
        cout << "Enter height: ";
        cin >> height;
        float areaOfTriangle = 0.5 * base * height;
        cout << "The area of Triangle is: " << areaOfTriangle << endl;
        break;

    default:
        cout << "Enter a proper value." << endl;
        break;
}

return 0; }
17
Q

program to find factorial

A

//Find factorial of a given number
#include <iostream>
using namespace std;
int main(){
int factorial = 1;
int number;
int i = 0;
cout<<"Enter a no.";
cin >> number;</iostream>

for(i=1; i<=number; i++){
    factorial = factorial * i;
}
cout << factorial << " is the factorial of "<<number;
return 0;

}

18
Q

program to print pyramid
1
12
123
1234
12345

A

include <iostream></iostream>

0
01
012
0123
01234
012345

using namespace std;
int main(){
int i = 0;
int n = 5;
int j = 0;
for(i=0; i<=n ; i++){
for(j=0; j<=i;j++){
cout«j;
}
cout«“\n”;
}
}

========
code: #include <iostream>
using namespace std;
int main(){
int i = 0;
int n = 5;
int j = 0;
int k = 0;
for(i=1; i<=n ; i++){
for(k=1;k<=n-i;k++){
cout<<" ";
}
for(j=1; j<=i;j++){
cout<<j;
}
cout<<"\n";
}
}
=======
1234
123
12
1
#include <iostream>
using namespace std;
int main(){
int i = 0;
int n = 5;
int j = 0;
int k = 0;
for(i=1; i<=n ; i++){
for(k=1;k<=n-i;k++){
cout<<k;
}
cout<<"\n";
}
}</iostream></iostream>

19
Q

to check if the number is palindrome

A

include<iostream></iostream>

//to check if the number is palindrome or not?

using namespace std;
int main(){
int originalNo = 0;
int changedNo = 0;
int revNo = 0;
int unitDigit = 1;
cout«“Enter Original Number”;
cin»originalNo;
changedNo = originalNo;
do{
unitDigit = changedNo % 10;
revNo = revNo * 10 + unitDigit;
changedNo = changedNo/10;
}
while(changedNo>0);
if(revNo == originalNo){
cout&laquo_space;originalNo «” is palindrome no.”;
}
else{
cout«originalNo«” is Not a palindrome”;
}
return 0;
}

20
Q

what is the difference between while loop and do while loop?

A

While loop is used when we do not know the fixed no. of iteration. here first initial condition is checked if is true then enters the while loop while is entry controlled loop.

in do while loop the body is executed at least once and then the condition is checked it is also called as exit controlled loop.

21
Q

program to demonstrate
1.break
2.goto
3.continue
4.exit

A

break is used to break the iteration
goto is used to goto that label
continue is opposite of break , it continues the iteration
exit: exit will exit() the program (we need to include process or stdlib)

22
Q

Write a program to print pyramid of numbers using nested for loop

A

include <iostream></iostream>

using namespace std;

int main() {
int i, j, k;
int num = 5;

for (i = 1; i <= num; i++) {
    // Print spaces
    for (k = 1; k <= num - i; k++) {
        cout << " ";
    }

    // Print numbers
    for (j = 1; j <= i; j++) {
        cout << j;
    }

    cout << "\n";
}

return 0; }
23
Q

program to find the simple interest

A

include<iostream></iostream>

using namespace std;
int main(){
float principal, rate, time, si;
cout«“Enter Principal, rate, time”;
cin»principal»rate»time;

si = (principal*rate*time)/100;
cout<<si<<" is the simple intrest.";

}

24
Q

from Q3 onwards

A