sample copy. not for distribution. · 2019. 3. 19. · by: deepak sharma . ph.d. (research scholar)...

13
Sample Copy. Not For Distribution.

Upload: others

Post on 23-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sample Copy. Not For Distribution. · 2019. 3. 19. · by: deepak sharma . ph.d. (research scholar) @ mdu, rohtak . m.tech (87%), ugc-net/jrf (3 times) gate, b.tech . beginner’s

Sample Copy. Not For Distribution.

Page 2: Sample Copy. Not For Distribution. · 2019. 3. 19. · by: deepak sharma . ph.d. (research scholar) @ mdu, rohtak . m.tech (87%), ugc-net/jrf (3 times) gate, b.tech . beginner’s

BY:

DEEPAK SHARMA

Ph.D. (RESEARCH SCHOLAR) @ MDU, ROHTAK

M.TECH (87%), UGC-NET/JRF (3 TIMES)

GATE, B.TECH

BEGINNER’S GUIDE

FOR

PROGRAMMING

Sample Copy. Not For Distribution.

Page 3: Sample Copy. Not For Distribution. · 2019. 3. 19. · by: deepak sharma . ph.d. (research scholar) @ mdu, rohtak . m.tech (87%), ugc-net/jrf (3 times) gate, b.tech . beginner’s

P a g e | 2

EDUCREATION PUBLISHINGRZ 94, Sector - 6, Dwarka, New Delhi - 110075Shubham Vihar, Mangla, Bilaspur, Chhattisgarh - 495001

Website: www.educreation.in

________________________________________________________________

© Copyright, 2019, Author

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted, in any form by any means, electronic, mechanical, magnetic, optical, chemical, manual, photocopying, recording or otherwise, without the prior written consent of its writer.

ISBN: 978-93-88910-81-1Price: Rs.250.00

The opinions/ contents expressed in this book are solely of the authors and do not represent the opinions/ standings/ thoughts of Educreation or the Editors . The book is released by using the services of self-publishing house.

Printed in India

Sample Copy. Not For Distribution.

Page 4: Sample Copy. Not For Distribution. · 2019. 3. 19. · by: deepak sharma . ph.d. (research scholar) @ mdu, rohtak . m.tech (87%), ugc-net/jrf (3 times) gate, b.tech . beginner’s

P a g e | 3

PREFACE

Hi, Dear Reader..... How are u? I hope you are fine.

I am good, thanks for asking.

A book is a writer's child. I made this one with great efforts.

Thanks for investing in this book and I am sure you will get a

good return.

This book is meant for all those people who want to start from

basic and learn the art of programming in c language. I will be

highly obliged if you message/mail me you feedback or

suggestion on +91-9671878677 (Whatsapp only) or on

[email protected].

How to use this book?

In this book, you will find a collection of 50 programs which are

being asked in almost every interview related to C

programming. After practicing these 50 programs you will see

the improvement in your programming skills and also in your

confidence. By learning these 50 programs one can easily

crack any interview based on C programming language.

You will find an explanation for each and every program in a

very simple manner just go through the program and try to

develop a logic after that see the output and try to figure out the

different path of execution and then finally see the explanation

of the program which helps you to get complete insight about

the program.

I wish you all the very best for your journey of learning the C

programming language.

“GOOD LUCK”

Sample Copy. Not For Distribution.

Page 5: Sample Copy. Not For Distribution. · 2019. 3. 19. · by: deepak sharma . ph.d. (research scholar) @ mdu, rohtak . m.tech (87%), ugc-net/jrf (3 times) gate, b.tech . beginner’s

P a g e | 4

ACKNOWLEDGMENTS

Writing a book is harder than I thought and more rewarding

than I could have ever imagined, none of this would be possible

without the blessings of my parents and elders.

I want to thank my parents Dr. Pawan Kumar Sharma

(Associate Professor, Government of Haryana) and Mrs.

Sushma Sharma who always stood by my side and supported

me emotionally as well as financially.

I want to thank GOD for helping me through every phase of my

life. I truly believe that anything can be achieved with the help

of GOD.

In the end, I want to thank EVERYONE who ever said anything

positive to me or taught me something. I heard it all, and it

meant something.

DEEPAK SHARMA

Ph.D. (Pursuing), M.TECH

UGC NET/JRF(3 times,) GATE

+91-9671878677(Whatsapp only)

[email protected]

Sample Copy. Not For Distribution.

Page 6: Sample Copy. Not For Distribution. · 2019. 3. 19. · by: deepak sharma . ph.d. (research scholar) @ mdu, rohtak . m.tech (87%), ugc-net/jrf (3 times) gate, b.tech . beginner’s

P a g e | 5

Sample Copy. Not For Distribution.

Page 7: Sample Copy. Not For Distribution. · 2019. 3. 19. · by: deepak sharma . ph.d. (research scholar) @ mdu, rohtak . m.tech (87%), ugc-net/jrf (3 times) gate, b.tech . beginner’s

P a g e | 6

CONTENTS

Sr. NAME OF PROGRAM Page

1 Program to print “Hello world” 10

2 Program to take the input of various data

types in C

13

3 Program to find the ASCII value of the

character

16

4 Program to show the use of gets and

puts function

18

5 Program to show the use of basic if-else

condition

20

6 Program to show the use of switch case with

break

22

