Sei sulla pagina 1di 29

PROGRAMMING IN C

ASSIGNMENT 1

NAME:- Ashish Mehra


UID :- 18BCS3796
BRANCH:- CSE-IBM
SECTION:- BD-1
SUBMITTED TO:- Mrs. Pooja
Logics Assignment :

Q1. Draw flowcharts and algorithm of the following.

I. Take 2 numbers from user and perform arithmetic operators +, -, *, /, % and


display good o/p messages.
1. Start
2. Take Input a, b
3. Apply the arithmetic operations START
on the numbers as sum= a + b, sub=a-b,
mul=a*b, div=a/b, mod=a % b.

4. Show results
Take input a,b
5. Stop

Sum=a+b , Sub=a-b,

mul=a*b , div=a/b,

mod=a%b

Show results

Stop
II. Calculate average of 5 numbers entered from user, display the average.
1. Start
2. Input a, b, c, d, e
START
3. Calculate average by adding all numbers and by
dividing it by 5 i.e. a + b + c + d+ e /5 and taking Its
average.

4.Display the average.

5.Stop.
Input a,b,c,d,e

average=(a+b+c+d+e
)/5

Print average

Stop
III. Print Hello 5 times.
1. Start
2. Take m =1
3. Apply the If statement given below:-int m=1;

If(m<=5)
printf(“Hello\n”);

m++;

4. Execute the loop until m exceeds 5

5. Stop
IV. Perform addition on digits of a number , e.g.423=4+2+3=9 should be
returned.

1.Step

2. Take a, b, c

3. The number taken should be a 3 digit no.


Start
4. Executing following statements:-

num%100=modulus1;

modulus1=a; num%100=modulus1

num/10; modulus1=a

num%10=modulus2; num/10

modulus2=b; num%10=modulus2

modulus2=b
num/100;

num=c;

5. Display the sum a+ b+ c num/100

6. Stop num=c

a+b+c

Stop
V. Reverse a program number e.g.423 should be reversed to 324.

1 . Start
Start
2. Take input a

3.

num=a%10 Input a
rev=rev*10+num

num/=10; False
a>=0 Print num=-
4. Check num>0
num
5. Go to step 3.
6. Print rev.
7. Stop.

num=a%10

rev=rev*10+num

num/=10

Print rev

Stop
VI. Check if no. is palindrome(If number and the reversed number is same)

1. Start

2. Take a, reverse=0,reminder=0 Start


3. Execute following statements to check palindrome

reverse=a%10;

a/=10; Input a, rev

reminder=reminder*10+reverse;

reverse=reminder; If a==rev

Check a>0

Go to step 3;

4. If a is equal to reverse,
print Number is palindrome

else number is not palindrome


Print palindrome
5. Stop.

Print number is not


palindrome

Stop
VII. Factorial of a number entered by user.
 1. Start Start
2. Take input n

3. (Initialise) i=1,fact=1
Input n
4. Repeat step 4 through 6 untill i=n

5. fact=fact*i

6. i=i+1 i=1,fact=
7. Print fact 1
8. Stop

Is FALSE
i<=n?

True

I=i+1

Print fact

fact=fact*i

Stop
VIII. GCD of 2 numbers entered by user.

1. Start

2. Take input 2 numbers a,b Start


3. Take a=d,b=n

4. r=a%b
Input a,b
5. While r!=0,r=n%d

6. GCD =d

7. Display GCD.
a=d,b=n
8. Stop

r=a%b

Invalid
False number
r!=0

r=n%d

d=GCD

Stop
IX. Check if a number is Prime or not.
Start
1. Start

2. Take p as input prime number.

3. Take n=2
Input p
4. Divide p by n i.e. p%n and take it r.

5. If r=0, p is not prime , goto step 9.

6. else set n=n+1 n=2


7. If n>=p, p is prime no.

8. Display p is prime.

9. Stop p%n== True p is not


0 prime

False

n= n+1

Stop
X. Check if a number entered is even or odd.

1. Start
Start
2. Take input as num

3. Execute : if num%2==0,then print

Even number Input num

else print odd number.

4. Stop.
num%2
=0 False Odd number

True

Even
Number

Stop
Q2. What are the characteristics of a good Algorithm along with basic definition of
Algorithm and Flowcharts.

Algorithm:- An algorithm is a logical step by step method to solve the

problems given without using and programming syntax.

Flowchart:- A flowchart is a graphical representation of an algorithm with the

help of different symbols and shapes.

Characteristics of a good algorithm:-

 Input and output should be defined precisely.


 Each step should be clear and unambiguous.
 Algorithmic step should be problem solving.

Q3.What are the basic symbols used in flowchart.

Ans. Start/Stop

Input/Output

Processing(operations like +,-,*,/ )

Decision(used to give condition)

It is used as a connector
General Topics :

Q1. What is a computer and state advantage of a computer ?

Ans. An electronic device which is capable of receiving information (data) in a


particular form and of performing a sequence of operation in accordance with a
predetermined but variable set of procedural instruction (program) to produce a result
in the form of information or signals.

