You are on page 1of 26

PROGRAMMING FUNDAMENTALS

PROGRAMMING FUNDAMENTALS

Finally, Run the saved file


PROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS

 Learning how conditions can be executed.


 Write a complete program that will use conditional constructs.

Learning how conditions can be executed.

A computer can proceed:


 In sequence
 Selectively (branch): making a choice
 Repetitively (iteratively): looping
Some statements are executed only if certain conditions are met. A condition is
met if it evaluates to true. Following are 3 selection/conditional statements.
PROGRAMMING FUNDAMENTALS

#include <iostream>
using namespace std;

int main() {
cout << "Enter a number: ";
int number;
cin >> number;
int absoluteValue=number;
if (number < 0) {
absoluteValue = -number;
}
cout << "The absolute value of " <<
number << " is " << absoluteValue << endl;
return 0;
}
#include <iostream>
using namespace std;

int main() {
cout << "Enter a number: ";
int number;
cin >> number;

if (number % 2 == 0) {
cout << number << " is even." << endl;
} else {
cout << number << " is odd." << endl;
}

return 0;
}
#include <iostream>
using namespace std;

int main() {
int age;
cout << "Enter your age: ";
cin >> age;

if (age >= 18) {


cout << "You are eligible to vote." <<
endl;
PROGRAMMING FUNDAMENTALS

char citizenship;
cout << "Are you a citizen? (y/n): ";
cin >> citizenship;

if (citizenship == 'y' || citizenship == 'Y')


{
cout << "You can participate in the
election." << endl;
} else { cout << "You need to be a
citizen to participate in the election." << endl;
}
}
else {
cout << "You are not eligible to vote."
<< endl;
}

return 0;
}

Write a complete program that will use conditional constructs.

Task 01: Write a program in which it takes a number from keyboard as an input and if the
number is greater than 100 it prints “The number is greater than hundred”.
Task 02: Write a program in which it takes two numbers from keyboard as input and subtract
larger number from smaller.
Task 03: Write a program which take a number from keyboard and checks the number whether
that number is less than 100 or not if that number is less than 100 than check that is it less than
50 or not.
Task 04: Write a program which takes marks as input and shows the output as follows:
Greater than or equal to 75 : A
Greater than or equal to 60 : B
Greater than or equal to 45 : C
Less than 45 : Fail
PROGRAMMING FUNDAMENTALS

Task 05: Get gender, year of service and qualification of employee as input from keyboard and
determine his/her salary based on following chart.

Gender Year of service Qualification salary


Male >=10 P 15000
>=10 G 12000
<10 P 10000
<10 G 7000
Female >=10 P 12000
>=10 G 10000
<10 P 7000
<10 G 6000
PROGRAMMING FUNDAMENTALS

 Learning how to use Switch statement, Case statement, Break


statement.

Learning how to use Switch statement, Case statement, Break statement


and Continue Statement.

Switch statement is C/C++ language is used for selection control. The difference
between if/else and switch selection statements is that the second one is used for
making a selection from multiple statements.
There are times when you'll find yourself writing a huge if block that consists of
many else if statements. The switch statement can help simplify things a little.
It allows you to test the value returned by a single expression and then execute
the relevant bit of code.
You can have as many cases as you want, including a default case which is
evaluated if all the cases fail. Let's look at the general form.
switch (expression)
{
case expression1:
/* one or more statements */
case expression2:
/* one or more statements */
/* ...more cases if necessary */
default:
/* do this if all other cases fail */
}
PROGRAMMING FUNDAMENTALS

#include <iostream>
using namespace std;
int main()
{
int a;
cin>>a;
switch (a) { case 1:
cout<<"You chose number 1\n";
case 2:
cout<<"You chose number 2\n"; case 3:
cout<<"You chose number 3\n"; case 4:
cout<<"You chose number 4\n"; default:
cout<<"That's not 1,2,3 or 4!\n"; You'll notice that the program will select the
} correct case but will also run through all the
return 0; cases below it (including the default) until the
} switch block's closing bracket is reached.
To prevent this from happening, we'll need to
insert another statement into our cases...

