c programming(assignment)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Assignment

1) What are the stages involved while solving a problem using computer? Show the steps in
figure.

Computer based problem solving in a systematic process of designing implementing and using
programming tool during the problem solving stage. This method enables the computer system
to be more intuitive with human logic than machine logic. Final outcome of this process is
software tools which is dedicated to solve the problem under consideration. Software is just a
collection of computer programs and programs are just a set of instructions which guides a
computer’s hardware. These instructions need to be well-specified for solving a problem. After
its creation, the software should be free of error free and well documented. Software
development Is the process of creating such software, which satisfies end user’s requirement.
The following stages should be followed while solving a problem using a computer :-
a) Problem Analysis
b) Algorithm Development
c) Flowcharting
d) Coding
e) Compilation and Execution
f) Debugging and Testing
g) Program Documentation

Problem Analysis

Algorithm Development

Flowcharting

Coding

Compilation and Execution

Debugging and Testing

Program Documentation
2) Write an algorithm to check whether the given number is prime or composite.

A natural numbers said to be prime if it is only divisible by itself and 1. A prime number can be
written as a product of only two numbers. For example: 3. Now 3 can be written in the form of
the product of two numbers in only one way. i.e 1x3. Whereas,
The number that are not prime are called composite numbers. A composite can be written in
the form of the product of two numbers in different ways. For example: 8. 8 is a composite
number which can be written as 1x8 and 2x4.
Following are the algorithm to check the given number is prime or composite:-
Step 1: Take num as input.
Step 2: Initialize a variable temp to 0.
Step 3: Iterate from 2 to the square root of the number.
Step 4: If the num is divisible by loop iterator, then increment loop.
Step 5: If the temp is equal to 0, “NUM IS PRIME” or else, “NUM IS COMPOSITE”
Step 6: Stop.

3) Write an algorithm to find the factorial of a given number.

Product of all consecutive integer numbers upto n is called factorial of a number and is denoted
by n!. For example: the value pf 5! Is 120.
Mathematically, n! = 1x2x3x-------x(n-1)! x n
All positive descending integers are added together to determine the factor of n. Hence, n! is
denoted as a factorial of n.
Following are the algorithm to find the factorial of a given number:-
Step 1: Start.
Step 2: Declare variable n,fact,i.
Step 3: Read number from user.
Step 4: Initialize variable fact = 1 and i = 1.
Step 5: Repeat until i=< number.
a) Fact = factx1
b) i = i+1
step 6: Print fact.
Step 7: Stop.

4) Write an algorithm and draw flowchart to determine whether a given integer is odd or even.

Odd and even numbers are type of integers, which are whole numbers. An even number is any
integer that is divisible by 2, means it has no remainder when divided by 2. For example: 2,4,6,8
and 10 are all even numbers which can be divided by 2. Whereas,
An odd number is any integer that is not divisible by 2 means it has a remainder of 1 when
divided by 2. For example: 1,3,5,7 and 9 are all odd numbers.
The goal of the odd-even program is to determine whether a given number is odd or even. To
find this out, the program first prompts the user to enter an integer using the printf function and
reads the input using the scanf function. The entered number is then stored in an integer
variable. After that, the algorithm determines whether the entered number is odd or even, and
after figuring it out, the algorithm displays the output and stops working.
Following are the algorithm to determine whether the given integer is odd or even :-
Step 1: Start.
Step 2: Declare a variable ‘num’.
Step 3: Read thr value for ‘num’ from users.
Step 4: Check whether num % 2 is equal to zero or not.
Step 5: If num % 2 = 0,display ‘even’ or else
display ‘odd’
Step 6: Stop.

It’s flowchart:

Start

Input value for ‘num’

If num %2=0 display ‘even’

Display ‘odd’

Stop

5) Why flowchart is required? Explain different symbols used in the flowchart.

Flowcharts are visual diagrams that uses symbols to show the steps of a process in order. They
are tools that business users and programmers rely on for various purposes. A programmer can
use flowchart to illustrate dataflow in computer programs. This include how data enters the
program, undergoes changes, and produces an output. A business users can use a flowchart to
evaluate how well a current or new business process works. This helps pave the way for process
improvement.
For programmers, flowcharts serve as a road map for writing code. They allow programmers to
break down a complex problem into smaller, more manageable steps. This helps them to
identify potential issues in the programs logic. By visualizing the data flow, programmers can
easily spot areas that needs improvement or optimization.
Flowchart is required to make it easier for technical users to communicate more complex logic
within a system. In addition, they can act as guides for creating the blueprint for designing a new
program. Many programmers use flowcharts to help them with debugging. The visual flow of
the shapes within a flowchart makes it easier to spot inconsistencies and perform analysis.
Flowcharts are also a great tool to help users maintaining proper documentation standards
while working on a project. It can help business users and developers become more efficient
since they can easily track data flow through a process.
Following shows the different symbols used in the flowchart:-

symbol Symbol name description


Flow lines Used to connect symbols.

Terminal Used to start, pause and halt


in the program logic.
Input/output Represent the information
entering or leaving system.
Processing Represents arithematics and
logical instructions.

decision Represents a decision to be


made.

connector Used to join different flow


lines.

6) What do you understand by the term’s compilation and execution.

