You are on page 1of 8

A.

CBSE - Class XI - Computer Science - Simple C++ Snippets (Part-1)


Q1: Write a simple program to print your name, class and school.
Answer:
#include <iostream.h>
int main()
{
cout << "My name is Akshay Kumar" << endl;
cout << "Class: XI-A (XYZ School)." << endl;
return 0;
}

Q2: Write a program to input a number (say 31) and display it in hexadecimal form (e.g.0f).
Answer:
#include <iostream.h>int main(){int x;cout << Enter an integer:" <<
endl;cin >> x;// hex keyword displays a number in hexadecimal form.cout <<
"x=" << hex << x << endl;cin.get();return 0;}
Q3: Write a C++ program two swap to integers without taking a temporary variable.
Answer:
#include <iostream.h>int main(){int a = 10;int b = 20;cout << "Value of a
(before swap): " << a << endl;cout << "Value of b (before swap): " << b <<
endl;a = a + b;b = a - b;a = a - b;cout << "Value of a (after swap): " << a
<< endl;cout << "Value of b (after swap): " << b << endl;cin.get();return
0;}

Q4:Write a program to check is a number is multiple of three or not.


Answer:
#include <iostream.h>int main(){int x;cout << Enter an integer:";cin >>
x;// use % operator to check remainderif (x % 3)cout << x << " is NOT a
multiple of three." << endl;elsecout << x << " is multiple of three." <<
endl;cin.get();return 0;}
Q5: Write a program to check if a year is a leap year or not.
Answer:
// program to check if it is a leap year or not// Note: leap years occur in
years exactly divisible by four,// except that years ending in 00 are leap
years// only if they are divisible by 400.#include <iostream.h>#include
<conio.h>int main(){int year;cout << "Enter a year (e.g. 2004): ";cin >>
year;if ((year % 400 == 0) || ((year %100 != 0) && (year % 4 == 0)))cout <<
"The year " << year << " is a leap year" << endl;elsecout << "The year " <<
year << " is NOT a leap year" << endl;getch();return 0;}
B.

CBSE - Class XI - Computer Science - Simple C++ Snippets (Part-2)


Q1. What is the size of a variable?
Answer : When you declare a variable, memory is allocated for that variable. For example
char
variabletakes 1 byte of memory while an
int
variable consumes two bytes.Note how much
int
will occupy memory space is platform dependent. For example, Turbo C++ is DOSbased program
allocates 2 bytes to an
int
variable. While visual c++ 6.0 compiler meant for 32-bitWindows applications, allocates 4 bytes to an
int
variable.
You can check the size of a variable allocated by a compiler by using
sizeof
operator.
#include <iostream.h>

#include <conio.h>

int

main()

clrscr(); // clear the screen

// using sizeof opeator to find the size of variable

cout << "size of char: "

<< sizeof(char) << " bytes."

<< endl;

cout << "size of int: "

<< sizeof(int) << " bytes."

<< endl;

cout << "size of long: "

<< sizeof(long) << " bytes."

<< endl;

cout << "size of float: "

<< sizeof(float) << " bytes."

<< endl;

cout << "size of short: "

<< sizeof(short) << " bytes."

<< endl;

cout << "size of double: "


<< sizeof(double) << " bytes."

<< endl;

int

x =10;

cout << "size of variable x: "

<< sizeof(x) << " bytes."

<< endl;

float

b = -200.10;

cout << "size of variable b: "

<< sizeof(b) << " bytes."

<< endl;

getch();

return

0;

}
When run (compiled with Turbo C++ 3.0), the program will give the following output:size of char: 1
bytes.size of int: 2 bytes.size of long: 4 bytes.size of float: 4 bytes.size of short: 2 bytes.size of double:
8 bytes.size of variable x: 2 bytes.size of variable b: 4 bytes.
Q2: In Turbo C++ (TC) An unsigned integer can have maximum value of 65535. What will
happen ifyou add another number to it?
Answer : In general, we say data overflow happens here. But the behaviour is same as seen in
car odometer. When the unsigned int crosses its maximum value, it wraps around and reset to
zero.e.g. 65535 + 1 will become 0, 65535 + 2 will become 1 and so on...You may try the following
program:
#include <iostream.h>

#include <conio.h>

int

main()

clrscr(); // clear the screen

int

val = 65535;

cout << "val = "

<< val << endl;

val = val + 1;

cout << "val = "

<< val << endl;


You're reading a preview. Unlock full access with a free trial.
Pages 4 to 31 are not shown in this preview.

Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

You're Reading a Preview


Unlock full access with a free trial.
Download With Free Trial

Recommended

You might also like