0% found this document useful (0 votes)
51 views23 pages

Output: Example Input

The document provides an algorithm to input marks of five subjects from a user, calculate the percentage, and output the grade based on percentage ranges. It begins by prompting the user to enter marks for five subjects. It then calculates the percentage by dividing the sum of all marks by 500 (since there are 5 subjects each out of 100). Finally, it uses if-else statements to check the percentage and output the correct grade according to the criteria: - Grade A if percentage is >= 90% - Grade B if percentage is >= 80% - Grade C if percentage is >= 70% - Grade D if percentage is >= 60% - Grade E if percentage is >= 40% - Grade F if

Uploaded by

Aftab Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views23 pages

Output: Example Input

The document provides an algorithm to input marks of five subjects from a user, calculate the percentage, and output the grade based on percentage ranges. It begins by prompting the user to enter marks for five subjects. It then calculates the percentage by dividing the sum of all marks by 500 (since there are 5 subjects each out of 100). Finally, it uses if-else statements to check the percentage and output the correct grade according to the criteria: - Grade A if percentage is >= 90% - Grade B if percentage is >= 80% - Grade C if percentage is >= 70% - Grade D if percentage is >= 60% - Grade E if percentage is >= 40% - Grade F if

Uploaded by

Aftab Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Question#1

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.

Percentage (Marks obtained/Total Marks)*100

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.

Step by step descriptive logic to find percentage and grade.


1. Input marks of five subjects in some variable say phy, chem, bio, math and comp.
2. Calculate percentage using formula per = (phy + chem + bio + math + comp) /
5.0;.
Carefully notice I have divided sum with 5.0, instead of 5 to avoid integer division.
3. On the basis of per find grade of the student.
4. Check if(per >= 90) then, print "Grade A".
5. If per is not more than 90, then check remaining conditions mentioned and print grade.
6. /**
7. * C program to enter marks of five subjects and find percentage and grade
8. */
9.
10.#include <stdio.h>
11.
12.int main()
13.{
14. int phy, chem, bio, math, comp;
15. float per;
16.
17. /* Input marks of five subjects from user */
18. printf("Enter five subjects marks: ");
19. scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &comp);
20.
21.
22. /* Calculate percentage */
23. per = (phy + chem + bio + math + comp) / 5.0;
24.
25. printf("Percentage = %.2f\n", per);
26.
27.
28. /* Find grade according to the percentage */
29. if(per >= 90)
30. {
31. printf("Grade A");
32. }
33. else if(per >= 80)
34. {
35. printf("Grade B");
36. }
37. else if(per >= 70)
38. {
39. printf("Grade C");
40. }
41. else if(per >= 60)
42. {
43. printf("Grade D");
44. }
45. else if(per >= 40)
46. {
47. printf("Grade E");
48. }
49. else
50. {
51. printf("Grade F");
52. }
53.
54. return 0;
55. }

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:

(i) if control structure

C++ Program to Find Largest Number


Among Three Numbers
In this example, you'll learn to find the largest number among three numbers
using if, if else and nested if else statements.

To understand this example, you should have the knowledge of the


following C++ programming topics:
 C++ if, if...else and Nested if...else

In this program, user is asked to enter three numbers.

Then this program finds out the largest number among three numbers entered
by user and displays it with proper message.

This program can be used in more than one way.


Example 1: Find Largest Number Using if Statement

#include <iostream>
using namespace std;

int main() {
float n1, n2, n3;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;

if(n1 >= n2 && n1 >= n3)


cout << "Largest number: " << n1;

if(n2 >= n1 && n2 >= n3)


cout << "Largest number: " << n2;

if(n3 >= n1 && n3 >= n2)


cout << "Largest number: " << n3;

return 0;
}

Output

Enter three numbers: 2.3


8.3
-4.2
Largest number: 8.3
Example 2: Find Largest Number Using if...else Statement

#include <iostream>
using namespace std;

int main() {
float n1, n2, n3;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;

if((n1 >= n2) && (n1 >= n3))


cout << "Largest number: " << n1;
else if ((n2 >= n1) && (n2 >= n3))
cout << "Largest number: " << n2;
else
cout << "Largest number: " << n3;

return 0;
}

Output

Enter three numbers: 2.3


8.3
-4.2
Largest number: 8.3

Example 3: Find Largest Number Using Nested if...else statement

#include <iostream>
using namespace std;

int main() {
float n1, n2, n3;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;

if (n1 >= n2) {


if (n1 >= n3)
cout << "Largest number: " << n1;
else
cout << "Largest number: " << n3;
}
else {
if (n2 >= n3)
cout << "Largest number: " << n2;
else
cout << "Largest number: " << n3;
}

return 0;
}

Output

Enter three numbers: 2.3


8.3
-4.2
Largest number: 8.3

(ii) nested if inside while loop.

C++ while Loop

The syntax of the  while  loop is:

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.

Flowchart of while Loop

Flowchart of C++ while loop


Example 1: Display Numbers from 1 to 5

// C++ Program to print numbers from 1 to 5

#include <iostream>

using namespace std;

int main() {
int i = 1;

// while loop from 1 to 5


while (i <= 5) {
cout << i << " ";
++i;
}

return 0;
}

Output

1 2 3 4 5

Here is how the program works.

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

Let's first look at the syntax of while loop.


while(condition)

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.

An example will make this clear.

#include <iostream>