One of the overwhelming advantage of the computer lies in its very necessity.

 It enhances communication and availability of information.


 It allows for more convenient resource sharing.
 It is highly flexible and makes file sharing easier.
 It increase cost efficiency and boosts storage capacity.
 It increases our productivity and saves our time as time efficient.
 If connected to internet, its ability to obtain resource would be another attribute.

Q2. Explain the areas where C is still used and preferred over other languages like
Java, Python, HTML, C++.

Ans. C is a middle level programming language developed at AT&T Bell Labs in


U.S.A by Dennis Ritchie. The objective of its development was in the context of the
re-design of the UNIX operating system to enable it to be used on multiple computers.
The C language has formed the basis for many languages including C++, Java,
JavaScript, Go, Rust, C#, PHP, Python, Perl, Verilog and C-Shell.

 Cis a structural programming language which allows a complex program to be


broken into simpler programs called function. It also allows free movement of
data across these function.
 C language is case sensitive & is highly portable and is used for scripting
system application which form a major part of Windows, UNIX and Linux
operating system.
 C can be used for low-level programming such as kernel and drivers.
 C is a general purpose programming language and can efficiently work on
enterprise applications, games, graphics and applications requiring
calculation.
 C language has a rich library which provides a number of built-in function.
 It also offers dynamic memory allocation.
Major areas where C is used are:

 Embedded System
 Open Source Software
 System Programming
 Controlling System
 Automotive System Controllers
 Network Hacking
 Writing/Designing Compilers

Q3. What are the different types of computing devices which you see in day-to-day
world ?

Ans. A machine for performing calculations automatically. It generally refers to


general-purpose device that can accept software for many purpose in contrast with a
dedicated unit of equipment such as network switch or router.

 Desktop- Quite large and not very portable.


 Laptop- Lighter than desktop computers and more portable.
 Notebooks- Smaller and lighter than laptops.
 Tablet- Usually smaller than laptops and notebooks and contains no physical
keyboard and mouse
 Smartphones- Usually contains a mobile telephone which is used for voice
conversation, etc.

Q4. Brief overview of features of C, along with advantages and disadvantages of C.

Ans. Feature of C:

 It is a robust language with rich set of built-in functions and operations that can
be used to write any complex program.
 The C compiler combines the capabilities of an assembly language with
features of a high-level language.
 It is mainly faster than BASIC.
 Program written in C is generally fast and efficient. This is due to variety of data
type and powerful operators.
 C is highly portable this means that programs once written can be run on
another machine little or no modification.
 C program is basically a collection of function that are supported in C library.
We can also create our own function and add it to C library.
 C language is the most widely used language in operating system and
embedded system and it has ability to extend itself.

Advantages of C language :

 C is highly portable language.


 C language is a building block for many other currently known languages. C has
variety of data types and powerful operators, due to this program in C language
are efficient, fast and easy to understand.
 There are only 32 keywords in ANSI C and its strength lies in built-in function.
 C language is a structured programming language.

Disadvantages of C language :

 C doesn’t have concept of OPPs, that’s why C++ is developed.


 There is no runtime checking in C language.
 There is no strict type checking.
 C doesn’t have the concept of namespace.
 C doesn’t have the concept of constructor or destructor.

Q5. What are the advantages of modularization of code ?

Ans. To avoid poor-planning Scenarios, computer scientists began to organize code


based on the task it executes. In this way code becomes reusable and easier to debug
and manage.

Advantage of Modularization of code:

 Easier to debug
 Reusable Code
 Readability
 Reliability
 Team Programming
 Manageability

Q6. What are programming languages and why we need them ?

Ans. A programming language is a notation for writing programs, which are


specifications of a computation or algorithm. Some but not all restrict the term
‘programming language’ to those language that can express all possible algorithms.
Traits often consider important for what constitutes a programming language
including:

 Function and target.


 Abstractions
 Expressive power

The main function of the lesson is to expose the need of programming language.
When we formative language or commands that describe actions we are making a
kind of code. This is also necessary for computers, which are simply machines that
can perform a no. of different tasks.

 Access the clarity of a set of instructions expressed in human language.


 Creates a set of instruction in human language for LEGO block arrangement.
 Identify connections between the ability to program and the ability to solve
problems.

Q7. What are high level and low level languages, advantages and disadvantages of
both languages ?

Ans. High level language provides higher level of abstraction from machine
language. They do not interact directly with the hardware. Rather, they focus more
on the complex arithmetic operations, optimal program efficiency and easiness in
coding.

Low level language programming uses machine friendly language. Programmers


writes code either in binary or assembly language. Writing programs in binary is
complex and cumbersome process. Hence, to make programming more
programmers friendly. Programs in high level language is written using English
statements.

Advantages of high-level and low-level language:

 High level languages are programmer friendly. They are easy to write, debug
and maintain.
 It provide higher level of abstraction from machine languages.
 It is machine independent language.
 Easy to learn.
 Less error prone, easy to find and debug errors.

Disadvantages of high-level and low-level language:

 It takes additional translation times to translate the source to machine code.


 High level programs are comparatively slower than low level programs.
 Compared to low level programs, they are generally less memory efficient.
 Cannot communicate directly with the hardware.

