Skip to main content

PROBLEM SOLVING AND PYTHON PROGRAMMING QUIZ

1) What is the first step in problem-solving? A) Writing code B) Debugging C) Understanding the problem D) Optimizing the solution Answer: C 2) Which of these is not a step in the problem-solving process? A) Algorithm development B) Problem analysis C) Random guessing D) Testing and debugging Answer: C 3) What is an algorithm? A) A high-level programming language B) A step-by-step procedure to solve a problem C) A flowchart D) A data structure Answer: B 4) Which of these is the simplest data structure for representing a sequence of elements? A) Dictionary B) List C) Set D) Tuple Answer: B 5) What does a flowchart represent? A) Errors in a program B) A graphical representation of an algorithm C) The final solution to a problem D) A set of Python modules Answer: B 6) What is pseudocode? A) Code written in Python B) Fake code written for fun C) An informal high-level description of an algorithm D) A tool for testing code Answer: C 7) Which of the following tools is NOT commonly used in pr...

Control statements

CONTROL STATEMENTS
Till now we know that the code in the C program is executed sequentially from the first line of the program to its last line. That is, the second statement is executed after the first, the third statement is executed after the second, so on and so forth. Although this is true, in some cases we want only selected statements to be executed. Control flow statements enable programmers to conditionally execute a particular block of code. There are three types of control statements: decision control (branching), iterative (looping), and jump statements. While branching means deciding what actions have to be taken, looping, on the other hand, decides how many times the action has to be taken. Jump statements transfer control from one point to another point.

Decision Control Statements
C supports decision control statements that can alter the flow of a sequence of instructions. These statements help to jump from one part of the program to another depending on whether a particular condition is satisfied or not. These decision control statements include:
(a) if statement, (b) if–else statement,
(c) if–else–if statement, and (d) switch–case statement.

if Statement
if statement is the simplest decision control statement that is frequently used in decision making. The general form of a simple if statement is shown in Fig. below
fig: if statement construct
The if block may include 1 statement or n statements enclosed within curly brackets. First the test expression is evaluated. If the test expression is true, the statements of the if block are executed, otherwise these statements will be skipped and the execution will jump to statement x.
The statement in an if block is any valid C language statement, and the test expression is any valid C language expression that evaluates to either true or false. In addition to simple relational 
expressions, we can also use compound expressions formed using logical operators. Note that there is no semi-colon after the test expression. This is because the condition and statement should 
be put together as a single statement.
#include <stdio.h>
int main()
{
int x=10;
if (x>0) x++;
printf("\n x = %d", x);
return 0;
}
In the above code, we take a variable x and initialize it to 10. In the test expression, we check if the value of x is greater than 0. As 10 > 0, the test expression evaluates to true, and the value of x is incremented. After that, the value of x is printed on the screen. The output of this program is
x = 11
Observe that the printf statement will be executed even if the test expression is false.
Note In case the statement block contains only one statement, putting curly brackets becomes optional. If there are more than one statement in the statement block, putting curly brackets becomes mandatory.

if–else Statement
We have studied that using if statement plays a vital role in conditional branching. Its usage is very simple. The test expression is evaluated, if the result is true, the statement(s) followed by the 
expression is executed, else if the expression is false, the statement is skipped by the compiler.
What if you want a separate set of statements to be executed if the expression returns a false value? In such cases, we can use an if–else statement rather than using a simple if statement. 
The general form of simple if–else statement is shown in Fig. Below
fig: if-else statement construct
In the if–else construct, first the test expression is evaluated. If the expression is true, statement block 1 is executed and statement block 2 is skipped. Otherwise, if the expression is false, statement 
block 2 is executed and statement block 1 is ignored. In any case after the statement block 1 or 2 gets executed, the control will pass to statement x. Therefore, statement x is executed in every case.
Write a program to find whether a number is even or odd.
#include <stdio.h>
int main()
{
int a;
printf("\n Enter the value of a : ");
scanf("%d", &a);
if(a%2==0)
 printf("\n %d is even", a);
else
 printf("\n %d is odd", a);
return 0;
}
Output
Enter the value of a : 6
6 is even

if–else–if Statement
C language supports if–else–if statements to test additional conditions apart from the initial test expression. The if–else–if construct works in the same way as a normal if statement. Its construct is given in Fig. below
fig: if-else-if statement construct
Note that it is not necessary that every if statement should have an else block as C supports simple if statements. After the first test expression or the first if branch, the programmer can have as many else–if branches as he wants depending on the expressions that have to be tested. For example, the following code tests whether a number entered by the user is negative, positive, or equal to zero.
#include <stdio.h>
int main()
{
int num;
printf("\n Enter any number : ");
scanf("%d", &num);
if(num==0)
 printf("\n The value is equal to zero");
else if(num>0)
 printf("\n The number is positive");
else
 printf("\n The number is negative");
return 0;
}
Note that if the first test expression evaluates to a true value, i.e., num=0, then the rest of the statements in the code will be ignored and after executing the printf statement that displays ‘The value is equal to zero’, the control will jump to return 0 statement. 