int main(){

using namespace std;

int n = 1;

while( n <= 10){

cout << n << endl;

n++;

return 0;

Output

In our example, firstly, we assigned a value 1 to a variable 'n'.

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.

Quite interesting. Isn't it !

loops in C++

The following animation will also help you to understand the while loop.

gif of while loop in C++

Let's see one more example of 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

Now let's see how for loop works.


for(n=1; n<=10; n++)
n=1 - This step is used to initialize a variable and is executed first and only once.
Here, 'n' is assigned a value 1.
n<=10 - This is a condition which is evaluated. If the condition is true, the
statements written in the body of the loop are executed. If it is false, the statement
just after the for loop is executed. This is similar to the condition we used in 'while'
loop which was being checked again and again.
n++ - This is executed after the code in the body of the for loop has been executed.
In this example, the value of 'n' increases by 1 every time the code in the body of
for loop executes. There can be any expression here which you want to run after
every loop.
In the above example, firstly, 'n=1' assigns a value 1 to 'n'.
Then the condition 'n<=10' is checked. Since the value of 'n' is 1, therefore the code
in the body of for loop is executed and thus the current value of 'n' i.e., 1 gets
printed.
Once the codes in the body of for loop are executed, step n++ is executed which
increases the value of 'n' by 1. So now the value of 'n' is 2.
Again the condition 'n<=10' is checked which is true because the value of 'n' is 2.
Again codes in the body of for loop are executed and 2 gets printed and then the
value of 'n' is again incremented.
When the value of 'n' becomes 10, the condition 'n <= 10' is true and 10 gets
printed. Now, when n++ increases the value to 'n' to 11, the condition 'n<=10'
becomes false and the loop terminates.
Let's see the example of adding 10 numbers.

#include <iostream>
int main(){

using namespace std;


int sum = 0, i, n;

for(i = 0; i < 10; i++){

cout << "Enter number" << endl;


cin >> n;

sum = sum + n;

}
cout << "Sum is " << sum << endl;

return 0;

}
Output

Initially, the value of the variable sum is 0.


In the first iteration, the value of n is entered 4 and thus the value
of sum becomes 4 since sum = sum + n (i.e. sum = 0 + n).
In the second iteration, the value of sum is 4 and we entered the value of n as 3.
Therefore, the expression sum = sum + n gets evaluated as sum = 4 + 3, thus
making the value of sum as 7.
In this way, this loop will add all the 10 numbers entered by the user.
There are other ways also to write program of for loop.
The first example of for loop in which we printed the first 10 natural numbers can
also be written in other ways which are :

int n = 1;
for( ; n <= 10; n++)
{
    cout << n << endl;
}

Another way is shown below.

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(){

using namespace std;


int n = 1;
do{
cout << n << endl;
n++;
}while( n <= 10 );
return 0;
}

Output

Let's try to understand this.


At first, the statements inside the body of loop (i.e., within the braces { } following
do ) are executed. This will print the value of 'n' i.e., 1 and n++ increments the value
of 'n' by 1. So now, the value of 'n' becomes 2.
Once the code inside the braces { } is executed, condition 'n <= 10' is checked.
Since the value of 'n' is 2, so the condition is satisfied.
Again the code inside the body of loop is executed and the value of 'n' becomes 2.
When the value of 'n' is 10 and 10 is printed, n++ increases the value of 'n' to 11.
After this, the condition becomes false and the loop terminates.
As you have seen, in do while loop, codes inside the loop got executed for the first
time without checking any condition and then it started checking the condition
from the second time.
Nesting of loops
Like 'if/else' we can also use one loop inside another. This is called nesting of loop.
See this example to make it clear.

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

for(i = 12; i <= 14; i++){ /*outer loop*/

cout << "Table of " << i << endl;

for(j = 1; j <= 10; j++){ /*inner loop*/

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 5 is 5*4*3*2*1, or 120.).


Answer:

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

# Python program to find the factorial of a number provided by the user.

# change the value for a different result


num = 7

# To take input from the user


#num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero


if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

Output

The factorial of 7 is 5040

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

i=6 120 * 6 = 720

i=7 720 * 7 = 5040

Factorial of a Number using Recursion

# Python program to find the factorial of a number provided by the user


# using recursion

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))

# change the value for a different result


num = 7

# to take input from the user


# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)

Python Program to Find the Factorial


of a Number
In this article, you'll learn to find the factorial of a number and display it.

To understand this example, you should have the knowledge of the


following Python programming topics:
 Python if...else Statement
 Python for Loop
 Python Recursion

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

# Python program to find the factorial of a number provided by the user.


# change the value for a different result
num = 7

# To take input from the user


#num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero


if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

Output

The factorial of 7 is 5040

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

i=6 120 * 6 = 720

i=7 720 * 7 = 5040

Factorial of a Number using Recursion

# Python program to find the factorial of a number provided by the user


# using recursion

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))

# change the value for a different result


num = 7

# to take input from the user


# num = int(input("Enter a number: "))

# call the factorial function


result = factorial(num)
print("The factorial of", num, "is", result)
In the above example,  factorial()  is a recursive function that calls itself. Here,
the function will recursively call itself by decreasing the value of the  x .
Question#5
Write a program that asks the user to type in a number, and then prints the
first ten multiples of that number ignoring the number 1. (Hint: use the
modulus operand)
Answer:
In R:

1. 1:10*5 
In Julia:

1. [1:10;]*5 
In Python:

1. for i in range(10): print((i+1)*5) 


In Java:

1. public class Example { 


2. public static void main(String[] args) { 
3. for (int i=1; i<= 10; ++i) System.out.println(i*5); 
4. } 
5. } 
In C++:

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. }

You might also like