16

Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek
Page 2: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

Fundamentals of Programming

Languages - I

First Year Engineering Common to All Branches As per the new revised syllabus of SSPU. w.e.f. academic year 2016-2017

Dr. Parikshit N. Mahalle

LMISTE, Ph.D., Member ACM,

Member IEEE, M.E. (Comp. Engg.)

Professor, HOD

Department of Computer Engineering

Smt. KashibaiNavale College of Engg.

Vadgaon (BK), Pune.

Prof. Vivek V. Jog

Ph.D.(Pursuing), M.E.(IT)

Assistant Professor

Department of Computer Engineering Smt. KashibaiNavale College of Engg.

Vadagaon (Bk, Pune

Gigatech Publishing House Igniting Minds

Page 3: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

Fundamentals of Programming Languages-I

First Year Engineering Common to All Branches

As per new revised syllabus of SPPU w.e.f. academic year 2016-2017

Dr. Parikshit N. Mahalle, Prof. Vivek V. Jog

First Edition : July 2017

Published By –

Gigatech Publishing House

631/32, Budhawar Peth,

Office No. 105, First Floor, Shan Bramha Complex, Pune – 411 002 .

Phone No. 952 952 0952

Copyright Gigatech Publishing House

All rights reserved. No part of this publication can be stored in any retrieval system or

reproduced in and form or by any means without prior permission of the Publisher.

LIMITS OF LIABILITY AND DISCLAIMER OF WARRANTY

The Authors and Publisher of this book have tried their best to ensure that the

program, procedure and function described in the book are correct . However the

author and publisher make no warranty with regard to the program and documentation

contained in the book.

ISBN NO. : 978-81-9341-402-6

Price : < 195/-

Available at All Leading Book Stalls

Distributor :

PRADEEP BOOK DISTRIBUTORS

102/3, 1ST Floor, Shan Bramha Complex,

Pune. Telephone No. 020 – 24458333,

942 208 7031 / 844 699 5959

E-mail : [email protected]

Note: For any queries, doubts, suggestions & complaints regarding the subject, please feel

free to contact on the below e-mail or phone number. We will be more than thankful for your

feedback.

Ph: 952 952 0952 E-mail : [email protected]

Page 4: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

Preface

Dear students,

We are extremely happy to present book on "Fundamentals of Programming

Languages - I" for you. We have divided the subject into small chapters so that the topics

can be arranged and understood properly. The topics within the chapters have been

arranged in a proper sequence to ensure smooth flow of the subject.

We are thankful to Mr. Harshal J Potdar and Mr. Rohit K Dongare for the

encouragement and support that they have extended. We are also thankful to the staff

members of Gigatech Publishing House (GPH) and others for their efforts to make this

book as good as it is. We have made every possible effort’s to eliminate all the errors in

this book. However if you find any, please let us know, because that will help us to

improve further.

We are also thankful to our family members and friends, for patience and

encouragement.

Authors

Page 5: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

Gigatech Publishing House Igniting Minds

SYLLABUS

Unit 1 : Introduction to computer

Block diagram of Computer, hardware, software. Introduction to System Software-

Operating System, Editor, Complier, Assembler, Linker, Loader. Problem solving using

computers, Introduction to computer programming, Introduction to program planning

tools- algorithm, flowcharts, pseudo codes, Software Development Life Cycle, Introduction

to open source operating systems and programming languages, Introduction to program

development environments: BOSS and GCC

Unit 2 : Programming language ‘C’

Types of programming languages-machine-level, assembly, high level languages. Features

of C, basic concepts, structure of a C program, declarations, constants, variables, data

types, Operators and expressions, conditional expressions. Operators-assignment,

arithmetic, relational, logical, increment and decrement, precedence and associativity of

operators, type conversions, Input and Output functions- scanf and printf;

Unit 3 : Decision Control Structures in ‘C’

if, if-else, nested if-else, cascaded if-else and switch statement, loop control structures: for,

while, do-while loops, break and continue. Pointers in 'C': Concept, address operators,

pointer variable declaration, pointer assignment, pointer initialization.

Unit 4 : Array, Functions and Strings in C

Arrays in 'C' : Concept, declaration, initialization, assessing elements, operations,

multidimensional array.

Functions in 'C' : definition, function call, call by value and call by reference, return

statement, standard library functions and user defined functions, passing array as function

parameter.

Strings in 'C' : Concept, declaration, initialization and string manipulation functions,

library functions

Laboratory Assignments

Group A

1. Write a C program to compute the roots of given quadratic equation for non-zero

coefficients.

2. Write a C program to calculate the sum of all positive even numbers and the sum

of all negative odd numbers from the set of input numbers

3. A customer is offered 15% discount on the printed price of a laptop. The customer

has to pay 7% sales tax on the remaining amount. Write a program in C to

calculate the amount to be paid by the customer.

4. Write a C program to accept the length of three sides of a triangle and to test and

print the type of triangle as equilateral, isosceles or right angled or none

Page 6: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

F.E. (Fundamentals of Programming Languages-I) 2 Contents

Gigatech Publishing House Igniting Minds

5. Write a C program to accept cost price and sales price of an item and calculate the

profit percentage or loss percentage.

6. Write a C program to find out if the number is a Pythagoras triplet. Ex a2 = b

2 c

2

7. Write a C program to implement Euclid's algorithm to find the GCD and LCM of

the given two integers.

8. Write a C program to check whether input number is Prime or not.

9. Write a C program to check if (any one)

A) The number is perfect number or not. Hint: If sum of the factors (including

1 and excluding the number itself) is same as that of original number then

