You are on page 1of 19

1.

Write an algorithm and flowchart to find out whether a given integer is zero, +ve or -ve
and explain it.
⇒ The algorithm to check if the given number is zero, +ve or -ve is:
Step 1. Start
Step 2. Input a number
Step 3. Compare the number with zero.
If the number is equal to zero display “The number is zero.”
If the number is less than zero display “The number is -ve.”
If the number is more than zero display “The number is +ve.”
Step 4. End
Flowchart here
2. What is a logical error ?
⇒A logic error is a mistake in a program that causes it to operate wrongly, but not to
terminate abnormally. A logic error produces unintended or undesired output.

3. Write a flowchart and program or check whether the number entered by the user is exactly
divisible by 5 or 11 .

#include<stdio.h>
int main()
{
int number;
printf(“Enter a number”);
scanf(“%d”,& number);
If (n%5==0 )
{
printf(“%d is divisible by 5”;number);
}
Elseif (n%11==0)
{
printf(“%d is divisible by 11”;number);
}
else
{
printf(“not divisible by both”);
}
return 0;
}
Flowchart here

3.What is an algorithm?Write an algorithm to check if a given number is palindrome or not.


⇒An algorithm is a process or set of rules to be followed in calculations or other problem-solving
operations, especially by a computer.
STEP 1-Start
STEP 2-Initialize rev=0,r,a=n
STEP 3-input n
STEP 4-If n>0
If yes goto 5
If no goto 8
STEP 5-Calculate r=n%10
STEP 6-Calculate rev=rev*10+r
STEP 7-Calculate n=n/10
STEP 8-If rev==a
If yes,print palindrome
If no,print not palindrome
STEP 9-End

4.Differentiate between if and switch statement.WAP to read marks off our subjects and find
total percentage and division according to given condition.

⇒Both “if-else” and “switch” are selection statements.

The if statement is used to select among two alternatives. It uses a boolean expression to
decide which of the alternatives should be executed.

if(condition)

{… }

else {.…..}

The switch statement is used to select among multiple alternatives. It uses an expression
to determine which alternative should be executed.

