0% found this document useful (0 votes)
10 views37 pages

PPSL Lab manual Pr 1-3

The document is a lab manual for the Programming for Problem Solving Lab (N-ESC102P) at S. B. Jain Institute of Technology, detailing hardware and software requirements, general instructions for students, and a list of practical exercises. It includes aims, objectives, and theoretical background on computer systems, operating systems, and programming languages, specifically focusing on C programming. The manual also provides installation instructions for IDEs like Dev C++ and Turbo C++ and outlines various types of operators in C.

Uploaded by

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

PPSL Lab manual Pr 1-3

The document is a lab manual for the Programming for Problem Solving Lab (N-ESC102P) at S. B. Jain Institute of Technology, detailing hardware and software requirements, general instructions for students, and a list of practical exercises. It includes aims, objectives, and theoretical background on computer systems, operating systems, and programming languages, specifically focusing on C programming. The manual also provides installation instructions for IDEs like Dev C++ and Turbo C++ and outlines various types of operators in C.

Uploaded by

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

Programming for Problem Solving Lab (N-ESC102P)

S. B. JAIN INSTITUTE OF TECHNOLOGY,


MANAGEMENT & RESEARCH, NAGPUR.
Session 2024-25

LAB MANUAL
Programming for Problem Solving Lab (N-ESC102P)

Year: 1st
Semester: I

Name of Student:
Roll No.:
Semester/Year:
Academic Session:
Date of Performance:
Date of Submission:

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

Hardware and Software Requirement

Hardware Requirement
● Processor : Dual Core
● RAM : 1GB
● Hard Disk Drive : > 80 GB

Software Requirement
● Operating System – Windows 2007 and Ubuntu
● Package used – gcc/g++
● IDE - TURBO C++, DEV C++

GENERAL INSTRUCTIONS FOR STUDENTS

DO’S
● Students should enter into the Laboratory with prior permission.
● Students should come in proper uniforms.
● Students should come with Practical note book to the laboratory.
● Students should maintain silence inside the laboratory.
● After completing the laboratory exercise, make sure to shut down the system and arrange
chairs properly.

DONT’S
● Students bringing the bags inside the laboratory.
● Students using mobile phones inside the laboratory.
● Students using the computers in an improper way.
● Students scribbling on the desk and mishandling the chairs.
● Students making noise inside the laboratory.

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

List of Practicals
Name of Lab Course: Programming for Problem Solving (N-ESC102P)
Sr.
Name of Practical CO
No.
PRE-LAB SESSION: To work with computer systems, various operating systems and
0 --
installation of various C language IDEs.
Construct a C program to demonstrate the working of various operators.
a. Construct a C program to perform an arithmetical operation by using the
assignment operator.
1 CO1
b. Construct a C program that compares two numbers using various relational
operators.

Construct a C program to demonstrate the working of decision-making statements.


a. Construct a C program to find the largest among 3 numbers.
2 CO1
b. Construct a C program to check whether a number is even positive, odd positive
or negative.
Construct a C program to demonstrate the iterative programming method.
3 a. Construct a C program to check whether a number is prime or not. CO1
b. Construct a C program to print a full pyramid asterisk pattern (*).
Construct a C program to demonstrate arrays.
a. Construct a C program to multiply two matrices of equal size and display the result.
4 CO2
b. Construct a C program that Constructs string copy operation STRCOPY (str1, str2)
that copies a string str2 to another string str1 without using the library function.
Develop a C programs to demonstrate function.
5 a. Develop a C program to compute the volume of a cuboid (Call by value). CO3
b. Develop a recursive C function to find the factorial of a number.
Build a C program to demonstrate the working of pointers.
a. Build a C program to store 5 elements in an array and print them with the help of
6 CO4
a pointer.
b. Build a program in C to calculate the length of a string using a pointer.
Build a C program to demonstrate the working of structure.
a. Build a C program to store the information of 5 students (name, roll no, marks in
2 subjects) and Print them onto the terminals.
7 b. Build a C program to print the name, employee ID, age, designation, and gross CO4
salary of 5 employees. Where gross sal =BS+ DA+ HRA+ other, DA=21% of BS,
HRA=51% of BS, other allowances=5% of BS. Take the appropriate input from the
keyboard.
Create a C program to demonstrate various file operations.
8 a. Create a C program to read file contents and display them on the console. CO5
b. Create a C program to read numbers from a file and display even numbers.

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

