c program float charchter integer, Thesis of Mathematics

c program float charchter integer

Typology: Thesis

2017/2018

Uploaded on 02/08/2018

sharad-ojha
sharad-ojha 🇮🇳

1 document

1 / 292

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Programs with Solutions
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download c program float charchter integer and more Thesis Mathematics in PDF only on Docsity!

C Programs with Solutions

C

Programs with Solutions

By

S. ANANDAMURUGAN M.E., (Ph.D)., MISTE., MACEEE., Senior Lecturer, Department of Computer Science & Engineering, Kongu Engineering College, Perundurai Tamil Nadu

UNIVERSITY SCIENCE PRESS

(An Imprint of Laxmi Publications Pvt. Ltd.)

BANGALORE (^) • CHENNAI (^) • COCHIN (^) • GUWAHATI (^) • HYDERABAD JALANDHAR (^) • KOLKATA (^) • LUCKNOW (^) • MUMBAI (^) • PATNA RANCHI (^) • NEW DELHI

Dedicated to My Son Master A. Shrikarthick

Contents

    1. C Concepts 1– Chapters Pages
    1. Introduction—C Programs 12–
    1. Fundamentals—C Programs 52–
    1. C Debugging 113–
    1. Sample Questions 249–
    1. Short Questions and Answers 262–
    1. Questions 280–

1.0 OVERVIEW OF C PROGRAMMING

C language is one of the most popular computer languages today because it is a structured, high level, machine independent language. It allows software developers to develop programs without worrying about the hardware platforms where they will be implemented. C is called a high level, compiler language. The aim of any high level computer language is to provide an easy and natural way of giving a programme of instructions to a computer.

C is one of a large number of high level languages which can be used for general purpose programming, i.e., anything from writing small programs for personal amusement to writing complex applications. It is unusual in several ways. Before C, high level languages were criticized by machine code programmers because they shielded the user from the working details of the computer. The C language has been equipped with features that allow programs to be organized in an easy and logical way. This is vitally important for writing lengthy programs because complex problems are only manageable with a clear organization and program structure.

C allows meaningful variable names and meaningful function names to be used in programs without any loss of efficiency and it gives a complete freedom of style, it has a set of very flexible loop constructions and neat ways of making decisions. These provide an excellent basis for controlling the flow of programs. Another feature of C is the way it can express ideas concisely. The richness of a language shapes what it can talk about. C gives us the apparatus to build neat and compact programs. C tries to make the best of a computer by linking as closely as possible to the local environment.

The increasing popularity of C is probably due to its many desirable qualities. It is a robust language whose rich set of built-in functions and operators can be used to write any complex program. The C compiler combines the capabilities of an assembly language with the features of a high-level language and therefore it is well suited for writing both system software and business packages. Programs written in C are efficient and fast. This is due to its variety of data types and powerful operators. C is highly portable. This means that C programs written for one computer can be run on another with little or no modification. Another feature of C is its ability to extend itself.

1

C CONCEPTS

Chapter 1

2 C PROGRAMS WITH SOLUTIONS

1.1 INTRODUCTION

C is a remarkable language. Designed originally by Dennis Ritchie, working at AT&T Bell Laboratories in New Jersey, it has increased in use until now it may well be one of the most widely- written computer languages in the world. C is a structured language. It allows variety of programs in small modules. It is easy for debugging, testing, and maintenance if a language is a structured one.

1.2 STRUCTURE OF A C PROGRAM

Include header file section Global declaration section main() { Declaration part Executable part } User-defined functions { Statements }

Include Header File Section

C program depends upon some header files for function definition that are used in program. Each header file by default is extended with .h. The header file should be included using # include directive as given here.

Global Declaration

This section declares some variables that are used in more than one function. These variables are known as global variables. This section must be declared outside of all the functions.

Function Main

Every program written in C language must contain main () function. The function main() is a starting point of every C program. The execution of the program always begins with the function main ().

Declaration Part

The declaration part declares the entire variables that are used in executable part. The initialisations of variables are also done in this section. Initialisation means providing initial value to the variables.

4 C PROGRAMS WITH SOLUTIONS

" Quotation mark < Less than ! Exclamation mark > Greater than | Vertical bar () Parenthesis left/right / Slash [ ] Bracket left/right \ Back slash {} Braces left/right ~ Tilde % Percent _ Underscore # Number sign or Hash $ Dollar = Equal to ? Question mark @ At the rate

1.5 DELIMITERS

Delimiters Use : Colon Useful for label ; Semicolon Terminates the statement ( ) Parenthesis Used in expression and function [ ] Square Bracket Used for array declaration { } Curly Brace Scope of the statement

hash Preprocessor directive

, Comma Variable separator

1.6 C KEYWORDS

Auto Double Int Struct Break Else Long Switch Case Enum Register Typedef Char Extern Return Union Const Float Short Unsigned Continue For Signed Void Default Goto Sizeof Volatile Do If Static while

C C ONCEPTS 5

1.7 IDENTIFIERS

Identifiers are names of variables, functions, and arrays. They are user-defined names, consisting sequence of letters and digits, with the letter as the first character.

1.8 CONSTANTS

Values do not change during the execution of the program Types:

1. Numerical constants: — Integer constants These are the sequence of numbers from 0 to 9 without decimal points or fractional part or any other symbols. It requires minimum two bytes and maximum four bytes. Eg: 10,20, + 30, – 14 — Real constants It is also known as floating point constants. Eg: 2.5, 5. 2. Character constants: — Single character constants A character constant is a single character. Characters are also represented with a single digit or a single special symbol or white space enclosed within a pair of single quote marks Eg: ‘a’, ‘8’, “ ”. — String constants String constants are sequence of characters enclosed within double quote marks. Eg: “Hello”, “india”, “444”