Compilation is the process of converting source code written in a high level programming
language into machine-readable code (object code) that can be executed by a computer.
During compilation, the source code is processed by a compiler, which generates the
corresponding object code.
Execution on the other hand, is the process of running or executing the compiled code on a
computer. This involves loading the compiled code into memory, linking it with any necessary
libraries and executing the code on the computer’s processor.
In summary, compilation is the process of converting source code into object code, while
execution is the process of running the object code on a computer.
7) What do you mean by debugging and testing? How these two terms differ in meaning?

Debugging is the process of fixing a bug in a software. It can be defined as identifying, analyzing
and removing errors. This activity begins after the software fails to execute properly and
concludes by solving the problem and successfully testing the software. It is considered to be an
extremely complex and tedious task because errors need to be resolved at all stages of
debugging. Whereas,
Testing is the process of verifying and validating that a software or application is bug-free, meets
the technical requirement as guided by its design and development and meets the user
requirements effectively and efficiently by handling all the exceptional and boundary cases.

These two terms are differ by the following ways:-

Debugging Testing

a) It is the process of correcting bugs found a) It is the process to find bugs and errors.
during testing.
b) It is the process to give absolution to code b) It is the process to identify the failure of
failure. implemented code.
c) Debugging is a deductive process. c) Testing is the display of errors.
d) It is done either by programmer or d) It is done by the tester.
developers.
e) It can’t be done without proper design e) There is no need of design knowledge in
knowledge. the testing process.
f) It is always manual. f) It can be manual or automated.
g) It is based on different types of bugs. g) It is based on different testing levels. i.e,
unit testing, integration testing and system
testing.

8) Why should we document a program? Explain.

Program is a set of instructions that a computer follows in order to perform a particular task. We
should document a program because of the following reasons:-
a) Readability: clear documentation helps other developers (as well as yourself at a later date)
understand code logic, structure and purpose.
b) Maintenance:- well-documented code is easier to maintain and update. It reduces the risk of
introducing errors while making changes.
c) Onboarding new developers:- new developers joining a project can quickly grasp the
architecture and functionalities through documentation. This accelerates the onboarding
process.
d) Debugging:- comments In the code can guide developers during debugging by explaining the
purpose of specific sections or tricky logics.
e) Troubleshooting:- documentation can provide insights into the intended behavior of the
code helping in the identification and resolutions of issues.
f) Compliance and standards:- documentation often includes adherence to coding standards.
This ensures consistencies in coding practices across a project or organization.
g) Enhancing code review:- During code reviews, documentation can be a valuable resources
for reviewers to understand the purpose of changes and the overall architecture.

In summary, documenting a program enhances its maintainability, collaboration, and


longevity. It contributes to a more efficient development process and a smoother
experience for developers working on the code base.

9) Explain the structure of c-program in detail.

The basic structure of a c program is divided into 6 parts which makes it easy to read, modify,
document and understand in a particular format. C program must follow the below-mentioned
outline in order to successfully compile and execute. Debugging is easier in a well-structured c
program.
There are 6 basic sections responsible for the proper execution of a program wich are
mentioned below:-
a) Documentation: This section consists of the description of the program, the name of the
program and the creation data and time of the program. It is specified at the start of the
program in the form of comments.
b) Preprocessor section: All the header files the program will be declared in the preprocessor
section of the program. Headers files help us to access other’s improved code into our code.
A copy of these multiple files is inserted into our program before the process of compilation.
c) Definition: preprocessors are the programs that process our source code before the process
of compilation. There are multiple steps which are involved in the writing and execution of
the program. Preprocessors directives start with ‘#’ symbol. The # symbol define
preprocessor is used to create a constant throughout the program. Whenever this name is
encountered by the compiler, it is replaced by the actual piece of defined code.
d) Global declaration: The global declaration section contains global variables, function
declaration, and static variables. variables and functions which are declared in the scope can
be used anywhere in the program.
e) Sub-program: User-defined functions are called in this section of the program. The control
of the program is shifted to the called function whenever they are called from the main or
outside the main function. These are specified as per the requirements of the programmer.
f) Main ( ) function: Every c program must have a main function. The main ( ) function of the
program is written in this section. Operations like declaration and execution are performed
inside the curly braces of the main program. The return type of the main ( ) function can be
int as well as void too. Void ( ) main tells the compiler that the program will not return any
value. The int ( ) main tells the compiler that the program will return an integer value.
10) Write an algorithm and draw flowchart to find the largest amount three numbers.

Following are the algorithm to find the largest numbers among three numbers:-
a) Start.
b) Input A,B,C
c) If (A>B) and (A>C) then print “A is greater”. Else if (B>A) and (B>C) then print “B is greater”.
Else print “c is greater”.
d) Stop.

Flowchart:-

Start

Input A,B,C

If A>B

If B>C if A>C

Print B print C print A

Stop
11) Write an algorithm and draw a flowchart that prints the sum of first 10 natural numbers.

Following are the algorithm to find the sum of first 10 natural numbers:-
a) Step 1: Start.
b) Step 2: Initialize as variable i to 1.
c) Step 3: Print the value of i.
d) Step 4: Increment i by 1. i.e, i+1.
e) Step 5: Check if the value of I is less than or equal to 10. If k<= 10, then go to step 2
otherwise go to step 6.
f) Step 6: stop.

Flowchart:-

Start

Create variable num = 1

Is num <= 10 Stop

Print num

Num = num+1

You might also like