the number is said to be perfect number.

B) The number is 'neon' number or not. Hint: If the sum of the digits of

square of the number is the number itself, then the number is said to be

neon. For example- 9, Square is 81 and 8 + 1 = 9.

C) The number is `Armstrong' number or not. Hint: If sum of cubes of digits is

equal to original number then the number is said to be Armstrong.

For example- Number 153 is Armstrong number (1)3 + (5)

3 + (3)

3 = 153.

10. Write a C program to separate digits of input 4 digit integer, separate & display its

digits.

11. Write a C program to generate first 20 Fibonacci numbers.

Write a C program to check if the entered 3 digit number is twisted prime or not.

Hint : If the number obtained after reversing the number is also a prime then the

number is twisted prime. Example 167, twisted 761, so 167 is twisted prime.

12. Write a C program to calculate the sum of all numbers from 1 to 100 that are

divisible by 4

Group B

14. Write a C program to store the N data samples in an array and calculate mean,

mode and median.

15. Write a C program to store N numbers in an array and display only those numbers

that are perfect squares.

16. Write a C program to store N numbers in an array and search particular number.

17. Write a C program to store N numbers in an array and display the square of each

number in the array

18. Write a C program to sort the roll numbers of present students. Mark the

attendance of the present students roll numbers randomly in an array.

19. Write a C program to store N numbers in an array and reverse the elements of the

array.

20. Write a C program to store Chemistry subject test marks of N students in an array

and find the Minimum and Maximum score. Test maximum marks= 20. Your

program should accept marks ranging between 0 to 20 only.

Page 7: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

F.E. (Fundamentals of Programming Languages-I) 3 Contents

Gigatech Publishing House Igniting Minds

21. Write a C program to store N numbers in an array and compute the sum of all

even and odd numbers in an array.

Group C

22. Write a C program to accept a string and to display the following:

A) Total number of characters in the string

B) Total number of vowels in the string

C) Total number of occurrence of particular character in the string

23. Write a C program to accept a string and change the case of each character of the

string.

Example "THIs Is a C Program" changes to "thiS iS A c pROGRAM"

24. Write a C program to accept a string and print the string in alphabetical order.

Example COMPUTER will be CEMOPRTU

25. Write a C program to accept a string with complete name of the employee and

display the string eliminating the middle word. Example "Mayur Kumar Kulkarni"

would be displayed as "Mayur Kulkarni"

26. Write a C program that will check whether given string is palindrome or not.

27. Write a C program that will check whether one string is substring of other string or

not.

28. Write a C program to enter a string. Frame a word by joining all the first characters

of the words in the string. Example- input "Just Allow Me" will give output as

"JAM"

29. Write a C program to accept a string and replace all the vowels in the string with *.

Group D

30. A class teacher wants to keep record of 10 students in the class along with the

names and marks obtained in 5 subjects. Write a C program with function that

displays

A) Name of the student with highest marks in a particular subject

B) Overall percentage result of the class

C) Total number of passing students in the class

D) Total number of students failing in one subject

E) Total number of distinctions in the class

31. Write a C program to carry out following operations on strings using library

functions

A) To concatenate a string S2 to string S1

B) To find the length of a given string

C) To compare two strings S1 and S2

D) To copy a string S2 to another string S1

Page 8: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

F.E. (Fundamentals of Programming Languages-I) 4 Contents

Gigatech Publishing House Igniting Minds

32. Write a C program to compute the factorial of the given positive integer using

function

33. Write a C program with functions to compute addition and subtraction of two

matrices

34. Write a C program that stores 12 city names in a single dimensional array. Write

function to display only those words that begin with a consonant and end with a

vowel example - Begaluru, Mumbai

35. A mall has 5 stores and 3 departments. Write a C program with function to find the

monthly sale of a particular store or department and the total monthly sale of each

store and each department.

36. Write a C program that declares and initializes a double, an int and a char variable

and a pointer to each of the three variables and one double pointer to store

pointer address. Your program should print the address of, and value stored in,

and the memory size (in bytes) of each of seven variables.

37. Write a C program with function to swap values of two elements (call by reference)

Page 9: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

F.E. (Fundamentals of Programming Languages-I) 5 Contents

Gigatech Publishing House Igniting Minds

CONTENTS

1. Introduction to computer 1.1 – 1.40

1.1 Introduction and Block Diagram of computer

1.1.1 Hardware

1.1.2 Software

1.2 Introduction to System Software

1.2.1 Operating System Software

1.2.2 Objectives of an Operating System

1.2.2.1 Main Objective of an Operating System Program

1.2.2.2 Functions of Operating System

1.2.3 Utility Software

1.2.4 Library Software / programs

1.2.5 Translator Software

1.2.5.1 Editor

1.2.5.2 Compiler

1.2.5.3 Assembler

1.2.5.4 Interpreter

1.3 Software Utilization : A Complete Picture

1.3.1 Linker

1.3.2 Loader

1.4 Problem Solving using Computers

1.5 Types of software and Their Licensing

1.5.1 Open Source

1.5.2 Freeware

1.5.3 Trial Versions / Sharewares

1.5.4 Proprietary

1.5.4.1 Single User License

1.5.4.2 Multi User License

1.5.5 Organizational/ Campus based

1.6 Introduction to Computer Programming

1.6.1 What is the need for planning a program before Coding ?

1.7 Introduction to Program planning Tool

1.7.1 Algorithm

1.7.1.1 Advantages of Generalized Algorithms

1.7.1.2 Steps to write Generalized Algorithm

1.7.1.3 Phases of Algorithm

1.7.1.4 An Example How to Create An Algorithm ?

1.7.1.5 Avoiding Infinite Loops in Algorithms

1.7.2 Flowchart

1.7.3 Pseudo Code : Its Necessity

Page 10: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

F.E. (Fundamentals of Programming Languages-I) 6 Contents

Gigatech Publishing House Igniting Minds

1.7.3.1 Writing A Pseudo Code : Some Do‘s and Don’ts

1.8 soft ware Development Life Cycle

1.9 Introduction to Open Source Operating Systems and Programming Languages

1.9.1 Open source operating System

1.9.2 Introduction to programming languages

1.9.2.1 Evolution of Programming Languages

1.9.3 Paradigm based Programming Language Classification

1.9.3.1 Procedural Programming Languages

1.9.3.2 Structured Programming Languages

1.9.3.3 Object-oriented Programming Languages

1.9.4 command scripting Language

1.9.5 Application Scripting Language

1.9.5.1 Markup Language

1.9.5.2 Universal Scripting Language

1.9.5.3 Advantages of Scripting Language

1.9.6 Features of Good Programming Language

1.10 Introduction to Program Development Environment

1.10.1 BOSS : stands for “BesIII Offline Software System “

1.10.2 GCC

2. Programming language ‘C’ 2.1 – 2.31

2.1 Introduction – facts and Features of C

2.1.1 Facts about C

2.1.2 Why to use C ?

2.1.3 Features of C Language

2.1.4 C program File

2.1.5 C Compilers

2.2 Introduction to Programming Language

2.3 Structure of C program

2.4 Constants

2.5 C Tokens, Keywords, Introduction and comments

2.5.1 C Tokens

2.5.2 C Keywords

2.5.3 Instructions

2.5.4 Comments

2.6 Identifiers

2.7 Variables

2.7.1 Types of Variables

2.8 Declarations

2.9 Storage Class

2.10 Data Types

2.10.1 Integer Type Data and Its Categories

2.10.2 Character Type Data and Its Categories

Page 11: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

F.E. (Fundamentals of Programming Languages-I) 7 Contents

Gigatech Publishing House Igniting Minds

2.10.3 Floating – Point Type Data and Its Categories

2.11 Operators and Expressions

2.11.1 Conditional Expressions

2.11.2 Arithmetic Operators

2.11.3 Increment and Decrement Arithmetic Operators

2.11.4 Relational Operators

2.11.5 Logical Operators

2.11.6 Bitwise Operators

2.11.6.1 Truth Table and Example of Bitwise Operations

2.11.7 Assignment Operators

2.12 Operator Precedence and Associativity

2.13 Type Conversions / Type Casting

2.14 Input and output Functions

2.14.1 printf () Functions

2.14.2 scanf () Functions

2.14.3 Unformatted Input/output

3. Decision Control Structures in ‘C’ 3.1 – 3.24

3.1 Introduction to Decision Control Structures in C

3.2 if

3.3 if - else

3.4 Nested if - else

3.5 Cascaded If – else – in Case of Multipath Decisions

3.6 Switch – case

3.7 Loop Control Structure

3.7.1 while loop

3.7.2 for loop

3.7.2.1 Nested for loop

3.7.3 do – while loop

3.7.4 Break Statement

3.7.5 Continue statement

3.7.6 goto Statement

3.7.7 Examples on Loop Control Structure

3.8 Pointer Concept

4. Array, Functions and Strings in C 4.1 – 4.29

4.1 Array in C

4.1.1 Concept

4.1.2 Classification of Array

4.2 Single Dimensional Array

4.2.1 Declaration and Initiation of Integer Type Array

4.2.1.1 Array Initiation and Access

4.2.2 Declaration and Initiation and Access of Character Type Array

Page 12: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

F.E. (Fundamentals of Programming Languages-I) 8 Contents

Gigatech Publishing House Igniting Minds

4.2.3 Declaration and Initiation and Access to Float Type Array

4.3 Operations on Multidimensional Array (Two Dimensional Arrays)

4.4 String Concepts, Declaration and Initialization

4.4.1 Read or Accept string

4.4.2 Print or Display string : Accessing Elements

4.4.3 Library Functions and Their Purpose

4.4.4 String Manipulations Functions

4.5 Function

4.5.1 What is C Function. ?

4.5.2 Uses of C Functions

4.5.3 Function Declaration, Function Call and Function Definition

4.5.4 Simple Example Program for C Function

4.6 Function Call

4.6.1 Call by Value

4.6.2 Call by Reference

4.7 Function Categories based on Use of Arguments and Return Value

4.7.1 A Function Without Parameters and Without Return Type

4.7.2 A Function Without Parameters and With Return Type

4.7.3 A Function With Parameters and Without Return Type

4.7.4 A Function With Parameters and With Return Type

4.7.5 Few Facts About Return Statements of C Functions

4.8 Functions Types in C

4.8.1 Standard Library Function

4.8.2 User Defined Function

4.8.2.1 Example for User Defined Function

4.9 Steps for Adding our Own Functions in C Library (Considering Turbo C Compiler)

4.9.4 Example of Library Function

4.10 Passing Array as Function Parameters

4.10.1 Only Specific Data from Specific Location

4.10.2 Passing Whole Array

4.10.3 Passing Two Dimensional Array

4.11 Array and Pointers

4.12 Structure

4.13 Union

MCQ’S 1 – 103

LABORATORY ASSIGNMENT 1 – 42

Page 13: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

Gigatech Publishing House Igniting Minds

1.1 Introduction and Block diagram of Computer

Computer of the day is nothing but an electronic device having very fast calculating

ability.

Definition 1 : A computer is an electronic machine that accepts data, stores and

processes data into information. The computer is able to work because there are

instructions in its memory directing it.

: Syllabus :

Introduction to Open Source Operating Systems and Programming Languages,

Introduction to Bharat Operating System (Boss ) GNU/Linux users model GUI,

System Folders, study Commands (Using command terminal) with switches; Is

Directory Commands, Change user, Privileges, passwords, Uy, who, config,

make, rpm, yum, sudo, Sutodown.

Eclipse Editor, Compiler, Linker, Libraries, (GUI, Configuring Programming

Environments: C,C++, Java, Python(Pydev), Output, Debug windows

Introduction to types of Programming Languages – Machine-level, Assembly

level and High level Languages, Scripting Languages, Natural Languages; Their

relative Advantages and Limitations. Characteristics of Good Programming

Languages; Selection a Language out of many available languages for coding

an application ; subprograms.

Short Introduction to LISP, Simulation Platforms: MATLAB and GNU Octagve

(Open Source), Importance of Documentation. Documentation Platform LATEX

(Free ware/Open Source).

Unit

1 Introduction to Computer

Page 14: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

F. E. (Fundamentals of Programming Languages-I) 1.2 Introduction to Computer

Gigatech Publishing House Igniting Minds

Definition 2 : A computer is a general purpose device that can be programmed to carry

out a set of arithmetic or logical operations automatically. Since a sequence of operations

can be readily changed, the computer can solve more than one kind of problem.

It accepts request from user through input unit in digital form, process the request

as per the request made by user and finally return back result produced to external world

through output device.

Processing of request is done following set of instructions that are generally

prepared and stored in internal memory at par with required business logic. This script is

known as a “Program”.

A typical computer system consists of (i) Hardware (ii) Software and (iii) Data.

1.1.1 Hardware

All physical components that belong to all sections of the system block diagram as

given in Fig.1.1.1 can be said to be “Hardware” of the computer. As seen in the block

diagram, computer hardware comprises of

(i) Input unit (ii) Output unit

(iii) Central Processing Unit (CPU) (iv) Memory unit

Fig. 1.1.1 : Block diagram of a typical computer

Page 15: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

F. E. (Fundamentals of Programming Languages-I) 1.3 Introduction to Computer

Gigatech Publishing House Igniting Minds

1. Input unit : To get required work done from computer, user of the system need to

command the system to do so. This is possible with the help of hardware that is

used in input section of the computer.

Input section hardware i.e. an input device is used :

i) To receive command from external world to perform required task

ii) To provide data on which operations are to be performed.

iii) To write set of instructions together called as a “program” those actually

define how to process users request to produce required results.

2. Output Unit : Task that needs to be accomplished is submitted to computer through

input devices. These tasks are executed by computer and appropriate results are

generated. Now it becomes important to produce these results to the requesting

user back. Output unit devices are thus used to communicate or disclose or display

these results to the external world back.

3. Central processing unit : This is actual processing part of the system. It further

consists of

i) Control unit

ii) Arithmetic and Logic unit

iii) Internal memory.

i) Control unit : This unit plays role of a manager. It manages execution of user

requests along with coordination with other functional units.

ii) Arithmetic and Logic unit : This unit looks after all mathematical and logical

computations. It received data either from input unit or from stored memory,

performs all operations based on the instructions provided in program or

through user command and generates appropriate result. These results are

than displayed to user back through output device.

iii) Internal memory : It is an immediate memory of the CPU. It is smaller, faster

and volatile form of memory. It is used to hold data, programs as well as

intermediate results that get generated during normal execution of program. It

thus saves the memory overheads in the form of read and write operations to

and from secondary memory and hence is a key contributor in speeding up

computer operations.

Registers : These are smallest, fastest and closest memory available to

processor for computations

Secondary Memory : This is not a part of computer block diagram but we felt that

it is better to explain this external part of system since whole computer works of

stored program concept. It means that any program that is to be executed in

system has to be brought in RAM for execution and its source by default is

Page 16: Fundamentals of Programming · First Year Engineering Common to All Branches As per new revised syllabus of SPPU w.e.f. academic year 2016-2017 Dr. Parikshit N. Mahalle, Prof. Vivek

Fundamentals of ProgrammingLanguages-I (Common for all branches)

Publisher : Gigatech PublishingHouse

ISBN : 9788193414026Author : Parikshit N MahalleAnd Vivek V Jog

Type the URL : http://www.kopykitab.com/product/11941

Get this eBook