S. B. JAIN INSTITUTE OF TECHNOLOGY,


MANAGEMENT & RESEARCH, NAGPUR.

PRE-LAB
AIM: To work with computer system, various operating system and
installation of various C language IDEs.

Name of Student:
Roll No.:
Semester/Year:
Academic Session:
Date of Performance:
Date of Submission:

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

AIM: To work with computer system, various operating system and installation of various c
language IDEs.

OBJECTIVE/EXPECTED LEARNING OUTCOME:


● To be able to understand computer system, various operating system.
● Navigate the operating system and start applications.
● To be able to understand & installation of various IDEs for C language.

THEORY:
Computer
A computer is a machine that can be programmed to carry out sequences of arithmetic or logical
operations automatically.
The brain of a computer is CPU. It has three components- Memory unit, Control unit and
Arithmetic and Logical unit (ALU)- Memory unit also called storage device is to store information.
Two types of memory are there in a computer. They are RAM (random access memory) and ROM
(read only memory). When a program is called, it is loaded and processed in RAM. When the
computer is switched off, whatever stored in RAM will be deleted. So, it is a temporary memory.
Whereas ROM is a permanent memory, where data, program etc are stored for future use. Inside
a computer there is a storage device called Hard disk, where data are stored and can be accessed
at any time.
The control unit is for controlling the execution and interpreting of instructions stored in
the memory. ALU is the unit where the arithmetic and logical operations are performed. The
Physical components of a computer are called hard wares. But for the machine to work it requires
certain programs (A set of instructions is called a program). They are called soft wares. There are
two types of soft wares – System software and Application software.
All general-purpose computers require the following hardware components:
● Memory: Memory enables a computer to store, at least temporarily, data and programs.
● Mass storage device: This allows a computer to permanently retain large amounts of data.
Common mass storage devices include solid state drives (SSDs) or disk drives and tape drives.

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

● Input device: Usually a keyboard and mouse, the input device is the conduit through which
data and instructions enter a computer.
● Output device: An output device is a display screen, printer, or other device that lets you
see what the computer has accomplished.
● Central processing unit (CPU): The heart of the computer, this is the component that
actually executes instructions.
● A motherboard: This component allows all of the other components to communicate with
one another.

Operating Systems
The set of instructions which resides in the computer and governs the system are called operating
systems, without which the machine will never function. They are the medium of communication
between a computer and the user. DOS, Windows, Linux, Unix etc are Operating Systems.

Block Diagram of Operating System


Dual boot/ multiple boot:
Multi-booting is the act of installing multiple operating systems on a single computer, and being
able to choose which one to boot. The term dual-booting refers to the common configuration of
specifically two operating systems. Multi-booting allows more than one operating system to

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

reside on one computer. if you have a primary operating system and an alternate system that you
use less frequently. Another reason for multi-booting can be to investigate or test a new operating
system without switching completely.

Windows:
Microsoft Windows has existed in one form or another since 1985, and it remains the most popular
operating system for home and office computers. Computers from a wide variety of manufacturers can
use Windows. Initial versions of Windows worked with an earlier Microsoft operating system called
MS-DOS, providing a modern graphical interface on top of DOS's traditional text-based commands.
Signature features of Microsoft Windows's user interface include windows themselves – rectangle-
shaped, on-panel screens that represent individual applications. The Windows Start menu has helped
generations of users find programs and files on their devices.

Linux:
Linux is an open source operating system (OS). An operating system is the software that directly
manages a system's hardware and resources, like CPU, memory, and storage. The OS sits between
applications and hardware and makes the connections between all of your software and the
physical resources that do the work. Linux terminal is a user-friendly terminal as it provides
various support options. To open the Linux terminal, press "CTRL + ALT + T" keys together, and
execute a command by pressing the 'ENTER' key.

Computer Languages
Languages are a means of communication. Normally people interact with each other through a
language. On the same pattern, communication with computers is carried out through a language.
This language is understood both by user and the machine. Just as every language like English,
Hindi has its grammatical rules; every computer language is bound by rules known as SYNTAX
of that language. The user is bound by that syntax while communicating with the computer system.
The software (set of programs) that reads a program written in high level language and
translates it into an equivalent program in machine language is called as Compiler. The program

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

written by the programmer in high level language is called source program and the program
generated by the compiler after translation is called as object program.

