# 117639449 CBSE Class XI Computer Science Notes

Description
computer science class xi
Categories
Published

View again

All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Transcript
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 remainder if (x % 3) cout << x << is NOT a multiple of three. << endl; else cout << 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; else cout << 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   variable takes 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 DOS based program allocates 2 bytes to an int  variable. While visual c++ 6.0 compiler meant for 32-bit Windows 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 if you 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;     getch();   return   0;   }   Q3: Consider the following C++ snippet:   int   x = 25000;   int   y = 2*x;   cout<< y << \t   << x;   Why does the output does not come as 50000? What could be the reason?   Answer : The range of int  (compiler takes int  as signed int ) is from -32768 to +32767. After multiplying the value exceeds the upper bound (+32767) and result is not the desired output. We should consider long  instead of int . Q4:Write a program to input a digit and display its ASCII code.   Answer : C++ gives char variable special treatment. It displays the ASCII character. E.g. if char variable stores 65, it will be displayed as 'A'. You may assign a character to int  (e.g. int x = 'P'). When you cout << x; it will print 80 not 'P'. ?  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // program to input a alphabet or digit   // and display its ASCII code   #include <iostream.h>   #include <conio.h>   void   main()   {   char   ch;   clrscr(); // clear screen   cout << Enter a character or digit: ;   cin >> ch;   int   x = ch; // stores 1 byte of character as integer   cout << ASCII code of character( << ch << ) is:   << x << endl;   x = 'P';   cout << x =   << x << endl;   getch();   }

Jul 23, 2017

#### 6-4 Absolute Value Equations

Jul 23, 2017
Search
Similar documents

View more...
Tags

Related Search