switch–case Statement
A switch-case statement is a multi-way decision statement that is a simplified version of an if–else–if block. The general form of a switch statement is shown in Fig.below
fig: switch case statement construct
The power of nested if–else–if statements lies in the fact that it can evaluate more than one expression in a single logical structure. switch statements are mostly used in two situations:
* When there is only one variable to evaluate in the expression
* When many conditions are being tested for
When there are many conditions to test, using the if and else–if constructs becomes complicated and confusing. Therefore, switch case statements are often used as an alternative to long if
statements that compare a variable to several ‘integral’ values (integral values are those values that can be expressed as an integer, such as the value of a char). Switch statements are also used to handle the input given by the user.
We have already seen the syntax of the switch statement. The switch case statement compares the value of the variable given in the switch statement with the value of each case statement that 
follows. When the value of the switch and the case statement matches, the statement block of that particular case is executed.
Did you notice the keyword default in the syntax of the switch case statement? Default is the case that is executed when the value of the variable does not match with any of the values of the case statements. That is, default case is executed when no match is found between the values of switch and case statements and thus there are no statements to be executed. Although the default case is optional, it is always recommended to include it as it handles any unexpected case.
In the syntax of the switch–case statement, we have used another keyword break. The break statement must be used at the end of each case because if it is not used, then the case that matched and all the following cases will be executed. For example, if the value of switch statement matched with that of case 2, then all the statements in case 2 as well as the rest of the cases including default will be executed. The break statement tells the compiler to jump out of the switch case statement and execute the statement following the switch–case construct. Thus, the keyword break is used 
to break out of the case statements.
Advantages of Using a switch–case Statement
Switch–case statement is preferred by programmers due to the following reasons:
* Easy to debug
* Easy to read and understand
* Ease of maintenance as compared to its equivalent if–else statements
* Like if–else statements, switch statements can also be nested
* Executes faster than its equivalent if–else construct
Programming Example
Write a program to determine whether the entered character is a vowel or not.
#include <stdio.h>
int main()
{
char ch;
printf("\n Enter any character : ");
scanf("%c", &ch);
switch(ch)
{
 case ‘A’:
 case ‘a’:
 printf("\n %c is VOWEL", ch);
 break;
 case ‘E’:
 case ‘e’:
 printf("\n %c is VOWEL", ch);
 break;
 case ‘I’:
 case ‘i’:
 printf("\n %c is VOWEL", ch);
 break;
 case ‘O’:
 case ‘o’:
 printf("\n %c is VOWEL", ch);
 break;
 case ‘U’:
 case ‘u’:
 printf("\n %c is VOWEL", ch);
 break;
 default: printf("\n %c is not a vowel", ch);
}
return 0;
}
Output
Enter any character : j
j is not a vowel
Note that there is no break statement after case A, so if the character A is entered then control will execute the statements given in case a.

Popular posts from this blog

Abbreviations

No :1 Q. ECOSOC (UN) Ans. Economic and Social Commission No: 2 Q. ECM Ans. European Comman Market No : 3 Q. ECLA (UN) Ans. Economic Commission for Latin America No: 4 Q. ECE (UN) Ans. Economic Commission of Europe No: 5 Q. ECAFE (UN)  Ans. Economic Commission for Asia and the Far East No: 6 Q. CITU Ans. Centre of Indian Trade Union No: 7 Q. CIA Ans. Central Intelligence Agency No: 8 Q. CENTO Ans. Central Treaty Organization No: 9 Q. CBI Ans. Central Bureau of Investigation No: 10 Q. ASEAN Ans. Association of South - East Asian Nations No: 11 Q. AITUC Ans. All India Trade Union Congress No: 12 Q. AICC Ans. All India Congress Committee No: 13 Q. ADB Ans. Asian Development Bank No: 14 Q. EDC Ans. European Defence Community No: 15 Q. EEC Ans. European Economic Community No: 16 Q. FAO Ans. Food and Agriculture Organization No: 17 Q. FBI Ans. Federal Bureau of Investigation No: 18 Q. GATT Ans. General Agreement on Tariff and Trade No: 19 Q. GNLF Ans. Gorkha National Liberation Front No: ...

ELECTROMAGNETIC WAVES

Understanding Electromagnetic Waves: The Invisible Messengers of Energy Electromagnetic (EM) waves are everywhere around us, shaping the way we live and communicate, though most of the time we are unaware of their presence. From the light we see to the signals carrying our favorite songs on the radio, EM waves play a fundamental role in both nature and modern technology. In this post, we’ll explore the nature of electromagnetic waves, their types, and their significance in daily life. What Are Electromagnetic Waves? At their core, electromagnetic waves are fluctuations of electric and magnetic fields that travel through space. Unlike sound waves, which need a medium like air or water to propagate, electromagnetic waves can travel through a vacuum. This means they can traverse the vast emptiness of space, which is how sunlight reaches Earth from the Sun. The discovery of electromagnetic waves is credited to James Clerk Maxwell in the 19th century. He formulated a set of equations—now kn...

Introduction to C Programs

INTRODUCTION The programming language ‘C’ was developed by Dennis Ritchie in the early 1970s at Bell Laboratories. Although C was first developed for writing system software, today it has become such a famous language that a various of software programs are written using this language. The main advantage of using C for programming is that it can be easily used on different types of computers. Many other programming languages such as C++ and Java are also based on C which means that you will be able to learn them easily in the future. Today, C is mostly used with the UNIX operating system. Structure of a C program A C program contains one or more functions, where a function is defined as a group of statements that perform a well-defined task.The program defines the structure of a C program. The statements in a function are written in a logical series to perform a particular task. The most important function is the main() function and is a part of every C program. Rather, the execution o...