Output: Example Input
Output: Example Input
Write and algorithm that asks the user to enter the total marks and the obtained marks of
students. The program should output the percentage and the grade obtained by the
student according to the following criteria.
Answer:
Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics
and Computer, calculate percentage and grade according to given conditions:
If percentage >= 90% : Grade A
If percentage >= 80% : Grade B
If percentage >= 70% : Grade C
If percentage >= 60% : Grade D
If percentage >= 40% : Grade E
If percentage < 40% : Grade F
Example
Input
Input marks of five subjects: 95h
95
97
98
90
Output
Percentage = 95.00
Grade A
In primary mathematics classes you have learned about percentage. Just to give a quick
recap, below is the formula to calculate percentage.
Question#2
Write an algorithm that finds the largest of the five integers entered by the user using (i) if
control structure (ii) nested if inside while loop.
Answer:
Then this program finds out the largest number among three numbers entered
by user and displays it with proper message.
#include <iostream>
using namespace std;
int main() {
float n1, n2, n3;
return 0;
}
Output
#include <iostream>
using namespace std;
int main() {
float n1, n2, n3;
return 0;
}
Output
#include <iostream>
using namespace std;
int main() {
float n1, n2, n3;
return 0;
}
Output
while (condition) {
// body of the loop
}
Here,
A while loop evaluates the condition
If the condition evaluates to true , the code inside the while loop is executed.
The condition is evaluated again.
This process continues until the condition is false .
When the condition evaluates to false , the loop terminates.
To learn more about the conditions , visit C++ Relational and Logical Operators.
#include <iostream>
int main() {
int i = 1;
return 0;
}
Output
1 2 3 4 5
Question#3
Write an algorithm that asks the user to select from the choices 1, 2, 3.
Choice 1 for displaying first 5 even numbers, generated through while loop.
Choice 2 for displaying first 5 odd numbers, generated through while loop. • Choice 3 for
displaying first 5 prime numbers.
Answer:
while loop
statement(s)
while loop checks whether the condition written in ( ) is true or not. If the condition is true, the
statements written in the body of the while loop i.e., inside the braces { } are executed. Then again the
condition is checked, and if found true, again the statements in the body of the while loop are executed.
This process continues until the condition becomes false.
#include <iostream>
int main(){
int n = 1;
n++;
return 0;
Output
while(n <= 10) - checks the condition 'n <= 10'. Since the value of n is 1 which is less than 10, the
statements within the braces { } are executed.
The value of 'n' i.e. 1 is printed and n++ increases the value of 'n' by 1. So, now the value of 'n' becomes
2.
Now, again the condition is checked. This time also 'n <= 10' is true because the value of 'n' is 2. So,
again the value of 'n' i.e., 2 gets printed and the value of 'n' will be increased to 3.
When the value of 'n' becomes 10, again the condition 'n <= 10' is true and 10 gets printed for the tenth
time. Now, n++ increases the value to 'n' to 11.
This time, the condition 'n <= 10' becomes false and the program terminates.
loops in C++
The following animation will also help you to understand the while loop.
The loop will run until the value of 'choice' becomes other than '1'. So, for the first
time, it will run since the value of 'choice' is '1'. Then it will perform the codes
inside the loop. At last, it will ask the user whether he wants to check more or not.
This can change the value of variable 'choice' and may terminate the loop.
Initially, the value of 'choice' was 1, so, the condition of while got satisfied and
codes inside it got executed. We were asked to give the value of choice and we
gave 1 again. Things repeated and after that, we gave choice a value of 0. Now, the
condition of while was not satisfied and the loop terminated.
for loop
Another type of loop is for loop.
Let's go to our first example in which we printed first 10 natural numbers using
while loop. We can also do this with for loop.
Let's look at the syntax of for loop.
for(initialization; condition; propagation)
{
statement(s)
}
#include <iostream>
int main(){
using namespace std;
int n;
for( n = 1; n <= 10; n++ ){
cout << n << endl;
}
return 0;
}
Output
#include <iostream>
int main(){
sum = sum + n;
}
cout << "Sum is " << sum << endl;
return 0;
}
Output
int n = 1;
for( ; n <= 10; n++)
{
cout << n << endl;
}
int n;
for( n = 1; n <= 10; )
{
cout << n << endl;
n++;
}
It means that we can also write the for loop by skipping one or more of its three
statements (initialization, condition, propagation) as done above.
do...while loop
This is another kind of loop. This is just like while and for loop but the only
difference is that the code in its body is executed once before checking the
conditions.
Syntax of do...while loop is:
do{
statement(s)
}
while( condition );
Consider the same example of printing the first 10 natural numbers for which we
wrote programs using while and for loop. Now, let's write its program using
do...while loop.
#include <iostream>
int main(){
Output
#include <iostream>
int main(){
using namespace std;
int i;
int j;
cout << i << "*" << j << "=" << (i*j) << endl;
}
}
return 0;
}
Output
When the first for loop is executed, the value of i is 12 and "Table of 12" gets
printed.
Now coming to the second loop, the value of j is 1 and thus 12*1 = 12 gets printed.
In the second iteration of the inner for loop, while the value of i is still 12, the value
of j becomes 2 and thus 12 * 2 = 24 gets printed.
Infinite Loop
There may exist some loops which can iterate or occur infinitely. These are called
Infinite Loop. These loops occur infinitely because their condition is always true.
We can make an infinite loop by leaving its conditional expression empty (this is
one of the many possible ways). When the conditional expression is empty, it is
assumed to be true. Let's see an example on how to make a for loop infinite.
#include <iostream>
int main(){
using namespace std;
for( ; ; ){
cout << "This loop will never end" << endl;
}
Return:0
Question#4
Write a program that asks the user to type in a number, and then calculates
the factorial of this number using while loop.
The factorial of a number is the product of all the integers from 1 to that
number.
For example, the factorial of 6 is 1*2*3*4*5*6 = 720 . Factorial is not defined for
negative numbers, and the factorial of zero is one, 0! = 1.
Factorial of a Number using Loop
factorial = 1
Output
Note: To test the program for a different number, change the value of num .
Here, the number whose factorial is to be found is stored in num , and we check
if the number is negative, zero or positive using if...elif...else statement. If
the number is positive, we use for loop and range() function to calculate the
factorial.
iteration factorial*i (returned value)
i=1 1*1=1
i=2 1*2=2
i=3 2*3=6
i=4 6 * 4 = 24
i=5 24 * 5 = 120
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
The factorial of a number is the product of all the integers from 1 to that
number.
For example, the factorial of 6 is 1*2*3*4*5*6 = 720 . Factorial is not defined for
negative numbers, and the factorial of zero is one, 0! = 1.
factorial = 1
Output
Note: To test the program for a different number, change the value of num .
Here, the number whose factorial is to be found is stored in num , and we check
if the number is negative, zero or positive using if...elif...else statement. If
the number is positive, we use for loop and range() function to calculate the
factorial.
iteration factorial*i (returned value)
i=1 1*1=1
i=2 1*2=2
i=3 2*3=6
i=4 6 * 4 = 24
i=5 24 * 5 = 120
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
1. 1:10*5
In Julia:
1. [1:10;]*5
In Python:
1. #include <iostream>
2. using namespace std;
3. int main() {
4. for (int i=1; i<=10; i++) cout << i*5 << endl;
5. return 0;
6. }
In C:
1. #include <stdio.h>
2. int main() {
3. int i;
4. for (i=1; i<=10; ++i) printf(“%d\n”, i*5);
5. return 0;
6. }
In C programming,
7. main()
8. {
9. int i;
10. for(i=1;i<=10;i++)
11. {
12. printf(“5*%d = %d\n”, i,5*i);
13. }
14. return 0;
15. }