assignment c++ programming

6
ASSIGNMENT : C++ PROGRAMMING QUESTIONS 1. Who is written C++ ? Answers : Bjarne Stroustrup 2. State statements below and give an example application in C++ Program. a. Go to Answers : Example 1: goto Statement /* C++ program to demonstrate the working of goto statement. */ /* This program calculates the average of numbers entered by user. */ /* If user enters negative number, it ignores that number and calculates the average of number entered before it.*/ # 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 n"<<i<<": "; cin>>num; if(num < 0.0) { goto jump; /* Control of the program moves to jump; */ } sum += num; } jump: average=sum/(i-1); cout<<"\nAverage = " <<average; return 0;

Upload: arifudin

Post on 07-Apr-2016

14 views

Category:

Documents


3 download

DESCRIPTION

C++

TRANSCRIPT

Page 1: assignment C++ programming

ASSIGNMENT : C++ PROGRAMMING

QUESTIONS

1. Who is written C++ ?

Answers : Bjarne Stroustrup

2. State statements below and give an example application in C++ Program.

a. Go to

Answers :

Example 1: goto Statement/* C++ program to demonstrate the working of goto statement. *//* This program calculates the average of numbers entered by user. *//* If user enters negative number, it ignores that number and calculates the average of number entered before it.*/

# 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 n"<<i<<": "; cin>>num; if(num < 0.0) { goto jump; /* Control of the program moves to jump; */ } sum += num; } jump: average=sum/(i-1); cout<<"\nAverage = "<<average; return 0;}

Page 2: assignment C++ programming

b. While

Answers :

Example:#include <iostream>

using namespace std;

int main ()

{

// Local variable declaration:

int a = 10;

// while loop execution

while( a < 20 )

{

cout << "value of a: " << a << endl;

a++;

}

return 0;

}

Page 3: assignment C++ programming

c. Break and Continue

Answers :

(BREAK):

Example 1: C++ breakC++ program to add all number entered by user until user enters 0.

// C++ Program to demonstrate working of break statement

#include <iostream>using namespace std;int main() { float number, sum = 0.0;

while (true) { // test expression is always true cout<<"Enter a number: "; cin>>number; if (number != 0.0) { sum += number; } else { break; // terminating the loop if number equals to 0.0 }

} cout<<"Sum = "<<sum; return 0;}

(CONTINUE)

Example 2: C++ continueC++ program to display integer from 1 to 10 except 6 and 9.

// C++ Program to demonstrate working of continue statement

#include <iostream>using namespace std;int main() { for (int i = 1; i <= 10; ++i) { if ( i == 6 || i == 9) { continue; } cout<<i<<"\t"; } return 0;}

Page 4: assignment C++ programming

d. While True

Example :

123456789101112131415161718192021

#include <iostream>using namespace std;

int main (int argc, char * const argv[]) {

int counter = 9;

while (true) {

cout << "Counter: " << counter << endl;;counter--;

if (counter == 0) {break;

}

}

return 0;}

Edit & Run

e. Do/While

Anwers :

Example :

// do_while_statement.cpp#include <stdio.h>int main(){ int i = 0; do { printf_s("\n%d",i++); } while (i < 3);}

f. Jump/Loop

example :

Page 5: assignment C++ programming

break;continue;return [expression];goto identifier;

g. If/else

EXAMPLE :

#include <iostream>

using namespace std;

int main(){ int mark; cout << "What mark did you get in the test?" << endl; cin >> mark;

if(mark >= 90) { cout << "You got an A*" << endl; cout << "You Passed!" << endl; } else if(mark >= 80) { cout << "You got an A" << endl; cout << "You Passed!" << endl; } else if(mark >= 70) { cout << "You got an B" << endl; cout << "You Passed!" << endl; } else if(mark >= 60) { cout << "You got an C" << endl; cout << "You Passed!" << endl; } else if(mark >= 50) { cout << "You got an D" << endl; cout << "You Failed!" << endl; } else if(mark >= 40)

Page 6: assignment C++ programming

{ cout << "You got an E" << endl; cout << "You Failed!" << endl; } else { cout << "You got an U" << endl; cout << "You Failed!" << endl; }

return 0;}