IDEs
An integrated development environment (IDE) is a software application that provides
comprehensive facilities to computer programmers for software development. It helps a
programmer to program easily by providing all the comprehensive facilities required for the
development of software. IDE can improve the productivity of a programmer or developer because
of its fast setup and various tools. Without this, a programmer takes a lot of time deciding on
various tools to use for their tasks.
Mainly, an IDE includes 3 parts i.e. source code editor, a build automation tool
(compiler), and a debugger. The source code editor is something where programmers can write the
code, whereas, build automation tool is used by the programmers for compiling the codes and the
debugger is used to test or debug the program in order to resolve any errors in the code.

Installation of IDEs
Dev C++
Step 1: First you must download the Dev C++ on your Windows machine. Visit to Download
Dev C++: https://sourceforge.net/projects/orwelldevcpp/
Step 2: This package will download C++ .exe file for Windows that can be used to install on
Windows 7/8/XP/Vista/10.
Step 3: Double click the executable file.
Step 4: The installer will ask you a language to select. Select “English” and click on “OK”.
Step 5: Then screen for license agreement will appear. Click on “I agree” to proceed further.
Step 6: You can see different components of Dev C++ that will be installed with this package.
Just click on “next” button.
Step 7: By default, the destination folder is in C drive. You are free to change this destination
folder but make sure you have enough memory. Click on “Install” button.
Step 8: In the next screen, installation begins.
Step 9: Now, Dev C++ is installed successfully on your Windows. Select “Run Dev C++” to run
it and click on “Finish” button.

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

Step 10:That’s it! Now you are ready to compile your C or C++ programs with Dev C++ compiler.

Turbo C++
Step 1: First you must download the Dev C++ on your Windows machine. Visit to Download Dev
C++: https://developerinsider.co/download-turbo-c-for-windows-7-8-8-1-and-windows-10- 32-
64-bit-full-screen/
Step 2: Now, you need to create a new directory turbo c inside the c: drive. Now extract the zip
file in c:\turboc directory.
Step 3: Double click on the install.exe file and follow steps. Now, click on the install icon located
inside the c:\turboc.
Step 4: Click on next.
Step 5: In the next screen, installation begins.
Step 6: After installation click on next, then click on finish.
Step 7: Now it will be showing console.

CONCLUSION: Hence we have studied about computer system, operating system and
installation of various C language IDEs.

REFERENCE:
● https://www.youtube.com/watch?v=rRSD128KWIM
● https://www.youtube.com/watch?v=g3GUnBZEPwQ
● https://en.wikipedia.org/wiki/Computer
● https://www.webopedia.com/definitions/computer/
● https://sourceforge.net/projects/orwelldevcpp/

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

S. B. JAIN INSTITUTE OF TECHNOLOGY,


MANAGEMENT & RESEARCH, NAGPUR.
Practical No. 1
Aim: Construct a C program to demonstrate the working of various
operators.
a. Construct a C program to perform an arithmetical operation by using
the assignment operator
b. Construct a C program that compares two numbers using various
relational operators

Name of Student:
Roll No.:
Semester/Year:
Academic Session:
Date of Performance:
Date of Submission:

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

Aim: Construct a C program to demonstrate the working of various operators.

a. Construct a C program to perform an arithmetical operation by using the assignment operator

b. Construct a C program that compares two numbers using various relational operators

OBJECTIVE/EXPECTED LEARNING OUTCOME:


● To be able to implement one or more operation to be evaluated or tested by the program.
● To be able to understand the use of operator on program.

THEORY:
Operator is a symbol that tells the compiler to perform specific mathematical, conditional, or
logical functions. It is a symbol that operates on a value or a variable. For example, + and - are the
operators to perform addition and subtraction in any C program.
Types of Operators in C
1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Bit wise operators
6. Conditional operators (ternary operators)
7. Increment/decrement operators
8. Special operators

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

1. Arithmetic Operators
C Arithmetic operators are used to perform mathematical calculations like addition, subtraction,
multiplication, division and modulus in C programs. Assume variable A holds 10 and variable B
holds 20 then.
Operator Description Example

+ Adds two operands. A + B = 30

− Subtracts second operand from the first. A − B = -10

* Multiplies both operands. A * B = 200

/ Divides numerator by de-numerator. B/A=2

% Modulus Operator and remainder of after an integer division. B%A=0

2. Assignment Operators
In C programs, values for the variables are assigned using assignment operators. For example, if
the value “10″ is to be assigned for the variable “sum”, it can be assigned as “sum = 10;” Other
assignment operators in C language are given below.
Operator Description Example