Switch(expression){

Case value1: …

Case value2: …

Default: …..

● ‘if’ is difficult to understand if the number of conditions are more.


● If the expression inside ‘if' turns out to be false then the statement inside
the else block will be executed. If the expression inside ‘switch' turns out to
be false then the default statement is executed.
● ‘if’ statement checks for equality as well as for logical expression. On the
other hand, ‘switch' checks only for equality.
● You can have multiple ‘if' statements for multiple choices of statements.

int main()

int m,c,i,d,p,t,per;

printf("Input marks in maths,cp,iit,digital logic and physics");

scanf("%d%d%d%d%d",&m,&c,&i,&d,&p);

t=m+c+i+d+p;

per=(t\500)*100;

if (m>=40 && c>=40 && i>=40 && d>=40 && p>=40)

if (per<100 && per>=80)

printf("Distinction");

else if (per<80 && per>=70)


{

printf("First division");

else if (per<70 && per>=50)

printf("Second Division");

else if (per<50 && per>=40)

printf("Third division");

else

printf("Failed");

else

{
printf("Failed");

return 0;

5. Explain the structure of the C–program with examples.


Documentation section:The documentation section consists of a set of comment lines giving
the name of the program, the author and other details, which the programmer would like to
use later.

Link section:The link section provides instructions to the compiler to link functions from the
system library.

Definition section:The definition section defines all symbolic constants.

Global declaration section:There are some variables that are used in more than one
function. Such variables are called global variables and are declared in the global
declaration section that is outside of all the functions. This section also declares all the
user-defined functions.

main()function section:Every C program must have one main function section. This section
contains two parts; declaration part and executable partDeclaration part : The declaration
part declares all the variables used in the executable part.Executable part : There is at least
one statement in the executable part. These two parts must appear between the opening
and closing braces. The program execution begins at the opening brace and ends at the
closing brace. The closing brace of the main function is the logical end of the program. All
statements in the declaration and executable part end with a semicolon.

Subprogram section:The subprogram section contains all the user-defined functions that
are called in the main () function. User-defined functions are generally placed immediately
after the main () function, although they may appear in any order. Note:All sections, except
the main () function section may be absent when they are not required.

6.Write a flowchart and algorithm to find whether a number is divisible by 5 or 7.

STEP 1:Start
STEP 2:Initialize n
STEP 3:Input n
STEP 4:If n%5==0
If yes print divisible by 5
If no goto 5
STEP 5:If n%11==0
If yes print divisible by 11
If no print neither divisible by 5 or 11
STEP 6:End
Flowchart here

7.Draw a flowchart and an algorithm to find out whether a given number is positive, negative
or zero .

Same as no. 1

8.What is structure ? How is it different from an array ?


Create a structure named student having data members' name roll number and
percentage.Complete the program to display the name of student having percentage
greater than 60.

Structure is a user defined data type in C . A structure creates a data type that can be used
to group items of possibly different types of data into a single type.It is a convenient way of
grouping several pieces of related information together.

Syntax:
Struct structure_name
{
Data type varname1;
Data type varname2;
……………………………….
……………………………….
};

Array and structure both are container data type . The major difference between them is
that an array contains all the elements of the same data type with its size defined during its
declaration whereas structure contains elements of different data types and its size is
determined by the number of elements declared in a structure when it is defined.

#include<stdio.h>
Struct student
{
Char name[20];
int roll;
int percentage;
};
Void main( )
{
Struct student s[100];
int n,i;
printf(“Enter number of students”);
scanf(“%d”,&n);
printf(“Enter record of %d student”,&n);
for(i=0;i<n;i++)
{
scanf(“%s%d%d”,&s[i].name,&s[i].roll,&s[i].percentage);
}
for(i=0;i<n;i++)
{
if (s[i].percentage>=60)
{
printf(“%s\n”,s[i].name);
}
}
getch( );
}

What is an algorithm ? How does it differ from flowchart?

Algorithm:An algorithm is a finite sequence of instructions for solving a stated problem.A


stated problem is a well-defined task that has to be performed and must be solvable by an
algorithm. The algorithm must eventually terminate,producing the required result.

Properties of a good algorithm

Input:A number of quantities are provided to an algorithm initially before the algorithm
begins.These quantities are inputs which are processed by the algorithm.

Definiteness:The processing rules specified in the algorithm must be unambiguous and lead
to a specific action.

Effectiveness:Each instruction must be sufficiently basic such that it can,in principle,be


carried out in a finite time by a person with a pencil and paper.

Finiteness:The total time to carry out all the steps in an algorithm must be finite.

Correctness:Correct set of output must be produced from the set of inputs.For the same
input,it must always produce the same output.

Eg;
Algorithm to find roots of quadratic equations.

STEP 1:Start
STEP 2:Input a,b,c
STEP 3:Calculate discriminant d=b^2-4*A*C
STEP 4:IF D=0 then
Print roots are equal
root1=root2=-b/2a
STEP 5:else if d>0 then
Root1=(-b+(b^2-4*A*C)^o.5)/2a
Root2=(-b-(b^2-4*A*C)^o.5)/2a
STEP 6:else print”roots are imaginary”
STEP 7:print Root1 and Root2.
STEP8:Stop

A flowchart is a common type of chart that represents an algorithm or a computer program


showing the steps as boxes of various kinds,,and their order by connecting these with
arrows.Flowcharts are used in analyzing,designing,documenting or managing a process or
program in various fields.
Eg : flowchart to differentiate positive , negative and zero.

Write a program to check whether an entered number is odd or even.

#include<stdio.h>
int main( )
{
int num;
printf(“Enter an integer”);
scanf(“%d”,&num);
if(num%2==0)
{
printf(“%d is even”,num);
}
else
{
printf(“%d is odd”,num);
}
return 0;
}

Write a program to calculate sum of first 10 odd numbers

#include<stdio.h>
int main( )
{
int s=0,i;
for(i=1;i<=10;i++)
{
if(i%2!=0)
{
s=s+i;
}
}
printf(“Sum of first 10 odd numbers is %d”,s);
return 0;
}

What is preprocessor directive?Discuss # define directive with example.

Preprocessor directives are the lines in a C program that begins with the character #
making them different from typical source code text.They are invoked by the compiler to
process some programs before compilation...Preprocessor directive changes the text of the
source code and the result is a new source code without these directives.

In C programing language,the # define directive directive allows the definition of macros


within your source code.These macro definitions allow constant values to be declared for
use throughout your code.
Macro definitions are not variables and cannot be changed by your program code like
variables.You generally use this syntax when creating constants that represent numbers,
strings or expressions.

Syntax

The syntax for creating a constant using #define in the c language is :

#define CNAME value


OR
#define CNAME(expression)
Where,
● CNAME : The name of constant . most c programmers define their constant names
in uppercase, but it is not a requirement of c language.
● Value : The value of the constant.
● Expression : Expression whose value is assigned to the constant .The expression
must be enclosed in parentheses if it contains operators.
Example

#include<stdio.h>
#define NAME”Aarav”
#define AGE 10
int main ( )
{
printf(“%s is over %d years old.”,NAME,AGE);
return 0;
}

What do you mean by programming language ? Differentiate between high level and low
level language.

A language for expressing a set of detailed instructions for a digital computer is called a
programming language.Although there are many computer languages, relatively few are widely
used.

High level Language-Sometimes abbreviated as HLL, a high-level language is a computer


programming language that isn't limited by the computer, designed for a specific job, and is
easier to understand. It is more like human language and less like machine language. However,
for a computer to understand and run a program created with a high-level language, it must be
compiled into machine language.

The first high-level languages were introduced in the 1950s. Today, high-level languages are in
widespread use. These include BASIC, C, C++, COBOL, FORTRAN, Java, Pascal, Perl, PHP,
Python, Ruby, and Visual Basic.

A low-level programming language is a programming language that provides little or no


abstraction from a computer's instruction set architecture—commands or functions in the
language map closely to processor instructions. Generally, this refers to either machine code or
assembly language. Because of the low (hence the word) abstraction between the language
and machine language, low-level languages are sometimes described as being "close to the
hardware". Programs written in low-level languages tend to be relatively non-portable, due to
being optimized for a certain type of system architecture.

Explain machine and assembly language with merits and demerits.

1.Machine Language-Machine language is the lowest level of programming language .It


consists of the numeric codes for the operations that a particular computer can execute directly.
The codes are strings of 0s and 1s, or binary digits (“bits”), which are frequently converted both
from and to hexadecimal (base 16) for human viewing and modification.

Machine language instructions typically use some bits to represent operations, such as addition,
and some to represent operands, or perhaps the location of the next instruction. Machine
language is difficult to read and write, since it does not resemble conventional mathematical
notation or human language, and its codes vary from computer to computer.

2.Assembly Language-Assembly language is one level above machine language. It uses short
mnemonic codes for instructions and allows the programmer to introduce names for blocks of
memory that hold data. One might thus write “add pay, total” instead of “0110101100101000” for
an instruction that adds two numbers.

Assembly language is designed to be easily translated into machine language. Although blocks
of data may be referred to by name instead of by their machine addresses, assembly language
does not provide more sophisticated means of organizing complex information.
Like machine language, assembly language requires detailed knowledge of internal computer
architecture. It is useful when such details are important, as in programming a computer to
interact with input/output devices (printers, scanners, storage devices, and so forth).

What do you mean by problem analysis?

Problem Analysis is the stage which begins with reviewing the program specifications indicating
what the new system should do.During this stage , the system analyst and programmer review
the specifications and possibly talk with users in order to fully understand what the software
should do .It consists of following steps :

● Determine objectives of the program


● Determine desired outputs
● Determine input requirements
● Determine processing requirements
● Evaluate feasibility of the program
● Document the analysis

What is an algorithm ? Explain its characteristics.

Done

What is flowchart ? Explain symbols used in flowchart with their semantics.

A flowchart is a common type of chart that represents an algorithm or a computer program


showing the steps as boxes of various kinds,,and their order by connecting these with
arrows.Flowcharts are used in analyzing,designing,documenting or managing a process or
program in various fields.

1. The Oval
An End or a Beginning

The oval, or terminator, is used to represent the start and end of a

process. Remember to use the same symbol again to show that your flowchart is

complete.
2. The Rectangle
A Step in the Flowcharting Process

The rectangle is your go-to symbol once you've started flowcharting. It represents any

step in the process you’re diagramming and is the workhorse of the flowchart diagram.

Use rectangles to capture process steps like basic tasks or actions in your process.

3. The Arrow
Indicate Directional Flow

The arrow is used to guide the viewer along their flowcharting path. And while there are

many different types of arrow tips to choose from, it is recommended sticking with one

or two for your entire flowchart. This keeps your diagram looking clean, but also allows

you to emphasize certain steps in your process.


4. The Diamond
Indicate a Decision

The diamond symbolizes that a decision is required to move forward. This could be a

binary, this-or-that choice or a more complex decision with multiple choices.

5.The parallelogram

This symbol indicates output or input.

Define program compilation and exclusion. Explain compilation process with suitable

block diagram.

Program compilation is the step performed on each output of the preprocessor. A code of a
program written in high level language is called source code;which is not directly

understandable by the computer.Hence, the source code should be translated into computer

understandable form,ie.machine level language.The translation process is called

compilation;which is done by a special software called compiler.

The machine level code that we set after compilation is called object code.Object code is the

executable file that is compatible with particular platform(Windows,Linux etc)

Execution of a program involves loading the executable object code into the computer memory

and executes the instructions.While executing the program,the program may load data from

memory or keyboard.
Explain history of c language in brief:

The C programming language came out of Bell Labs in the early 1970s. According to the Bell

Labs paper The Development of the C Language by Dennis Ritchie, “The C programming

language was devised in the early 1970s as a system implementation language for the nascent

Unix operating system. Derived from the typeless language BCPL, it evolved a type structure;

created on a tiny machine as a tool to improve a meager programming environment.” Originally,

Ken Thompson, a Bell Labs employee, desired to make a programming language for the new

Unix platform. Thompson modified the BCPL system language and created B. However, not
many utilities were ever written in B due to its slow nature and inability to take advantage of

PDP-11 features in the operating system. This led to Ritchie improving on B, and thus creating

C.

The development of C was to become the basis for Unix. According to the Bell Labs paper, “By

early 1973, the essentials of modern C were complete. The language and compiler were strong

enough to permit us to rewrite the Unix kernel for the PDP-11 in C during the summer of the

year.” This now meant that C was becoming a strong language that could, and would be,

implemented across many systems. By the middle of the 1970s, the C-based Unix was used in

many projects within the Bell System as well as “a small group of research-oriented industrial,

academic, and government organizations outside [Bell Labs]".

C has changed over the years and is still a common language to use in lower level programs,

like kernels. But it is also used for many applications ranging from device drivers to other

programming languages’ compilers or interpreters. The language also made way for C++,

Objective-C, C#, and many more C-based languages that each have their own speciality.

Explain structure of c program with example.

Done

Write an algorithm and flowchart to determine if a number is positive, negative or zero.

Done

Write an algorithm and flowchart to find the smallest among three numbers.

Algorithm

STEP 1:Start

STEP 2:Input 3 numbers a,b,c

STEP 3:compare a<b


If yes,goto 4

If no,goto 5

STEP 4:compare a<c

If yes,print a is smallest,goto 6

If no,print c is greatest,goto 6

STEP 5:compare b<c

If yes , print b is smallest,goto 6

If no,print c is smallest,goto 6

STEP 6:end

Write an algorithm and draw a flowchart to find real roots of quadratic equation.

Done

Write an algorithm and draw a flowchart to find if a number is prime or not.

You might also like