You are on page 1of 13

C++ PROGRAMMING

QUESTION
1. Who is Written C++.

Bjarne Stroustrup born 30 December 1950) is a Danish computer scientist, most


notable for the creation and development of the widely used C++ programming
language. He is a Distinguished Research Professor and holds the College of
Engineering Chair in Computer Science at Texas A&M University, is a visiting professor
at Columbia University, and works at Morgan Stanley as a Managing Director in New
York.
EDUCATION Stroustrup has a master's degree in mathematics and computer
science (1975) from Aarhus University, Denmark, and a Ph.D. in computer science
(1979) from the University of Cambridge, England. His thesis advisor in Cambridge
was David Wheeler.

CAREER Stroustrup began developing C++ in 1978 (then called "C with Classes"), and,
in his own words, "invented C++, wrote its early definitions, and produced its first
implementation chose and formulated the design criteria for C++, designed all its major
facilities, and was responsible for the processing of extension proposals in the C++
standards committee.Stroustrup also wrote a textbook for the language, The C++
Programming Language.
Stroustrup was the head of AT&T Bell Labs' Large-scale Programming Research
department, from its creation until late 2002. Stroustrup was elected member of
the National Academy of Engineering in 2004. He is a Fellow of the ACM (1994) and
an IEEE Fellow. He works at Texas A&M University as a Distinguished Professor where
he holds the College of Engineering Endowed Chair in Computer Science. He is also a
visiting faculty in Computer Science Department at Columbia University. ITMO
University noble doctor since 2013 In 2015, he was made a Fellow of the Computer
History Museum for his invention of the C++ programming language.

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


program.
A. Go to

In C programming, goto statement is used for altering the normal

sequence of program execution by transferring control to some other


part of the program.

Syntax of goto statement


goto label;
.............
.............
.............
label:
statement;

In this syntax, label is an identifier. When, the control of


program reaches to goto statement, the control of the program
will jump to the label: and executes the code below it.

Example of 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 <stdio.h>

int main(){

float num,average,sum;

int i,n;

printf("Maximum no. of inputs: ");

scanf("%d",&n);

for(i=1;i<=n;++i){

printf("Enter n%d: ",i);

scanf("%f",&num);

if(num<0.0)

goto jump;

/* control of the program moves to label

jump */

sum=sum+num;

jump:

average=sum/(i-1);

printf("Average: %.2f",average);

B. While
Loops causes program to execute the certain block of code repeatedly until
some conditions are satisfied, i.e., loops are used in performing repetitive
work in programming.
Suppose you want to execute some code/s 10 times. You can perform it by
writing that code/s only one time and repeat the execution 10 times using
loop.
There are 3 types of loops in C programming:

Syntax of while loop

while (test expression) {

statement/s to be executed.

The while loop checks whether the test expression is true or not. If it
is true, code/s inside the body of while loop is executed,that is,
code/s inside the braces { } are executed. Then again the test
expression is checked whether test expression is true or not. This
process continues until the test expression becomes false.

Example of while loop

Write a C program to find the factorial of a number, where the number is


entered by user. (Hints: factorial of n = 1*2*3*...*n

/*C program to demonstrate the working of while loop*/


#include <stdio.h>
int main(){
int number,factorial;
printf("Enter a number.\n");
scanf("%d",&number);
factorial=1;
while (number>0){

/* while loop continues util test

condition number>0 is true */


factorial=factorial*number;
--number;
}
printf("Factorial=%d",factorial);
return 0;
}

C. Break and Continue


There are two statements built in C
programming, break; and continue; to alter the normal flow of a
program. Loops perform a set of repetitive task until text
expression becomes false but it is sometimes desirable to skip
some statement/s inside loop or terminate the loop immediately
without checking the test expression. In such cases, break and
continue statements are used. The break;statement is also used
in switch statement to exit switch statement.

break Statement

In C programming, break is used in terminating the loop immediately after it is


encountered. The break statement is used with conditional if statement.

Syntax of break statement


break;

The break statement can be used in terminating all three loops for, while and
do...while loops.

The figure below explains the working of break statement in all three
type of loops .

Example of break statement

Write a C program to find average of maximum of n positive numbers entered


by user. But, if the input is negative, display the average(excluding the
average of negative input) and end the program.

/* C program to demonstrate the working of break statement by


terminating a loop, if user inputs negative number*/

# include <stdio.h>

int main(){

float num,average,sum;

int i,n;

printf("Maximum no. of inputs\n");

scanf("%d",&n);

for(i=1;i<=n;++i){

printf("Enter n%d: ",i);

scanf("%f",&num);

if(num<0.0)

break;

sum=sum+num;

average=sum/(i-1);

printf("Average=%.2f",average);

return 0;

//for loop breaks if num<0.0

D. WHILE TRUE
The do...while Loop is similar to while loop with one very important difference.
Inwhile loop, check expression is checked at first before body of loop but in case
ofdo...while loop, body of loop is executed first then only test expression
ischecked. That's why the body of do...while loop is executed at least once.

E. Do/ While
One interesting thing about the while loop is that if the loop condition is
initially false, thewhile loop will not execute at all. It is sometimes the
case that we know we want a loop toexecute at least once, such as when
displaying a menu. To help facilitate this, C++ offersthe do-while loop:

The statement in a do-while loop always executes at least once. After the
statement has been executed, the do-while loop checks the condition. If the
condition is true, the CPU jumps back to the top of the do-while loop and
executes it again.Here is an example of using a do-while loop to display a menu
to the user and wait for theuser to make a valid choice:

F. Jump / Loop

Cause a certain piece of program to be executed a certain number of times. Consider


these scenarios: You want to execute some code/s certain number of time.
You want to execute some code/s certain number of times depending upon input from
user.
Example :

Output :

G. If / Else

An if statement can be followed by an optional else statement, which executes when


the boolean expression is false.
Syntax:

The syntax of an if...else statement in C++ is:

If the boolean expression evaluates true, then the if block of code will be
executed,otherwise else block of code will be executed.

When the above code is compiled and executed, it produces the following result:

You might also like