1.9 VARIABLES

It is a data name used for storing a data value. Its value may be changed during the program execution. The value of variables keeps on changing during the execution of a program.

1.10 DATA TYPES

Data type Size (Bytes) Range Format Specifiers Char 1 – 128 to 127 %c Unsigned char 1 0 to 255 %c

C C ONCEPTS 7

Unformatted Functions

— The unformatted input/output functions only work with the charcter data type Input Output getch() putch() getche() putchar() getchar() put() gets()

1.13 DECISION STATEMENTS

It checks the given condition and then executes its sub-block. The decision statement decides the statement to be executed after the success or failure of a given condition. Types:

  1. If statement
  2. If-else statement
  3. Nested if-else statement
  4. Break statement
  5. Continue statement
  6. Goto statement
  7. Switch() statement
  8. Nested switch ()case
  9. Switch() case and Nested if

Statement Syntax If statement if(condition) Statement; If-else statement If (condition) { Statement 1; Statement 2; } else { Statement 3; Statement 4; } Nested if-else statement If (condition) {

8 C PROGRAMS WITH SOLUTIONS

Statement 1; Statement 2; } Else if (condition) { Statement 3; Statement 4; } Else { Statement 5; Statement 6; } Break statement Break; Continue statement Continue; Goto statement goto label; Switch() statement Switch (variable or expression) { Case constant A: Statement; Break; Case constant B: Statement; Break; Default: Statement; }

1.14 LOOP CONTROL STATEMENTS

Loop is a block of statements which are repeatedly executed for certain number of times. Types

  1. For loop
  2. Nested for loops
  3. While loop
  4. do while loop
  5. do-while statement with while loop

10 C PROGRAMS WITH SOLUTIONS

Operations

  1. Insertion
  2. Deletion
  3. Searching
  4. Sorting
  5. Merging

1.16 STRINGS

Character arrays are called strings. Group of characters, digits, symbols enclosed within quotation marks are called as strings.

String Standard Functions

Functions Description Strlen() Determines the length of a string Strcpy() Copies astring from source to destination Strncpy() Copies charcters of a string to another string upto the specified length Stricmp() Compares characters of two strings Strcmp() Compares characters of two strings upto the specified length Strncmp() Compares characters of two strings upto the specified length Strnicmp() Compares characters of two strings upto the specified length Strlwr() Converts uppercase characters of a string to lower case Strupr() Converts lowercase characters of a string to upper case Strdup() Duplicates a string Strchr() Determines the first occurrence of a given character in a string Strrchr() Determines the last occurrence of a given character in a string Strstr() Determines the first occurrence of a given string in another string Strcat() Appends source string to destination string Strrev() Reverses all characters of a string Strset() Sets all characters of a string with a given argument or symbol Strspn() Finds up to what length two strings are identical Strpbrk() Searches the first occurrence of the character in a given string and then displays the string starting from that charcter

C C ONCEPTS 11

1.17 FUNCTIONS

It is a self-contained block or a sub program of one or more statements that performs a special task.

Declaration of functions

Function_name (argument/parameter) Argument declaration; { Local variable declaration; Statement1; Statement 2; Return (value); }

Call by value

In this type, value of actual arguments is passed to the formal arguments and the operation is done on the formal arguments. Any change made in the formal argument does not effect the actual arguments because formal arguments are photo copies of actual arguments.

Call by reference

In this type, instead of passing values, addresses are passed. Function operates on address rather than values. Here the formal arguments are pointers to the actual argument.

1.18 RECURSION

A function is called repetitively by itself.

I NTRODUCTION— C P ROGRAMS 13

scanf(“%d”,&r); area=pirr; printf(“area of circle=%f ”,area); ci=2pir; printf(“circumference=%f ”,ci); getch(); } Output: enter radius of a circle: 5 area of circle=78. circumference=31. 3] Program to find the simple interest. #include #include void main() { int p,r,t,si; clrscr(); printf(“enter principle, Rate of interest & time to find simple interest: ”); scanf(“%d%d%d”,&p,&r,&t); si=(prt)/100; printf(“simple intrest= %d”,si);

getch();

} Output: enter principle, rate of interest & time to find simple interest: 500 5 2 simple interest=

4] Program to convert temperature from degree centigrade to Fahrenheit.

#include

#include

void main() {

14 C P ROGRAMS WITH S OLUTIONS

float c,f; clrscr(); printf(“enter temp in centigrade: ”); scanf(“%f”,&c); f=(1.8c)+32; printf(“temp in Fahrenheit=%f ”,f); getch(); } Output: enter temp in centigrade: 32 temp in Fahrenheit=89. 5] Program to calculate sum of 5 subjects and find percentage. #include #include void main() { int s1,s2,s3,s4,s5,sum,total=500; float per; clrscr(); printf(“enter marks of 5 subjects: ”); scanf(“%d%d%d%d%d”,&s1,&s2,&s3,&s4,&s5); sum=s1+s2+s3+s4+s5; printf(“sum=%d”,sum); per=(sum100)/total; printf(“percentage=%f”,per); getch(); } Output: enter marks of 5 subjects: 60 65 50 60 60 sum= percentage=60. 6] Program to show swap of two no’s without using third variable. #include #include