7 Program to check if the input character is a

vowel using switch case

27

8 Program to reverse the case of the input

character

30

9 Program for swapping of two numbers

using a temporary variable

33

10 Program for swapping of two numbers

without using a temporary variable

36

11 Program for swapping two numbers using a

bitwise operator

38

Sample Copy. Not For Distribution.

Page 8: Sample Copy. Not For Distribution. · 2019. 3. 19. · by: deepak sharma . ph.d. (research scholar) @ mdu, rohtak . m.tech (87%), ugc-net/jrf (3 times) gate, b.tech . beginner’s

P a g e | 7

12 Program for swapping two numbers using

multiplication and division

41

13 Program to print the largest and smallest

using global declaration

43

14 Program to print half pyramid a using

numbers

46

15 Program to print half pyramid using * 49

16 Program to print half pyramid using

alphabets

52

17 Programs to print inverted half pyramid

using * and numbers

55

18 Program to print full pyramid using * 58

19 Program to print pascal's triangle 61

20 Program to print Floyd’s triangle 65

21 Program to show the use of a simple while

loop

68

22 Program to show the use of basic for loop 71

23 Program to show the use of basic do-while

loop

74

24 Program to show the use of nested for loops 77

25 Program to print factorial of a number 80

26 Program to print the Fibonacci series 83

Sample Copy. Not For Distribution.

Page 9: Sample Copy. Not For Distribution. · 2019. 3. 19. · by: deepak sharma . ph.d. (research scholar) @ mdu, rohtak . m.tech (87%), ugc-net/jrf (3 times) gate, b.tech . beginner’s

P a g e | 8

27 Program to check whether a number is a

palindrome or not

86

28 Program to find the sum of digits of a

number

89

29 Program to reverse a string 92

30 Program for showing the use of conditional

operator

95

31 Program to find the average of n numbers 97

32 Program to find Armstrong number between

1 and 500

100

33 Program for checking whether a number is

even or odd using a bitwise operator

102

34 Program to find factors of a number 104

35 Program to find the sum of n input numbers 107

36 Program to find first n prime numbers 110

37 Program to find the largest number among n

input numbers

114

38 Program to find exponential without using

pow() method

117

39 Program to check if the input number is int

or float

120

40 Program to print the multiplication table of

any number

124

41 Program to insert a new element in an

existing array at the desired location

128

42 Program to delete an element from an

existing array

133

Sample Copy. Not For Distribution.

Page 10: Sample Copy. Not For Distribution. · 2019. 3. 19. · by: deepak sharma . ph.d. (research scholar) @ mdu, rohtak . m.tech (87%), ugc-net/jrf (3 times) gate, b.tech . beginner’s

P a g e | 9

43 Program to find the largest and smallest

element in an array

137

44 Program to find the sum of n input numbers

using an array

141

45 Program to calculate simple interest 144

46 Program to calculate Compound interest 146

47 Program to print day of week name using

switch case

148

48 Program to identify a leap year 153

49 Program to find the largest of three numbers 157

50 Program to find the second largest number

among three user input numbers

160

Sample Copy. Not For Distribution.

Page 11: Sample Copy. Not For Distribution. · 2019. 3. 19. · by: deepak sharma . ph.d. (research scholar) @ mdu, rohtak . m.tech (87%), ugc-net/jrf (3 times) gate, b.tech . beginner’s

P a g e | 10

1. PROGRAM TO PRINT “HELLO WORLD”:

Program:

#include<stdio.h>

#include<conio.h>

int main()

{

clrscr();

int num; //variable declaration with int data type

printf("\n\t\t\t Hello World! \n\t\t Welcome to the world of \n\t\t\t

C PROGRAMS\n");

printf("\n\n\t\t\tWriter : Deepak Sharma\n\n\n");

getch();

return 0;

}

Output:

Sample Copy. Not For Distribution.

Page 12: Sample Copy. Not For Distribution. · 2019. 3. 19. · by: deepak sharma . ph.d. (research scholar) @ mdu, rohtak . m.tech (87%), ugc-net/jrf (3 times) gate, b.tech . beginner’s

P a g e | 11

Explanation:

In this program we are printing some text on the screen using printf

statement.

#include<filename> is simply preprocessor directive which is used

for file inclusion. Using #include perprocessor directive we can

simply reuse the contents of one file in to another like functions,

variables etc.

#include<stdio.h> header file used for standard input and output

operations. This is used to take input from keyboard and displaying

output on screen. #include<stdio.h> contains functions like printf

and scanf.

#include<conio.h> header file used for console input output

operations. This header file contains functions such as getch(); and

clrscr();

int main(); Main function is the essential part of any c program

because execution of every c program starts from main function.

clrscr(); This is a predefined function of conio.h This function is

used to clear the previous output on the output window.

printf(""); This is a readymade predefined function used for

printing on the output window.

\n: New line character used for advances the output to the next line

equivalent to pressing the enter key on keyboard.

\t: Used for tab spacing between the values equivalent to pressing

the tab on keyboard.

Sample Copy. Not For Distribution.

Page 13: Sample Copy. Not For Distribution. · 2019. 3. 19. · by: deepak sharma . ph.d. (research scholar) @ mdu, rohtak . m.tech (87%), ugc-net/jrf (3 times) gate, b.tech . beginner’s

Sample Copy. Not For Distribution.