Break:
The break statement terminates the execution of the nearest enclosing switch statement in which
it appears. Control passes to the statement that follows the terminated statement
#include <iostream>
using namespace std;
int main()
{
int a;
cin>>a;
switch (a) { case 1:
cout<<"You chose number 1\n"; break;
case 2:
cout<<"You chose number 2\n"; break;
case 3:
cout<<"You chose number 3\n"; break;
case 4:
cout<<"You chose number 4\n"; break;
default:
cout<<"That's not 1,2,3 or 4!\n";}
return 0;
}
PROGRAMMING FUNDAMENTALS

Task 01: Calculator Program using Switch Statement


You are required to create a simple calculator program that takes an operator (+, -, *, /) and two
numbers as input from the user and performs the corresponding arithmetic operation using a
switch statement.
Your program should:
1. Display a prompt for the user to enter an operator (+, -, *, /).
2. Allow the user to enter two numbers.
3. Use a switch statement to determine the operator and perform the appropriate calculation.
4. Display the result of the operation.
Ensure that your program handles the following cases:
 If the user enters an invalid operator, the program should display an error message.
 If the user attempts to divide by zero, the program should display an appropriate error
message.
Task 02: Grade Converter
You are tasked with creating a program that takes a numerical grade as input and converts it into
a letter grade using the following scale:
 A: 90-100
 B: 80-89
 C: 70-79
 D: 60-69
 F: 0-59
Your program should:
1. Display a prompt for the user to enter a numerical grade.
2. Use a switch statement to determine the corresponding letter grade based on the input.
3. Display the calculated letter grade.
PROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS

 Learning how to use while, nested while and do while loop


loops

Learning how to use while, nested while and do while loop loops

The most basic loop in C is the while loop. A while statement is like a repeating
if statement. Like an If statement, if the test condition is true: the statements get
executed. The difference is that after the statements have been executed, the test
condition is checked again. If it is still true the statements get executed again.
This cycle repeats until the test condition evaluates to false.
A while loop is a control flow statement that allows code to be executed
repeatedly based on a given condition. The while consists of a block of code and
a condition. The condition is first evaluated and if the condition is true the code
within the block is then executed. This repeats until the condition becomes false.

while ( expression )
{
Single statement or Block of statements;
}
PROGRAMMING FUNDAMENTALS

#include <iostream>
using namespace std;
int main()
{
int i = 5;

while ( i > 0 )
{
cout<<i;
cout<<"\n";
i = i -1;
}
return 0;
}

#include <iostream>
using namespace std;

int main() {
int n, sum = 0;

cout << "Enter a positive integer: ";


cin >> n;
int num=n;
while (n > 0) {
sum =sum+n; // Add n to the running
sum
n--; // Decrement n
}
cout << "The sum of numbers from 1 to "
<< num << " is: " << sum << endl;

return 0;
}
Nested While Loop

#include <iostream>
using namespace std;

int main() {
int rows;
cout << "Enter the number of rows for the
triangle: ";
cin >> rows;
PROGRAMMING FUNDAMENTALS

int i = 1;
while (i <= rows) {
int j = 1;
while (j <= i) {
cout << "* ";
j++;
}
cout << endl;
i++;
}

return 0;
}
DO While Loop
Its functionality is exactly the same as the while loop, except that condition in the do-while
loop is evaluated after the execution of statement instead of before, granting at least one
execution of statement even if condition is never fulfilled. For example, the following
example program echoes any number you enter until you enter 0.

#include <iostream>
using namespace std;
int main ()
{
int n;
do{

cout << "Enter number (0 to end): ";


cin >> n;
cout << "You entered: " << n << "\n"; }
while (n != 0);
return 0;
}