= 10 is assigned to variable sum. sum=10

+= This is same as sum=sum+10. sum+=10

-= This is same as sum = sum-10. sum-=10

*= This is same as sum = sum*10. sum*=10

/= This is same as sum = sum/10. sum/=10

%= This is same as sum = sum%10. sum%=10

&= This is same as sum = sum&10. sum&=10

^= This is same as sum = sum^10. sum^=10

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

3. Relational Operators
The following table shows all the relational operators supported by C. Assume variable A holds 10
and variable B holds 20 then −
Operator Description Example

Checks if the values of two operands are equal or not. If yes, then the (A == B)
== condition becomes true. is not true.
Checks if the values of two operands are equal or not. If the values are not (A != B) is
!= equal, then the condition becomes true. true.
Checks if the value of left operand is greater than the value of right (A > B) is
> operand. If yes, then the condition becomes true. not true.
Checks if the value of left operand is less than the value of right operand. (A < B) is
< If yes, then the condition becomes true. true.
Checks if the value of left operand is greater than or equal to the value of (A >= B)
>= right operand. If yes, then the condition becomes true. is not true.
Checks if the value of left operand is less than or equal to the value of (A <= B)
<= right operand. If yes, then the condition becomes true. is true.

4. Logical Operators
These operators are used to perform logical operations on the given expressions. o There are 3
logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).
Assume variable A holds 1 and variable B holds 0, then −
Operator Description Example

Called Logical AND operator. If both the operands are non-zero, then the (A && B)
&& condition becomes true. is false.
Called Logical OR Operator. If any of the two operands is non-zero, then (A || B) is
|| the condition becomes true. true.
Called Logical NOT Operator. It is used to reverse the logical state of its
operand. If a condition is true, then Logical NOT operator will make it false. !(A &&
!
B) is true.

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

5. Bitwise Operators
These operators are used to perform bit operations. Decimal values are converted into binary values
which are the sequence of bits and bit wise operators work on these bits. o Bit wise operators in C
language are & (bitwise AND), | (bitwise OR), ~ (bitwise OR), ^ (XOR), << (left shift) and >>
(right shift). The truth tables for &, |, and ^ is as follows –
p q p&q p|q p^q

0 0 0 0 0

0 1 0 1 1

1 1 1 1 0

1 0 0 1 1

Assume A = 60 and B = 13 in binary format, they will be as follows −


A = 0011 1100
B = 0000 1101

A&B = 0000 1100


A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60 and
variable 'B' holds 13, then −
Operator Description Example

Binary AND Operator copies a bit to the result if it exists in (A & B) = 12, i.e.,
& both operands. 0000 1100
(A | B) = 61, i.e.,
| Binary OR Operator copies a bit if it exists in either operand. 0011 1101
Binary XOR Operator copies the bit if it is set in one operand (A ^ B) = 49, i.e.,
^ but not both. 0011 0001

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

Binary One's Complement Operator is unary and has the effect (~A ) = ~(60), i.e,.
~ of 'flipping' bits. -0111101
Binary Left Shift Operator. The left operands value is moved A << 2 = 240 i.e.,
<< left by the number of bits specified by the right operand. 1111 0000
Binary Right Shift Operator. The left operands value is moved A >> 2 = 15 i.e.,
>> right by the number of bits specified by the right operand. 0000 1111

6. Conditional Operators or ternary operators


Conditional operators return one value if condition is true and returns another value is condition is
false. This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
Example : (A > 100 ? 0 : 1);

7. Increment/decrement Operators
Increment operators are used to increase the value of the variable by one and decrement
operators are used to decrease the value of the variable by one in C programs.
Syntax:

Increment operator : ++var_name; (or) var_name++;


Decrement operator : --var_name; (or) var_name --; …
Example:
Increment operator : ++i; i++;
Decrement operator: -- i ; i -- ;

Difference between pre/post increment & decrement operators in C:

Operator type Operator Description


Pre increment ++i Value of i is incremented before assigning it to variable i.
Post-increment i++ Value of i is incremented after assigning it to variable i.
Pre decrement – –i Value of i is decremented before assigning it to variable i.
Post_decrement i– – Value of i is decremented after assigning it to variable i.

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

8. Special Operators
Besides the operators discussed above, there are a few other important operators
including sizeof and ? : supported by the C Language.

Operator Description Example

sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.

&a; returns the actual address of the


& Returns the address of a variable. variable.
* Pointer to a variable. *a;

a. Write a C program to perform addition, subtraction, multiplication, division of two


numbers.

ALGORITHM / FLOWCHART (for any one operation):

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

CODE:

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

INPUT & OUTPUT (With Different Test Cases):

Sr. No. INPUT OUTPUT


1.

2.

3.

b. Construct a C program that compares two numbers using various relational operators

ALGORITHM / FLOWCHART (for any one operation):

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

CODE:

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

INPUT & OUTPUT (With Different Test Cases):

Sr. No. INPUT OUTPUT


1.

2.

3.

CONCLUSION: Hence, we understood the concept of operators in C programming language and have
written a c program to demonstrate the working of operators.

REFERENCE:
● https://www.youtube.com/watch?v=d5cLIiTSoTA
● https://www.programiz.com/c-programming/c-operators
● https://www.geeksforgeeks.org/operators-in-c/

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

S. B. JAIN INSTITUTE OF TECHNOLOGY,


MANAGEMENT & RESEARCH, NAGPUR.
Practical No. 2
Aim: Construct a C program to demonstrate the working of decision-making
statements.
a. Construct a C program to find the largest among 3 numbers.
b. Construct a C program to check whether a number is even positive,
odd positive or negative.

Name of Student:
Roll No.:
Semester/Year:
Academic Session:
Date of Performance:
Date of Submission:

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

AIM: Construct a C program to demonstrate the working of decision-making statements.


a. Construct a C program to find the largest among 3 numbers.
b. Construct a C program to check whether a number is even positive, odd positive or negative.

OBJECTIVE/EXPECTED LEARNING OUTCOME:


● To be able to implement one or more conditions to be evaluated or tested by the program.
● To be able to implement a statement or statements to be executed if the condition is
determined to be true, and optionally.
● To be able to implement other statements to be executed if the condition is determined to be
false.

THEORY:
Decision making structures require that the programmer specifies one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the condition
is determined to be false. Show below is the general form of a typical decision making structure
found in the programming languages –
C programming language assumes any non-zero and non-null values as true, and if it is either zero
or null, then it is assumed as false value.
C programming language provides the following types of decision making statements.

Decision-making statements:
1. if statement – An if statement consists of a Boolean expression followed by one or
more statements.
Syntax:- if(condition)
{
True Statement;
}

2. if…..else statement: An if statement can be followed by an optional else statement,


which executes when the Boolean expression is false.
Syntax:- if(condn)

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