Q8. Difference between compiler, interpreter, assembler and why do we need them
?

Ans. Compiler : It's a computer programs that transforms source code written in a
programming language into machine language that is the target language which
usually has a binary form known as object code.

Interpreter : It translates high level instructions into an intermediate form, it


translates the code into the intermediate form line by line an caries out specific
actions.

Assembler : It is a program that takes basic computer instructions and converts then
into a pattern of bits that the computer's processor can use to perform it's basic
operations. The language used to program the assembler is called assembly
language.
Errors are displayed after entire program is checked in case of compiler. Hence
debugging is comparatively hard. In case of an interpreter, Errors are displayed for
every instruction interpreted (if any). An interpreter continues translating the
program until the first error is met, in which case it stops. Hence debugging is easy.

Q9. What are stand-alone application and web-based application, exemplify and
specify which languages are use to develop those applications ?

Ans. Stand-alone applications: Software that is not a part of some bundled software.
A program that run as a separate computer process, not an add-on of an existing
process. Standalone program, a program that does not require operating system's
services to run. A portable application, which can be run without the need for
installation procedure.
Examples: MS-Office, Picasa, WinRAR, calculator, adobe, Antivirus.

Web based applications: A web-based application is any program that is accessed


over a network connection using HTTP, rather than existing within a device’s
memory. Web-based applications often run inside a web browser. However, web-
based applications also may be client-based, where a small part of the program is
downloaded to a user’s desktop, but processing is done over the internet on an
external server. Web-based applications are also known as web apps.
Examples: chrome, Firefox, edge, google drive, YouTube, E-mail.

Q10. What is memory and different types (ROM, RAM, cache, secondary
storage(hard disk, u.s.b). Explain w.r.t memory cost and time to access.
Ans. Memory is a storage area in computer system. The term memory identifies
data storage that comes in the form of chips and the word storage is use for memory
that exists on tapes or disks.

Different types of Storages and memory:-

 RAM:- Random access memory is also called as Read write memory. It is a


form of computer data storage that stores data and machine code currently
being used.

 ROM:- Read only memory is a type of non-volatile memory used in computers


and other electronic devices. Data stored in ROM can only be modified slowly
with difficulty so, it is mainly used to store application, software.

 Cache:- In computing, a cache is a hardware or software component that


stores data so that future requests for that data stored in cache might be the
result of an earlier computation or a copy of data stored elsewhere.

 Hard disk:- The hard disk or a hard drive , is a spindle of magnetic discs that
can hold several gigabytes of data. Therefore, disk space refers to how much
space is available on your hard disk for storing files.

 USB:- It is a data storage device that includes flash memory with an integrated
USB interface. It is removeable and rewriteable and much smaller than a
DVD Or CD.

Q11. What is bit, byte, binary number system and decimal number system. why
computers implement binary and not any other number system like octa, hex,
decimal) ?

Ans. A bit is a single binary digit that can represent 0 or 1.

A byte is 8 binary digits working together to represent a number that can take a
value between 0 and 255 in the decimal system.

Decimal is a term that describes the base-10 number system, probably the most
commonly used number system. The decimal number system consists of ten
single- digit numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

There are two reasons computers use the binary system:


 Two clearly distinct states that provide a safety range for reliability.
 Least amount of necessary circuitry, which results in the least amount of
space, energy consumption, and cost.

Q12. Convert 120 base 10 to base 2.


Ans. (120)10=(01111000)2

Q13. What is portability and why is code written in low level language is not
portable(high level program is) w.r.t CPU architecture. Please explain using a
sample 20 bit CPU architecture.
Ans. Portability : Portability is a characteristic attributed to a computer program if it
can be used in an operating systems other than the one in which it was created
without requiring major rework. Porting is the task of doing any work necessary to
make the computer program run in the new environment.
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. The word "low"
refers to the small or nonexistent amount of abstraction between the language and
machine language; because of this, low-level languages are sometimes described
as being "close to the hardware". Programs written in low-level languages tend to be
relatively non-portable.
Low-level languages can convert to machine code without a compiler or
interpreter second-generation programming languages use a simpler processor
called an assembler— and the resulting code runs directly on the processor. A
program written in a low-level language can be made to run very quickly, with a
small memory footprint. An equivalent program in a high-level language can be less
efficient and use more memory. Low-level languages are simple, but considered
difficult to use, due to numerous technical details that the programmer must
remember. By comparison, a high-level programming language isolates execution
semantics of a computer architecture from the specification of the program, which
simplifies development.Programming exercises(in c):
1.Assign an integer value to a character variable and then display in %d
and %d format.
Output
2.Using sizeof( ) display size if all basic type of data types.

Output
Iterations:-
1.

2.

Overall(Unit 1):
1. Write a program that reads two no. from keyboard and gives addition,
subtraction, multiplication, division.
2.Program to Print Largest of Three No
3.Program for increment and decrement operators.

4.Program to convert
distance into cm, inches, m, feet entered by user in km.
5.Program for exponential series ex.
6. Write a program to implement Pascal Triangle.
7. Write a program to find sum of all integers greater than 100 & less than 200
and are divisible by 5.

Potrebbero piacerti anche