LET US C 8TH EDITION EBOOK
Where can I get PDF version of book "Let Us C" by Yashwant Kanetkar's? Where can I get PDF solutions to Yashwant Kanetkar's "Let Us C" 15th edition? Where can I get Let Us C by Yashwant Kanetkar 5th edition solution as a PDF file?. book on C. Let Us C Of Yashwant Kanetkar 11th Edition check out more at instruktsiya.info Let Us C Fifth Edition Yashavant P. Kanetkar Dedicated to baba Who couldn't be The expression num designates the twenty-eighth element in the array.
|Language:||English, Spanish, Indonesian|
|Genre:||Fiction & Literature|
|ePub File Size:||28.88 MB|
|PDF File Size:||14.70 MB|
|Distribution:||Free* [*Regsitration Required]|
PDF Drive is your search engine for PDF files. As of today we have 78,, eBooks for you to download for free. No annoying ads, no download limits, enjoy . idea of “Let Us C” was conceived up to the release of this Fifth free to download them, improv Principles of Data Structures Using C and C++. This is 11th edition of let us C by Yashwant instruktsiya.info is basic info about the ebook. About the Book: (let us c solutions pdf free download) let us c is the best selling Yashavant P. 8th November , AM #4.
Prakash Jose on 1 st February If any one has the pfd file of let us C, plz send it to nandishhs yahoo. Nandish on 13 th January So kindly send the ebooks to my mail. So,pls send me that book as soon as possible to my mail ID " mdu. SK P on 14 th December Sir I want to learn C language. Shashank Vishwakarma on 12 th December Yogesh on 8 th December Rajesh thakur on 5 th December It's a placement time. Akash on 29 th November Sir pls send me direct link for "test your c skills" by yashwant kanitkar.
Sir, i want this book at the earliest. Thank you. Jay on 21 st November I want solution of the exercise of Let Us C book.
So plz send urgently. Archana on 20 th November Mohsin Khan on 3 rd November I'll be very thankfull to you. Ishaan on 18 th October Priyanka Verma on 17 th October Huma Taha on 17 th October It'd be real kind of you. My e mail id is: Hira Zainab on 10 th October Dear Sir, I am an housewife. I had done my bca I have to learn C very deeply to work as school teacher pls sedn me a copy very urgently.
I want to learn Loop in details and also learn all symbol use in c. I need Test your C skills by yaswanth kanethkar e-book. Please send me. B sunil on 8 th September Anybody having "Let us C-solutions" kindly inform through mail.
Amit kumar on 30 th August Thanks Ishika. Sundari on 27 th August Thangadurai on 27 th August Need the solutions of the exercise of Let us C by yashwanth kanetkar.
I need it. So,please do me this favour as soon as you can. Ankeet Panda on 15 th August But at the same time I also want to learn the C language deeply so that I can write codes independent of the platform and also because it is considered to be one of the basic and till date one of the most robust and powerful programming languages. I've 'Let Us C' but what else should I go through. Can u please send me some links? Sayan Mukherjee on 12 th August Sir, i need ebook of "let us c" by yashwant kanitkar.
If u have,then plz send the link or file to my e-mail id. Sir I Want Let us C by kanetkar answers. Can you please send to mail ID Thanks, Sekhar. Sir i am in GIS field and i want to convert my career in to programming,so give me some manual of c for beigner. Thanking You Jyotirmaya Mishra. Jyotirmaya Mishra on 10 th August Kishan srivastava on 8 th August Sir, I want to learn C language.
I feel very hard to learn that language. Can you please help me to learn C by myself. With regards, Vennila.
A on 7 th August Sir, i need e book of "let us c solution" by yashwant kanitkar. If u have,then plz send me. Debashis Das on 6 th August Hai i am rengarajan i joined in software company last week now am in the training phase i need c latest version book that u have.
Sir, i need e book of "let us c" by yashwant kanitkar. I want to learn C language. Can you please send me ebooks on C language.
And some books which are specific on pointers. Rima on 16 th July Anant T. Jadhav on 11 th July Vijay M on 11 th July I am learnig C on my own with bible of C Ashwani Saxena on 7 th July Tanushri Kundu on 7 th July Akshay on 3 rd July I need c language book. Vijay Ankush Mane on 27 th June Thankyou Usman. Mohammad Usman on 22 nd June Sir,I just wanted to learn C Basic. So i need some good guidelines how to learn C language in easy way.
So i wanted to download free Let us C Book. How can i get it. Revathi on 22 nd June Dear Sir, I m pursuing diploma in electronics. I want this book as it is in my course. So, pl. I hope you will help me. Monika Singh on 13 th June Maninder Singh on 12 th June I want to learn C progarming from the very Basics.
Muralikrishnan on 2 nd June How to get easy free download for Let us C language. Lakshmi on 21 st May Manish on 15 th May Geetha on 14 th May How can we create an pointer array and giving the values to them and comparing with the variable values E please give me the study material of c language.
Naveen Kumar on 3 rd March Md Irfan on 19 th February Jeet on 15 th February Muhammad RAfi on 10 th February ANJU on 30 th January Ebook "let us C" by yashawant kanetkar. I want learn C and I want e book of let us c by yashawant kanetkar Smit Achakapure on 29 th January If I could get a copy of the E book, I would be grateful. Sneha gopinah on 23 rd January Please send me link to book Letus C By yashwant Kanetkar as i have to revise structures because earlier i read from that book.
Please send me the link of answers of Let Us C as early as early Now i am pursuing MCA 2nd year and i want to improve my C knowledge so please send me the links to download any pdf form of C books. Hi, I need the link for let us C by yashwant kanetkar. Can u please mail me?
Thanks, Shwetha. I want c book on your web site and my email id. Sir i want to learn c language pl send me notes for c language from begning. I am pursuing software engineering and has Embedded C subject in my course this sem, so I desparately need this "Let Us C" book online version by Yashvant Kanitker as soon as possible as my sem exam is gonna held soon.
Rishabh Jain on 20 th November Bhushan Bhagwat Bharambe on 18 th October Also, if there are more than one set of parentheses, the operations within the innermost parentheses would be performed first, followed by the operations within the second innermost pair and so on. A careless imbalance of the right and left parentheses is a common error. Best way to avoid this error is to type and then type an expression inside it. A few examples would clarify the issue further.
Example 1. Determine the hierarchy of operations and evaluate the following expression: This so happens because 6 and 4 both are integers and therefore would evaluate to only an integer constant. All operators in C are ranked according to their precedence. And mind you there are as many as 45 odd operators in C, and these can affect the evaluation of an expression in subtle and unexpected ways if we aren't careful.
However, it can be realized at this stage that it would be almost impossible to remember the precedence of all these operators. So a full-fledged list of all operators and their precedence is given in Appendix A. This may sound daunting, but when its contents are absorbed in small bites, it becomes more palatable. So far we have seen how the computer evaluates an arithmetic statement written in C. But our knowledge would be incomplete unless we know how to convert a general arithmetic statement to a C statement.
C can handle any complex expression with ease. Some of the examples of C expressions are shown in Figure 1.
Associativity can be of two types—Left to Right or Right to Left. Getting Started 35 unambiguous. Unambiguous in what sense? It must not be involved in evaluation of any other sub-expression. Similarly, in case of Right to Left associativity the right operand must be unambiguous. Let us understand this with an example. But both enjoy Left to Right associativity. Getting Started 37 since no matter which is performed earlier the result would be same.
Appendix A gives the associativity of all the operators available in C. There are four types of control instructions in C.
Let Us C Of Yashwant Kanetkar 11th Edition
They are: Decision and Case control instructions allow the computer to take a decision as to which instruction is to be executed next. The Loop control instruction helps computer to execute a group of statements repeatedly.
In the following chapters we are going to learn these instructions in detail. Try your hand at the Exercise presented on the following pages before proceeding to the next chapter, which discusses the decision control instruction. Summary a The three primary constants and variable types in C are integer, float and character. Exercise [A] Which of the following are invalid variable names and why? A sample entry has been filled in the table for expression a.
Getting Started 45 1 8. Write a program to calculate his gross salary. Getting Started 47 b The distance between two cities in km. Write a program to convert and print this distance in meters, feet, inches and centimeters.
Assume that the maximum marks that can be obtained by a student in each subject is Write a program to convert this temperature into Centigrade degrees. Write a program to interchange the contents of C and D. The percentage of total literacy is If the amount to be withdrawn is input through the keyboard in hundreds, find the total number of currency notes of each denomination the cashier will have to give to the withdrawer. For example if the number that is input is then the output should be displayed as If the weather is fine, then I will go for a stroll.
If the highway is busy I would take a diversion. If the pitch takes spin, we would win the match. If she says no, I would look elsewhere. If you like this book, I would write the next edition. You can notice that all these decisions depend on some condition being met. C language too must be able to perform different sets of actions depending on the circumstances. In fact this is what makes it worth its salt. C has three major decision making instructions—the if statement, the if-else statement, and the switch statement.
A fourth, somewhat less important structure is the one that uses conditional operators. In this chapter we will explore all these ways except switch, which has a separate chapter devoted to it, later in which a C program can react to changing circumstances.
In the programs written in Chapter 1 we have used sequence control structure in which the various steps are executed sequentially, i. By default the instructions in a program are executed sequentially. However, in serious programming situations, seldom do we want the instructions to be executed sequentially. Many a times, we want a set of instructions to be executed in one situation, and an entirely different set of instructions to be executed in another situation.
This kind of situation is dealt in C programs using a decision control instruction. As mentioned earlier, a decision control instruction can be implemented in C using: The Decision Control Structure 51 Now let us learn each of these and their variations in turn. The if Statement Like most languages, C uses the keyword if to implement the decision control instruction.
The general form of if statement looks like this: The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, then the statement is executed. If the condition is not true then the statement is not executed; instead the program skips past it. But how do we express the condition itself in C? And how do we evaluate its truth or falsity?
The relational operators allow us to compare two values to see whether they are equal to each other, unequal, or whether one is greater than the other. Here is a simple program, which demonstrates the use of if and the relational operators. The following flowchart would help you understand the flow of control in the program. Chapter 2: STOP Figure 2. Study it carefully before reading further. To help you understand it easily, the program is accompanied by an appropriate flowchart.
Example 2. If quantity and price per item are input through the keyboard, write a program to calculate the total expenses. Enter quantity and rate Therefore, the variable dis, which was earlier set to 0, now gets a new value Using this new value total expenses are calculated and printed.
Thus, dis, which is earlier set to 0, remains 0, and hence the expression after the minus sign evaluates to zero, thereby offering no discount. The answer is yes, since in C, a variable if not specifically initialized contains some unpredictable value garbage value.
The Real Thing We mentioned earlier that the general form of the if statement is as follows if condition statement ; Truly speaking the general form is as follows: We can even use arithmetic expressions in the if statement.
In the first if, the expression evaluates to 5 and since 5 is non-zero it is considered to be true. Hence the printf gets executed.
Yashavant P. Kanetkar
In the second if, 10 gets assigned to a so the if is now reduced to if a or if Since 10 is non-zero, it is true hence again printf goes to work.
In the third if, -5 is a non-zero number, hence true. So again printf goes to work. In place of -5 even if a float like 3. So the issue is not whether the number is integer or float, or whether it is positive or negative. Issue is whether it is zero or non-zero.
Multiple Statements within if It may so happen that in a program we want more than one statement to be executed if the expression following if is satisfied. The Decision Control Structure 57 placed within a pair of braces as illustrated in the following example. The current year and the year in which the employee joined the organization are entered through the keyboard. If the number of years for which the employee has served the organization is greater than 3 then a bonus of Rs.
If the years of service are not greater than 3, then the program should do nothing. If a pair of braces is not used then the C compiler assumes that the programmer wants only the immediately next statement after the if to be executed on satisfaction of the condition.
In other words we can say that the default scope of the if statement is the immediately next statement after it. It does nothing when the expression evaluates to false. Can we execute one group of statements if the expression evaluates to true and another group of statements if the expression evaluates to false? Of course! This is what is the purpose of the else statement that is demonstrated in the following example: In a company an employee is paid as under: If his salary is either equal to or above Rs.
If the employee's salary is input through the keyboard write a program to find his gross salary. The statements in the if block and those in the else block have been indented to the right.
Follow the Author
The Decision Control Structure 61 followed throughout the book to enable you to understand the working of the program better.
To override this default scope a pair of braces as shown in the above example must be used. Nested if-elses It is perfectly all right if we write an entire if-else construct within either the body of the if statement or the body of an else statement.
This is shown in the following program. If the condition in the first if statement is false, then the condition in the second if statement is checked.
If it is false as well, then the final else statement is executed. You can see in the program how each time a if-else construct is nested within another if-else construct, it is also indented to add clarity to the program.
Inculcate this habit of indentation, otherwise you would end up writing programs which nobody you included can understand easily at a later date. In the above program an if-else occurs within the else block of the first if statement. Similarly, in some other program an if-else may occur in the if block as well.
There is no limit on how deeply the ifs and the elses can be nested. Forms of if The if statement can take any of the following forms: There are several things to note about these logical operators. Most obviously, two of them are composed of double symbols: These single symbols also have a meaning. They are bitwise operators, which we would examine in Chapter Let us see how they are used in a program.
The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets a division as per the following rules: Percentage above or equal to 60 - First division Percentage between 50 and 59 - Second division Percentage between 40 and 49 - Third division Percentage less than 40 - Fail Write a program to calculate the division obtained by the student. There are two ways in which we can write a program for this example.
These methods are given below. Observe that the program uses nested if-elses. This leads to three disadvantages: As a result the whole program creeps to the right. The following program illustrates this. If one of the conditions evaluate to false then the whole thing is treated as false. Two distinct advantages can be cited in favour of this program: In the previous program the statements went on creeping to the right.
This effect becomes more pronounced as the number of conditions go on increasing. This would make the task of matching the ifs with their corresponding elses and matching of opening and closing braces that much more difficult. The else if Clause There is one more way in which we can write program for Example 2. This involves usage of else if blocks as shown below: In this case every else is associated with its previous if.
The last else goes to work only if all the conditions fail. Even in else if ladder the last else is optional. Note that the else if clause is nothing different. It is just a way of rearranging the else with the if that follows it. This would be evident if you look at the following code: For example, consider the following example: A company insures its drivers in the following cases: In all other cases the driver is not insured.
If the marital status, sex and age of the driver are the inputs, write a program to determine whether the driver is to be insured or not. Here after checking a complicated set of instructions the final output of the program would be one of the two—Either the driver should be ensured or the driver should not be ensured.
As mentioned above, since these are the only two outcomes this problem can be solved using logical operators. But before we do that let us write a program that does not make use of logical operators. In a more real-life situation there would be more conditions to check leading to the program creeping to the right.
Let us now see how to avoid these problems by using logical operators. If we list down all those cases in which the driver is insured, then they would be: The following program demonstrates it. Write a program to calculate the salary as per the following table: The third logical operator is the NOT operator, written as!. This operator reverses the result of the expression it operates on. For example, if the expression evaluates to a non-zero value, then applying!
Vice versa, if the expression evaluates to zero then on applying! The final result after applying! Here is an example of the NOT operator applied to a relational expression. The NOT operator is often used to reverse the logical value of a single variable, as in the expression if! Avoid it if you want, as the same thing can be achieved without using the NOT operator. Hierarchy of Operators Revisited Since we have now added the logical operators to the list of operators we know, it is time to review these operators and their priorities.
Figure 2. The higher the position of an operator is in the table, higher is its priority. A full-fledged precedence table of operators is given in Appendix A. Operators Type! Enter value of i You entered 5 Enter value of i You entered 5 Surprising? This is because we have written the condition wrongly. As a result, the condition gets reduced to if 5 , irrespective of what you supply as the value of i. Therefore, if 5 always evaluates to true and hence the result.
Another common mistake while using the if statement is to write a semicolon ; after the condition, as shown below: If the condition fails then straightaway the printf gets executed. Thus, irrespective of whether the condition evaluates to true or false the printf is bound to get executed. Remember that the compiler would not point out this as an error, since as far as the syntax is concerned nothing has gone wrong, but the logic has certainly gone awry.
Moral is, beware of such pitfalls. The following figure summarizes the working of all the three logical operators. Operands Results x y!
In fact, they form a kind of foreshortened if-then-else. Their general form is, expression 1? Let us understand this with the help of a few examples: The following points may be noted about the conditional operators: This is illustrated in the following examples: The error can be overcome by enclosing the statement in the: Hence it reports an error.
The limitation of the conditional operators is that after the? In practice rarely is this the requirement. Summary a There are three ways for taking decisions in a program.
So, to execute more than one statement they must be written in a pair of braces. However, an else block is always associated with an if statement. A zero value is considered to be false and a non-zero value is considered to be true. Exercise if, if-else, Nested if-elses [A] What would be the output of the following programs: Also determine how much profit he made or loss he incurred.
The Decision Control Structure 83 b Any integer is input through the keyboard. Write a program to find out whether it is an odd number or even number. Write a program to determine whether the year is a leap year or not.
If any year is input through the keyboard write a program to find out what is the day on 1st January of this year. Write a program to obtain the reversed number and to determine whether the original and reversed numbers are equal or not.
A triangle is valid if the sum of all the three angles is equal to degrees. Use sqrt and pow functions l Given a point x, y , write a program to find out if it lies on the x-axis, y-axis or at the origin, viz. Expression Value a! Grade is 10 if all three conditions are met Grade is 9 if conditions i and ii are met Grade is 8 if conditions ii and iii are met Grade is 7 if conditions i and iii are met Grade is 6 if only one condition is met Grade is 5 if none of the conditions are met Write a program, which will require the user to give values of hardness, carbon content and tensile strength of the steel under consideration and output the grade of the steel.
For first 5 days the fine is 50 paise, for days fine is one rupee and above 10 days fine is 5 rupees. If you return the book after 30 days your membership will be cancelled. Write a program to accept the number of days the member is late to return the book and display the fine or the appropriate message. The Decision Control Structure 91 f If the three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is valid or not.
The triangle is valid if the sum of two sides is greater than the largest of the three sides. If the time taken by the worker is between 2 — 3 hours, then the worker is said to be highly efficient. If the time required by the worker is between 3 — 4 hours, then the worker is ordered to improve speed. If the time taken is between 4 — 5 hours, the worker is given training to improve his speed, and if the time taken by the worker is more than 5 hours, then the worker has to leave the company.
If the time taken by the worker is input through the keyboard, find the efficiency of the worker. However, he should get at least 45 percent in A. Write a program to receive marks in A and B and Output whether the student has passed, failed or is allowed to reappear in B. Send him intimation. Intimate to him data the balance will be shipped. Write a C program to implement the company policy. Conditional operators [G] What would be the output of the following programs: In the first one, the calculations were carried out in a fixed order, while in the second, an appropriate set of instructions were executed depending upon the outcome of the condition being tested or a logical decision being taken.
You can probably think of several examples of this from real life, such as eating a good dinner or going for a movie. Programming is the same; we frequently need to perform an action over and over, often with variations in the details each time. Loops The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied.
This repetitive operation is done through a loop control instruction. There are three methods by way of which we can repeat a part of a program.
The while Loop It is often the case in programming that you want to do something a fixed number of times. Perhaps you want to calculate gross salaries of ten different persons, or you want to convert temperatures from centigrade to fahrenheit for 15 different cities.
Chapter 3: The Loop Control Structure 99 The while loop is ideally suited for such cases. Let us look at a simple example, which uses a while loop. The flowchart shown below would help you to understand the operation of the while loop. Enter values of p, n and r 5 The logic for calculating the simple interest is written within a pair of braces immediately after the while keyword. The parentheses after the while contain a condition. So long as this condition remains true all statements within the body of the while loop keep getting executed repeatedly.
To begin with the variable count is initialized to 1 and every time the simple interest logic is executed the value of count is incremented by one. The operation of the while loop is illustrated in the following figure. In place of the condition there can be any other valid expression. So long as the expression evaluates to a non-zero value the statements within the loop would get executed.
In the first case the parentheses are optional. The Loop Control Structure This is an indefinite loop, since i remains equal to 1 forever. The correct form would be as under: It can even be a float. Once again the increment and decrement could be by any value, not necessarily 1. What do you think would be the output of the following program? To begin with, it prints out numbers from 1 to After that value of i is incremented by 1, therefore it tries to become , which falls outside the valid integer range, so it goes to other side and becomes which would certainly satisfy the condition in the while.
This process goes on indefinitely. The reason is, we have carelessly given a ; after the while. This would make the loop work like this In fact we can put a pair of braces around any individual statement or set of statements without affecting the execution of the program.
More Operators There are variety of operators which are frequently used with while. To illustrate their usage let us consider a problem wherein numbers from 1 to 10 are to be printed on the screen. The program for performing this task can be written using while in the following different ways: Similarly, to reduce the value of a variable by 1 a decrement operator -- is also available.
It increments the value of i by 1. When the control reaches printf , i has already been incremented, hence i must be initialized to 0.
The for Loop Perhaps one reason why few programmers use while is that they are too busy using the for, which is probably the most popular looping instruction.
The for allows us to specify three things about a loop in a single line: Compare this program with the one, which we wrote using while. The flowchart is also given below for a better understanding. Let us now examine how the for statement gets executed: Since count is 1 the condition is satisfied and the body of the loop is executed for the first time. The following figure would help in further clarifying the concept of execution of the for loop.
Thus the following for loops are perfectly ok. This time we would use a for loop instead of a while loop. Since there is only one statement in the body of the for loop, the pair of braces have been dropped.
As with the while, the default scope of for is the immediately next statement after for. Note that inspite of this the semicolon after the condition is necessary. The Loop Control Structure Here the initialisation is done in the declaration statement itself, but still the semicolon before the condition is necessary.
Note that it is necessary to initialize i to 0. Nesting of Loops The way if statements can be nested, similarly whiles and fors can also be nested. To understand how nested loops work, look at the program given below: The Loop Control Structure terminates when the value of c exceeds 2, and the outer loop terminates when the value of r exceeds 3.
As you can see, the body of the outer for loop is indented, and the body of the inner for loop is further indented. These multiple indentations make the program easier to understand. Instead of using two statements, one to calculate sum and another to print it out, we can compact this into one single statement by saying: Not only this, a for loop can occur within a while loop, or a while within a for.
Multiple Initialisations in the for Loop The initialisation expression of the for loop can contain more than one statement separated by a comma.
However, only one expression is allowed in the test expression. This expression may contain several conditions linked together using logical operators. Use of multiple statements in the initialisation expression also demonstrates why semicolons are used to separate the three expressions in the for loop. If commas had been used, they could not also have been used to separate multiple statements in the initialisation expression, without confusing the compiler.
However, in real life programming one comes across a situation when it is not known beforehand how many times the statements in the loop are to be executed. This situation can be programmed as shown below: Note that this loop ensures that statements within it are executed at least once even if n is supplied first time itself. The Loop Control Structure Though it is simpler to program such a requirement using a do- while loop, the same functionality if required, can also be accomplished using for and while loops as shown below: The keyword break allows us to do this.
When break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if. Write a program to determine whether a number is prime or not. A prime number is one, which is divisible only by 1 or itself. All we have to do to test whether a number is prime or not, is to divide it successively by all numbers from 2 to one less than itself.
If remainder of any of these divisions is zero, the number is not a prime. If no division yields a zero then the number is a prime number. Following program implements this logic. Why does the program require the if statement after the while loop at all? Well, there are two ways the control could have reached outside the while loop: When the loop terminates in the second case, it means that there was no number between 2 to num - 1 that could exactly divide num.
That is, num is indeed a prime. The keyword break, breaks the control only from the while in which it is placed. Consider the following program, which illustrates this fact. The continue Statement In some programming situations we want to take the control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed.
The keyword continue allows us to do this. When continue is encountered inside any loop, control automatically passes to the beginning of the loop. A continue is usually associated with an if. As an example, let's consider the following program.
The Loop Control Structure Note that when the value of i equals that of j, the continue statement takes the control to the for loop inner bypassing rest of the statements pending execution in the for loop inner. The do-while Loop The do-while loop looks like this: This difference is the place where the condition is tested.
The while tests the condition before executing any of the statements within the while loop. As against this, the do-while tests the condition after having executed the statements within the loop.
Figure 3. The while, on the other hand will not execute its statements if the condition fails for the first time. This difference is brought about more clearly by the following program. The Loop Control Structure Here, since the condition fails the first time itself, the printf will not get executed at all. Let's now write the same program using a do-while loop. There are some occasions when we want to execute a loop at least once no matter what. This is illustrated in the following example: A break takes you out of the do-while bypassing the conditional test.
A continue sends you straight to the test at the end of the loop. They modify the value of the operand to the left of them. Exercise while Loop [A] What would be the output of the following programs: Overtime is paid at the rate of Rs. Assume that employees do not work for fractional part of an hour.
Write a program to find the value of one number raised to the power of another. The Loop Control Structure e Write a program to print out all Armstrong numbers between 1 and If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number.
Your program should ensure that the computer always wins. Rules for the game are as follows: Range is the difference between the smallest and biggest number in the list.
The Loop Control Structure b An expression contains relational operators, assignment operators, and arithmetic operators. In the absence of parentheses, they will be evaluated in which of the following order: Only once 2. At least once 3. More than once 4. None of the above e In what sequence the initialization, testing and execution of body is done in a do-while loop 1. Initialization, execution of body, testing 2. Execution of body, initialization, testing 3. Initialization, testing, execution of body 4.
None of the above f Which of the following is not an infinite loop. None of the above [E] Attempt the following: Use nested loops, break and continue b Write a program to fill the entire screen with a smiling face. The Loop Control Structure Write a program, which will produce a table of values of i, y and x, where y varies from 1 to 6, and, for each value of y, x varies from 5.
The table should get displayed in the following form. The machine costs Rs. If 12 percent per annum can be earned on alternate investments what would be the minimum life of the machine to make it a more attractive investment compared to alternative investment? For example, which school to join or which hotel to visit or still harder which girl to marry you almost always end up making a wrong decision is a different matter altogether!
Serious C programming is same; the choice we are asked to make is more complicated than merely selecting between two alternatives. C provides a special control statement that allows us to handle such cases effectively; rather than using a series of if statements.
This control instruction is in fact the topic of this chapter. Towards the end of the chapter we would also study a keyword called goto, and understand why we should avoid its usage in C programming. Decisions Using switch The control statement that allows us to make a decision from the number of choices is called a switch, or more correctly a switch- case-default, since these three keywords go together to make up the control statement.
They most often appear as follows: The Case Control Structure integer. The keyword case is followed by an integer or a character constant. Each constant in each case must be different from all the others. What happens when we run a program containing a switch? First, the integer expression following the keyword switch is evaluated. The value it gives is then matched, one by one, against the constant values that follow the case statements.
When a match is found, the program executes the statements following that case, and all subsequent case and default statements as well. If no match is found with any of the case statements, only the statements following the default are executed. A few examples will show how this control structure works. Consider the following program: The program prints case 2 and 3 and the default case. Well, yes. We said the switch executes the case where a match is found and all the subsequent cases and the default as well.
If you want that only case 2 should get executed, it is upto you to get out of the switch then and there by using a break statement. The following example shows how this is done. Note that there is no need for a break statement after the default, since the control comes out of the switch anyway. The Case Control Structure The operation of switch is shown below in the form of a flowchart for a better understanding. You can in fact put the cases in any order you please.
Here is an example of scrambled case order: I am in case 22 b You are also allowed to use char values in case and switch as shown in the following program: How this can be done is shown in the following example. However, the statement would never get executed. For example, in the following program the printf never goes to work.
Chapter 4: Yes and no. Yes, because it offers a better way of writing programs as compared to if, and no because in certain situations we are left with no choice but to use if. The disadvantage of switch is that one cannot have a case in a switch which looks like: All that we can have after the case is an int constant or a char constant or an expression that evaluates to one of these constants. Even a float is not allowed. Thus the following switch statements are legal.
However, use of continue will not take the control to the beginning of switch as one is likely to believe. Such statements would be called nested switch statements.
This aspect of switch is discussed in the exercise at the end of this chapter. These are: Thus the following switch is illegal: Then why use a switch at all? For speed—switch works faster than an equivalent if-else ladder. How come? This is because the compiler generates a jump table for a switch during compilation. As a result, during execution it simply refers the jump table to decide which case should be executed, rather than actually checking which case is satisfied.
As against this, if-elses are slower because they are evaluated at execution time. A switch with 10 cases would work faster than an equivalent if-else ladder. Also, a switch with 2 cases would work slower than if-else ladder. If the 10th case is satisfied then jump table would be referred and statements for the 10th case would be executed. As against this, in an if-else ladder 10 conditions would be evaluated at execution time, which makes it slow.
Note that a lookup in the jump table is faster than evaluation of a condition, especially if the condition is complex. If on the other hand the conditions in the if-else were simple and less in number then if-else would work out faster than the lookup mechanism of a switch. Hence a switch with two cases would work slower than an equivalent if-else.
Thus, you as a programmer should take a decision which of the two should be used when. The goto Keyword Avoid goto keyword! And yet many programmers find goto seductive. In a difficult programming situation it seems so easy to use a goto to take the control where you want. However, almost always, there is a more elegant way of writing the same program using if, for, while and switch. These constructs are far more logical and easy to understand.
The big problem with gotos is that when we do use them we can never be sure how we got to a certain point in our code. They obscure the flow of control. So as far as possible skip them. You can always get the job done without them. Trust me, with good programming skills goto can always be avoided. This is the first and last time that we are going to use goto in this book. However, for sake of completeness of the book, the following program shows how to use goto.
Enter the number of goals scored against India 3 To err is human! Enter the number of goals scored against India 7 About time soccer players learnt C and said goodbye!
It is necessary to use this function since we don't want the statement printf "To err is human! Also write down the same program without using goto. Summary a When we need to choose one among number of alternatives, a switch statement is used. Damn cool! Factorial of a number. Prime or not 3. Odd or even 4. The outline of this program is given below: The Case Control Structure The statement while 1 puts the entire logic in an infinite loop. This is necessary since the menu must keep reappearing on the screen once an item is selected and an appropriate action taken.
The user should enter the class obtained by the student and the number of subjects he has failed in. If the number of subjects he failed in is less than or equal to 3 then the grace is of 5 marks per subject. If the number of subjects he failed in is less than or equal to 2 then the grace is of 4 marks per subject. He has to rely on others. You may call a mechanic to fix up your bike, hire a gardener to mow your lawn, or rely on a store to supply you groceries every month.
A computer program except for the simplest one finds itself in a similar situation. It cannot handle all the tasks by itself. In this chapter we will study these functions. We will look at a variety of features of these functions, starting with the simplest one and then working towards those that demonstrate the power of C functions. What is a Function A function is a self-contained block of statements that perform a coherent task of some kind.
Every C program can be thought of as a collection of these functions. As we noted earlier, using a function is something like hiring a person to do a specific job for you. Suppose you have a task that is always performed exactly in the same way—say a bimonthly servicing of your motorbike.
Let us now look at a simple C function that operates in much the same way as the mechanic. Actually, we will be looking at two things—a function that calls or activates the function and the function itself.
Chapter 5: Smile, and the world smiles with you Cry, and you stop the monotony! Here, main itself is a function and through it we are calling the function message. We mean that the control passes to the function message. The activity of main is temporarily suspended; it falls asleep while the message function wakes up and goes to work. When the message function runs out of statements to execute, the control returns to main , which comes to life again and begins executing its code at the exact point where it left off.
Consider the following example: I am in main I am in italy I am in brazil I am in argentina From this program a number of conclusions can be drawn: When main runs out of function calls, the program ends. As we have noted earlier the program execution always begins with main.
Except for this fact all C functions enjoy a state of perfect equality. One function can call another function it has already called but has in the meantime left temporarily in order to call a third function which will sometime later call the function that has called it, if you understand what I mean.
Trace carefully the way control passes from one function to another.
Since the compiler always begins the program execution with main , every function in a program must be called directly or indirectly by main. In other words, the main function drives other functions. Let us now summarize what we have learnt so far.
Even main can be called from other functions. However, it is advisable to define the functions in the same order in which they are called. This makes the program easier to understand. We would discuss this aspect of C functions later in this chapter. Thus, the following program code would be wrong, since argentina is being defined inside another function, main.
Library functions Ex.
User-defined functions Ex. This library of functions is present on the disk and is written for us by people who write compilers for us. Almost always a compiler comes with a library of standard functions. The procedure of calling both types of functions is exactly same. Why Use Functions Why write separate functions at all? Why not squeeze the entire logic into one function, main? Two reasons: Suppose you have a section of code in your program that calculates area of a triangle.
This section of code is nothing but a function. If the operation of a program can be divided into separate activities, and each activity placed in a different function, then each could be written and checked more or less independently. Separating the code into modular functions also makes the program easier to design and understand. What is the moral of the story? It is a very bad style of programming. Instead, break a program into small units and write functions for each of these isolated subdivisions.
What is important is that these functions perform some logically isolated task. We call them and they do what they are designed to do.
You have unknowingly used the arguments in the printf and scanf functions; the format string and the list of variables used inside the parentheses in these functions are arguments. Consider the following program. In this program, in main we receive the values of a, b and c through the keyboard and then output the sum of a, b and c. However, the calculation of sum is done in a different function called calsum.
If sum is to be calculated in calsum and values of a, b and c are received in main , then we must pass on these values to calsum , and once calsum calculates the sum we must return it from calsum back to main.
Any number of arguments can be passed to a function being called. However, the type, order and number of the actual and formal arguments must always be same. But the compiler would still treat them as different variables since they are in different functions. No separate return statement was necessary to send back the control.
This approach is fine if the called function is not going to return any meaningful value to the calling function. In the above program, however, we want to return the sum of x, y and z. Therefore, it is necessary to use the return statement. The return statement serves two purposes: In the above program the value of sum of three numbers is being returned.To get rid of this error, perform the following steps and then recompile the program.
Thank you. As a result, during execution it simply refers the jump table to decide which case should be executed, rather than actually checking which case is satisfied. The user should enter the class obtained by the student and the number of subjects he has failed in.
If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. Rumpa mandal on 30 th June The hierarchy of commonly used operators is shown in Figure 1. The Decision Control Structure 3.
- POSE METHOD OF RUNNING EBOOK
- BUSINESS STATISTICS IN PRACTICE EBOOK
- ANY EBOOK
- PERCY JACKSON THE ULTIMATE GUIDE EBOOK
- GRATIS EBOOK EN ILLEGAAL
- THE REAL ACT PREP GUIDE 3RD EDITION PDF
- EBOOK SHERLOCK HOLMES VERSI INDONESIA
- INTENSIVE CARE NURSING EBOOK
- STRENGTH TRAINING ANATOMY THIRD EDITION PDF
- FANCY GOLDFISH BOOK
- ELECTRIC CIRCUIT ANALYSIS BOOK
- GUITAR FOR DUMMIES 2ND EDITION PDF
- ABBREVIATION DICTIONARY PDF
- CONTEMPORARY ENGLISH BIBLE PDF