POP Mdel Paper 1 Solution
POP Mdel Paper 1 Solution
POP Mdel Paper 1 Solution
Solution to
Model Question Papers
Principles of Programming Using C
BPOPS103/ (22POP13)
WELCOME
to
VLA CREATIONS
Model
Question Paper-1
SOLUTION
A computer is a programmable electronic device that accepts raw data as input and processes it with a set of
instructions (a program) to produce the result as output
Apart from being classified by generations, computers can also be categorized by their size.
The size of a computer is often an indirect indication of its capabilities.
Supercomputers:
These are huge machines having the most powerful and fastest processors.
It uses multiple CPUsfor parallel data processing.
Speeds are measured in flops (floating-point operations per second). The
fastest operates at 34 petaflops.
They are used for weather forecasting and analysis of geological data. They
have enormous storage, use more power and generate a lot of heat. They are
used by government agencies.
Mainframes:
These are multi-user machines that support many users using the feature of time-sharing. It can
run multiple programs even with a single CPU.
The processor speed is measured in MIPS (Million instructions per second).
It is used to handle data, and applications related to organization and online transactions in banks, financial
institutions, and large corporations.
Minicomputers/Midrange computers:
It was introduced by DEC(Digital Equipment Corporation).
They can serve hundreds of users and are small enough to partially occupy a room. They
are used in smaller organizations or a department of a large one.
They are not affordable to be used at the home.
Microcomputers:
The microcomputer or PCis introduced by Apple and endorsed by IBM. This
is a single-user machine powered by a single-chip microprocessor. They are
very powerful machines having gigabytes of memory.
They are both used in standalone mode and in a network.
A microcomputer takes the form of a desktop, notebook (laptop), or netbook (smaller laptop). PCs
today are powered by 3 types of OS – windows (7, 8, or 10), Mac OS X (Apple), and Linux. They
are used for engineering and scientific applications and for software development.
• STEP 1: Start
• STEP 2: Take radius as input from the user using std input.
• [Read radius r]
• STEP 3: Calculate the area of circle using the formula
• [ area = (3.14)*r*r ]
• STEP 4: Calculate the Circumference using the formula
• [ circumference = 2*3.14 * r]
• STEP 5: Print the area and circumference to the screen using the std output.
• STEP6: Stop
• Speed Computers can perform millions of operations per second The speed of computers is usually
given in nanoseconds and picoseconds, where 1 nanosecond = 1 × 10 −9 seconds and
1 picosecond = 1 × 10 −12 seconds.
• Accuracy: A computer is a very fast, reliable, and robust electronic device. It always gives accurate results,
provided the correct data and set of instructions are input to it.
• Automation: Besides being very fast and accurate, computers are automatable devices that can
perform a task without any user intervention. The user just needs to assign the task to the computer,
after which it automatically controls different devices attached to it and executes the program
instructions.
• Diligence: Unlike humans, computers never get tired of a repetitive task. It can continually work for
hours without creating errors. Even if a large number of executions need to be executed, each and
every execution requires the same duration, and is executed with the same accuracy.
• Versatile: Versatility is the quality of being flexible. Today, computers are used in our daily life in different fields.
• Memory: Similar to humans, computers also have memory. Just the way we cannot store everything in our
memory and need secondary media, such as a notebook, to record certain important things, computers also
have internal or primary memory (storage space) as well as external or secondary memory. While the internal
memory of computers is very expensive and limited in size, the secondary storage is cheaper and of bigger
capacity.
Variable is the name of a memory location that we use for storing data. We can change the value
of a variable in C or any other language, and we can also reuse it multiple times.
Rules for Forming Variable
• Variables cannot include any special characters or punctuation marks (like #, $, ^, ?, ., etc.)
except the underscore_.
• There cannot be two successive underscores.
• Keywords cannot be used as identifiers.
• The case of alphabetic characters that form the variable name is significant.
For example, "FIRST' is different from 'first' and 'First'.
• Variables can be a combination of characters and numbers.
i)valid vi) valid vii) invalid
ii) Invalid
iii) invalid
iv) valid
v) invalid
#include<stdio.h>
int main()
{
int p,r,t,SI;
printf(Enter principle amount, Rate of interest and time to find simple interest:);
scanf(“%d %d %d”, p, r, t);
SI=(p*r*t)/100;
printf(“Simple interest = %d”,&SI);
return 0;
}
OUTPUT
Enter principle amount, Rate of interest and time to find simple interest
18000 2 6
Simple interest: 2160
Relational operators: The operators that are used to find the relationship between two operands are called
relational operators.
The relationship between the two operand values results in true (always 1) or false (always 0).
Relational operators available in C are:
All the relational operators are having a same priority and left to right associativity.
The relational operators have lower precedence than arithmetic operators.
Logical operators
The operators that are used to combine two or more relational expressions
are called logical operators.
The output of relational expression is true or false, the output of
logical expression is also true or false.
• Logical NOT: The logical NOT operator is denoted by „!‟. The output of not operator can be true or false.
The result is true if the operand value is false and the result is false if the operand is true.
• Logical AND: The logical AND operator is denoted by „&&‟. The output of and operator is true if both the
operands are evaluated to true. If one of the operand is evaluated false, the result is false.
• Logical OR: The logical OR operator is denoted by „||‟. The output of or operator is true if and only if at
least one of the operands is evaluated to true. If both the operands are evaluated to false, the result is
false.
• Ex: Given A=10, Evaluate C=!A
• A=10 other than value 0, any value is treated as 1.
So, A=1
• C = !A
• C = !1
• C=0
#include <math.h> printf("root1 = %.2f and root2 = %.2f", root1, root2);
#include <stdio.h> }
int main( ) { // condition for real and equal roots
float a, b, c, disc, root1, root2, realPart, imagPart; else if (disc == 0) {
printf("Enter coefficients a, b and c: "); root1 = root2 = -b / (2 * a);
scanf("%f %f %f", &a, &b, &c); printf("Roots are real and equal \n");
//check for zero coefficients printf("root1 = root2 = %.2f ", root1);
if((a==0)||(b==0)||(c==0)) }
{ // if roots are not real
printf("Invalid coefficients\n"); else {
return 0; realPart = -b / (2 * a);
} imagPart = sqrt(-disc) / (2 * a);
disc = b * b - 4 * a * c; printf("Roots are imaginary \n");
// condition for real and different roots printf("root1 = %.2f+%.2fi and root2 = %.2f - %.2fi",
if (disc> 0) realPart, imagPart, realPart,imagPart);
{ }
root1 = (-b + sqrt(disc)) / (2 * a); return 0;
root2 = (-b - sqrt(disc)) / (2 * a); }
printf("Roots are real and distinct \n");
C-language provides goto statement to transfer control unconditionally to other part of the program. Although
use of goto statement is not advisable but sometime it is desirable to use go to statement. It has the following
form:
goto statement requires a label to determine where to transfer the control. A label
must end with colon (:). We can use any valid name as a label similar to variable name. When
compiler encounters goto statement with a label name then, it transfers the control to the
location where label has been defined in the program
Example: #include<stdio.h>
void main()
{
printf(“Bangalore \t”);
goto label;
printf(“is a \t”);
lable: printf(“smart city”);
}
Output:
Bangalore smart city
The „switch‟ statement is a control statement used to make a select one alternative among several alternatives.
It is a “multi-way decision statement” that tests whether an expression matches one of the case values and
branches accordingly.
Here switch, case, break and default are built-in C language words.
•If the choice matches to label1 then block1 will be executed else if it evaluates to label2 then block2 will be
executed and so on.
•If choice does not matches with any case labels, then default block will be executed
Example: case '/': result = num1 / num2;
#include <stdio.h> break;
void main() default: printf("\n You have entered an Invalid Operator
{ ");
char Operator; }
float num1, num2, result = 0; printf("\n The result of %f %c %f = %f", num1, Operator,
printf("Enter an Operator (+, -, *, /) :\n "); num2, result);
scanf("%c", &Operator); }
printf("Enter the Values for two Operands: num1 and
num2 : \n ");
scanf("%f%f", &num1, &num2);
switch(Operator)
{
case '+': result = num1 + num2;
break;
case '-': result = num1 - num2;
break;
case '*': result = num1 * num2;
break;
Refer example of 3(c) for 4(a)
Formatting Input/Output
C language supports two formatting functions printf and scanf.
printf is used to convert data stored in the program into a text stream for output to the monitor.
scanf is used to convert the text stream coming from the keyboard to data values and stores them in
program variables.
printf()
The printf function (stands for print formatting) is used to display information
required by the user and also prints the values of the variables. For this, the print function takes data
values, converts them to a text stream using formatting specifications in the control string and passes the
resulting text stream to the standard output. The control string may contain zero or more conversion
specifications, textual data, and control characters to be displayed.
printf("control string", variable list);
The function accepts two parameters-control string and variable list. The control string may also contain
the text to be printed like instructions to the user, captions, identifiers, or any other text to make the output
readable.
Examples:
printf("\n The number is %6d", 12);
O/p: The number is 12
scanf()
The scanf() function stands for scan formatting and is used to read formatted data
from the keyboard. The scanf function takes a text stream from the keyboard, extracts and formats data
from the stream according to a format control string and then stores the data in specified program
variables. The syntax of the scanf() function can be given as: scanf("control string", arg1, arg2,
arg3,….argn);
The control string specifies the type and format of the data that has to be obtained
from the keyboard and stored in the memory locations pointed by arguments arg1, arg2,, argn, i.e., the
arguments are actually the variable addresses where each piece of data is to be stored.
Ex: int num;
scanf("%d", &num);
The scanf function reads an integer value (because the type specifier is %d) into the address or the
memory location pointed by num.
if statement
The if statement is a powerful decision-making statement and is used to control the
flow of execution of statements. It is basically a two-way decision statement and is used in conjunction with an
expression. But only if statement is a one way selection which executes statements based on whether conditional
statement is true/false. Relational operators <=,<,>,= =, > = and != are used to specify conditions.
Syntax: if(test expression)
{
statement(s);
}
Example: #include<stdio.h>
void main()
{
int age;
printf(“Enter age of person:\n”);
scanf(“%d”,&age);
if(age>60)
printf(“Eligible for pension”);
}
if…else statement
The if…else statement is an extension of the simple if condition statement. if..else
statement is two way selection statement which executes statements when the conditional expression in the if is true,
if it is false then statement under else are executed.
Syntax: Example:
if(test expression) #include<stdio.h>
{ void main()
True-block statement(s) { int age;
} printf(“Enter age of person:\n”);
else scanf(“%d”,&age);
{ if(age>60)
False-block statement(s) printf(“Eligible for pension”);
} else
Statement-x printf(“Not eligible for pension”);
}