Task 01: In a tabular format , print the integers from 0 to 9 , their squares and their cubes.
Task 02: Write a program that inputs temperatures of seven days of a week and calculates its
average.
PROGRAMMING FUNDAMENTALS

Task 03: Number Reversal


You are given a positive integer. Your task is to create a program that takes this integer as input
and prints its digits in reverse order.
Your program should: Display a prompt for the user to enter a positive integer.
Use a while loop to extract and print each digit of the number in reverse order.
Terminate the loop when all digits have been printed.
For example, if the user enters the number 12345, your program should print 54321
Task 04: Write a program who prints asterick sign (*) in such a way using nested while loops
****
***
**
*
Task 05: Number Guessing Game
You are tasked with creating a simple number guessing game. The program will generate a
random number between 1 and 100 (inclusive) and allow the user to guess the number. The user
will keep guessing until they correctly guess the number.
Your program should:
1. Generate a random number between 1 and 100.
2. Display a prompt for the user to enter their guess.
3. Use a do-while loop to repeatedly ask the user for their guess and provide feedback.
4. If the user's guess is incorrect, provide hints such as "Too high" or "Too low".
5. Once the user guesses the correct number, display a congratulations message along with
the number of attempts made.
For example, if the correct number is 42, the program could display:
PROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS

 loop
A for is a repetition
Learning how to usecontrol structurestructure
for repetition that allows you to efficiently write a
loop that needs
Write to execute
complete a specific
program usingnumber
for. of times.
Here is the flow of control in a for loop:
Learning how to use for repetition structure
 The init step is executed first, and only once. This step allows you to
declare and initialize any loop control variables. You are not required to
put a statement here, as long as a semicolon appears.
 Next, the condition is evaluated. If it is true, the body of the loop is
executed. If it is false, the body of the loop does not execute and flow of
control jumps to the next statement just after the for loop.
 After the body of the for loop executes, the flow of control jumps back up
to the increment statement. This statement allows you to update any loop
control variables. This statement can be left blank, as long as a semicolon
appears after the condition.
 The condition is now evaluated again. If it is true, the loop executes and
the process repeats itself (body of loop, then increment step, and then
again condition). After the condition becomes false, the for loop
terminates.
for ( init; condition; update statement )
{
statement(s);
}
PROGRAMMING FUNDAMENTALS

#include<iostream>
using namespace std;
int main()
{
int x;
for ( x=5; x <= 50; x = x+5 )
{
cout<< "Loop counter value is " << x <<
".\n";
}
system ("pause"); return 0;}

#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a positive integer: ";
cin >> number;

if (number < 0) {
cout << "Factorial is not defined for
negative numbers." << endl;
} else {
int factorial = 1;

for (int i = 1; i <= number; i++) {


factorial *= i;
}

cout << "Factorial of " << number << "


is: " << factorial << endl;
}

return 0;
}
Note: for (;;) works as an infinite loop.
PROGRAMMING FUNDAMENTALS

Task 01: Write a program to get an integer from user as input and print its multiplication table.

Task 02: Get two numbers from user. Write a program to find the value of first number raised to
the power of the second.

Task 03: Write a program that reads in the size of the side of a square then prints a hollow
square of that size out of asterisks and blanks. Your program should work for squares of all side
sizes between 1 and 20. For example, if your program reads a size of 5, it should print

*****
* *
* *
* *
*****

Task 04: Number Guessing Game with a for loop


You are required to create a number guessing game program. The program will generate a
random number between 1 and 100 (inclusive) and allow the user to guess the number. The user
will keep guessing until they correctly guess the number.
Your program should:
1. Generate a random number between 1 and 100.
2. Display a welcome message and a prompt for the user to enter their guess.
3. Use a for loop to repeatedly ask the user for their guess and provide feedback.
4. If the user's guess is incorrect, provide hints such as "Too high" or "Too low".
5. Once the user guesses the correct number, display a congratulations message along with
the number of attempts made.

You might also like