{
True Statement;

}
else {

False Statement;

3. nested if statements: You can use one if or else if statement inside another if or else if
statement(s).
Syntax:- if(condn1){
if(condn2){
Statement;
}
else {

Statement;
}
}

4. if…..else if ladder: Here top to bottom statements will be execute.


Syntax:- if(condn1){
Statement1;
}
else if(condn2){
Statement2;
}
else if(condn3){
Statement3;
}

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

5. switch statement: A switch statement allows a variable to be tested for equality


against a list of values.
Syntax:- switch(condn)
{
case expression1
Statement1;
break;
case expression2
Statement2;
break;
case expression3
Statement3;
break;
}
a. Write a c program to find the largest among 3 numbers.

ALGORITHM / FLOWCHART (for any one if statement):

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

CODE:

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

INPUT & OUTPUT (With Different Test Cases):

Sr. No. INPUT OUTPUT


1.

2.

3.

b. Write a c program to check whether a number is even positive, odd positive or negative.

ALGORITHM / FLOWCHART:

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

CODE:

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

INPUT & OUTPUT (With Different Test Cases):

Sr. No. INPUT OUTPUT


1.

2.

3.

CONCLUSION: Hence we have written a c program to demonstrate the working of decision-


making statements.

REFERENCE:
● “Programming in ANSI C” by balaguruswamy 7th edition
● https://www.youtube.com/watch?v=aG7J3m3vlNA
● https://en.wikipedia.org/wiki/Prime_number
● https://www.tutorialspoint.com/cprogramming/c_decision_making.htm

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

S. B. JAIN INSTITUTE OF TECHNOLOGY,


MANAGEMENT & RESEARCH, NAGPUR.
Practical No. 3
Aim: Construct a C program to demonstrate the iterative Programming
method.
a. Construct a C program to check whether a number is prime or not.
b. Construct a C program to print a full pyramid asterisk pattern (*).

Name of Student:
Roll No.:
Semester/Year:
Academic Session:
Date of Performance:
Date of Submission:

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

AIM: Construct a C program to demonstrate the iterative programming method.


a. Construct a C program to check whether a number is prime or not.
b. Construct a C program to print full pyramid asterisk pattern (*).

OBJECTIVE/EXPECTED LEARNING OUTCOME:


● To be able to implement iteration in program.
● To be able to understand the use of looping statement.

THEORY:
CONTROL FLOW STATEMENTS
1. The while Statement
The while statement is a simple way to repeatedly execute one or more statements as long as some
condition is "true".
Syntax:
while (expression) statement;
Firstly, expression is evaluated.
If it is false, statement is not executed and control passes to the next program instruction.
If it is true, statement is executed, then expression re-evaluated to decide if statement should be executed
again.
Example: i=0;
while (numbers[i]!=0)
i++;
In the above examples the statement following the while condition is simply a blank. ie. it does nothing
when it is executed. However, each time the program goes round the loop the variable i is executed as
there is an embedded i++ statement inside the condition that is evaluated.
Note that the statement(s) following the condition in a while loop may never be executed if expression
evaluates to "false" the first time, but if there is a statement embedded in the loop it is executed at least
once while the program decides whether it is true or false.
Once again, braces can be used to form a compound statement if more then one statement is to be
executed, repeatedly
Example: total=i=0;
while (numbers[i]!=0) {

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

total+=numbers[i];
i++;
}

2 The do .. while Statement


This is similar to the Repeat..Until construction in Pascal, in that one or more statements are repeatedly
executed until some expression at the end of the loop evaluates to false (so execution of the statement(s)
takes place at least once). The
Syntax:
do
statement
while (expression);
Example: i=0;
do
letters[i++]=getchar();
while (i<10);
(will read characters from the standard input into the first ten elements of the array letters)
Use of braces around one statement in the do while loop, though not essential, will improve program
clarity, as the while line cannot then be confused with the start of a while statement
Example: do {
letters[i++]=getchar();
} while (i<10);

3. The for Statement


The for statement is a special case of the while statement, particularly useful for processing a number
of contiguous array elements.
Syntax:
for (expression1; expression2; expression3)
statement;
It is useful for expression1 to be an initial assignment to a "control" variable tested in expression2,
which is incremented or decremented in expression3.

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

Example: total=0;
for (i=0; i<10; i++)
total+=numbers[i];
expression1, expression2, expression3 are all optional. If not present expression1 and expression3 are
treated as blank statements and the condition, expression2 is considered to be always true.

4. The break and continue Statements


The break statement already introduced will cause termination of a switch statement. It will also
terminate while, do or for loop, and is equivalent to an unconditional branch to the statement following
the loop.
Example: for(;;) {
ch=getchar();
if (ch=='\n')
break; /* exits the loop */
arr[i++]=ch;
}
In general it is not a good idea to have more than one exit out of a loop. The break statement should,
therefore, only be used in "do forever" loops. The continue statement can also be used within any loop
to cause a break in execution. It causes the particular iteration within the loop to terminate, but not the
loop itself.
Example: while (i<10) {
i++;
if (arr[i]=='\n')
continue; /* Output all on same line */
putchar (arr[i]);
number_output++;
}

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

a. Construct a C program to check whether a number is prime or not.

ALGORITHM / FLOWCHART:

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

CODE:

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

INPUT & OUTPUT (With Different Test Cases):

Sr. No. INPUT OUTPUT


1.

2.

3.

b. Construct aa C program to print a full pyramid asterisk pattern (*)

ALGORITHM / FLOWCHART:

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

CODE:

Department of First Year, S.B.J.I.T.M.R., Nagpur


Programming for Problem Solving Lab (N-ESC102P)

INPUT & OUTPUT (With Different Test Cases):

Sr. No. INPUT OUTPUT


1.

2.

3.

CONCLUSION: Hence, we have written a c program to demonstrate the iteration in given


problem statements.

REFERENCE:
● https://www.studocu.com/in/document/kalinga-institute-of-industrial-
technology/programming-in-c/c-programming-viva-questions/29118795
● https://intellipaat.com/blog/tutorial/c-tutorial/c-loops/
● https://www.youtube.com/watch?v=irqbmMNs2Bo

Department of First Year, S.B.J.I.T.M.R., Nagpur

You might also like