CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE a) Mention any 4 characteristic of the object car. Ans name, colour, model number, engine state, power b) What does the following signify? i. cout : It stands for common output. It is used to display the constants, variables and expressions on the output screen. ii. cin : It stands for common input. It is used to input a value from the user and store in a variable iii. conio.h : It is a header file included in the program so the use of getch(), conio.h command can be recognized. c) Mention the shortcut key used in the following cases i. Open saved program : F3 ii. Save any c++ program : F2 iii. To compile the program : Alt + C iv. To execute the program : Alt + R d) Which command is used to give multiline output? Ans. endl e) What is the extension of C++ file? Ans..cpp f) Expand IDE Ans: Integrated Development Environment g) Identify the following operators and their category i. << Insertion operator ii. >= Relational operator iii. >> Extraction operator iv.!= relational operator v. && logical operator vi.! logical operator vii. % arithmetic operator h) Define the following terms i. Tokens : it is the smallest meaningful element of a C++ program. It defines the structure of the C++ language. These are the basic building blocks for making a program. eg. Keywords, constants, identifiers, operators etc ii. Keywords: These are the predefined words which have a specific meaning in any programming language. These words cannot be used as variables or constants. eg. int, char, float, void iii. Punctuators: these are the tokens that have a special meaning to the compiler. Eg brackets (), [], semicolon ; dot (.) 1
i) Mention the rules of defining a variable. The first character can be either an alphabet or an underscore (_) Except underscore, no other special characters are allowed while defining a variable. Variable name cannot be a keyword Both alphabet and digits can be used. Spaces are not allowed in variable name j) Match the following data type with their storage space Datatype char bool float int Storage space 1 byte 2 bytes 1 byte 4 bytes k) Identify the valid/invalid variables also write the reason for invalid variables. i. cout : invalid; keywords cannot be used ii. Hello: valid iii. First name: invalid; spaces are not allowed iv. 1name : invalid; must start with an alphabet l) Identify the header files used in the following cases i. clrscr() : conio.h ii. cin: iostream.h iii. getch(): conio.h iv. cout: iostream.h m) Differentiate between / & % operator. Ans / : it is an arithmetic operator that perform normal division. i.e it gives the quotient as the answer Eg. 5/2 will be 2.5 % it is an arithmetic operator that performs modulo division. i.e it gives the remainder as the answer Eg. 5%2 will be 1 n) Write C++ commands for the following i. Declare a character variable that stores a value P char x= P ; ii. Declare integer variable with value 10 int x=10; iii. Declare decimal variable which stores value 12.5 and has storage space 8 bytes. 2
float y=12.5; iv. Declare a Boolean variable. bool y; o) Debug the following #include<iostream> cout << hello how are you ; cout >> please remove errors and correct me ; Corrected program cout << hello how are you ; cout << please remove errors and correct me ; p) Write the output for the following program cout << 25 + 10 << endl; cout << 75-80 ; Output 25+10-5 q) Write a program in C++ to print the difference of any two numbers entered by the user. Solution cout << enter two numbers ; cin>>a; 3
cin>>b; cout << the difference is <<a-b; r) If the cost of 1 kg oranges is Rs 60/-, then using C++ write the program to find the cost of 50kg oranges. Solution int x=60; cout << the cost of 1 kg oranges is Rs 60/- ; cout << the cost of 50kgs oranges will be << 50*x; CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 2, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE Q1 Write a program in C++ to swap 2 numbers. int x,y,z; cin>> x; cin>>y; cout << the numbers are << x<< y; z=x; x=y; y=z; cout << the numbers after swapping are << x<< y; Q2 Complete the following program to print the sum of for i natural numbers. int i, _j, _s_; s=0; 4
cout<< enter the number <<endl; cin>> i; for (j=1; j<= i; j++) s=s+j; cout<< the sum of the first <<i<< numbers is << s; getch(); Q3 Differentiate between entry controlled and exit controlled loops. Entry controlled Exit controlled The loops in which the condition is tested in the The loops in which the condition is tested at the beginning end It doesn t work if the condition is not satisfied It works at least once even if the condition is not satisfied while loop do while loop Q4 If there is a single statement in place of a block of statements, then it is optional to use curly braces. (Y/N) - YES Q5 Complete the syntax of the if Q6 Debug the following program and rewrite after correcting the code. statement if (condition) statements int x=15; do else cout<< ** ; statements while x<=20 ; Q7 Convert the following program into for statement without changing the meaning of the program. clrscr(); int x=20; do cout<<x; x++; while (x<=30); getch(); 5
Solution clrscr(); int x; for ( x=20 ; x<=30 ; x++) cout<<x; getch(); Q7 Write a program to check if the number entered by user is even or odd using ternary operator. Display 0 for even and 1 for odd. clrscr(); int x,y; cout<< enter the number ; cin >> x; y=(x%2==0)? 0 : 1; cout <<y; Q8 Define a) loop(iteration) : A loop is a sequence of instructions that is continually repeated until the specified condition is true. b) Flow of control: Flow of control refers to the action taken based on different conditions in the program and execute the set of instructions accordingly. Q9 What are the three types of statements in C++. Ans The three types of statements in C++ are Branching Looping Jumping 6