Posts

Showing posts with the label Chapter3 C program

Difference on Break and Continue Statement in C

Image
 

Program To Print Multiplication table of 2, 3 and 4

Image
T o print the multiplication table we have to use for loop. Inner loop is used here where two varriable are plays vital role i.e. i and j. Symbolic letter "x" and "=" are also being used to give illusion of multiplication. So, the program begins here This program takes two varriable i andd j and are multiplied in the inner loop. In a first loop the value of i remains same while the value of j increases by 1. Integer specifier are used. Here new line is printed as the loop of j ended the new line begins. Output Here, all the multiplication table is not shown because the output window is small.

Program to print 2 3 4 5 6, 4 5 6 7 , 6 7 8 , 8 9 , 10 [Nested for loop]

Image
Output Explanation Here, nested for loops is used. The varriable  i initialize from 1 and then passes through next loop j and it gets continue to execute the statement that is inside the j block until the condition of j loop gets false. In the first loop of j, the value of i is 1 which is equal to the initialization of j and prints  the addition of  i and j  i.e  2.  Next time, the value of j increase to 2 but the value of i remains same as 1 and prints the addition of i and j i.e, 3  and  at last loop of j the value of j reaches to 5 but still the value of i is 1 and prints the addition of i and j,  i.e, 6.  When the condition gets false,  steps goes outside the loop and it meets the statement that prints next line.  Since,  the  condition of   loop   of  first loop of i is not false, the loops  begins  agai.  This  time the value of i is 2 and passes inside th...

Program to print odd numbers less than 20 using for loop.

Image
Explanation, Here, i is initialized with value and in every loop, value in i increase by 2 until the condition gets false. Output Exercise: Print even number less than 30 using for loop.

Program to print multiplication table of given number

Image
Explanation In this program, n stores a number and i is an increment. The loop start from 1 and ends on 10. Inside printf, the format %dx%d=%d, here %d are replaced by the value of n which is constant, i that is incremented in every loop and n*i which yields product of n and i. Here, x remain same till the program that makes a multiple sense in the output.

Program to print sum of first ten natural numbers

Image
Explanation Here the variable s denotes to sum and is initialized with value 0. When the loop begins, the s adds the value of i with its previous values along with the loop.When the loop terminate, the last sum is stored in s and is printed outside of the loop. Output

Program to print 'n' numbers using for loop

Image
Explanation Here, in the program, two variables are used n and i . n stores number inputted by user and "i" is used as loop. As the loop increment it will print the value of i until it becomes greater than number stored in n . Output Exercise Make a program that will print "*" for n times using for loop.

Program to print first 9 natural numbers using For loop

Image
Here, I have used Dev C++ platform, it is easy to download. In this program, integer "i" is declared. For Loop has been used initiating i from 1 and print the value of i in each loop increasing value of i by 1 until given condition becomes false ie, i<10. Output Exercise Print natural numbers from 10 to 30 using For Loop. Give your answer in comment section.

Goto Statement or Jump statement in C

Image
T he goto statement is also known as jump statement that makes a program procedure to jump any where from anywhere within the program. It is sometime also called unconditional jump statement.  In this program, it takes name of a person and then prints it. Here " jump: " is a label that goto statement will transfer the step when encountered. So, after printing the name, it asks to continue and if a user enter y or Y the goto statement transfer the step to that label and if any other key is pressed the program will print Visit Again.

Break and Continue in C

Image
B reak statement when encountered in the loop it immediately break the loop and program resumes at the line following the loop. It also terminate the case statement in switch. For example, if we are writing a program to search a name among 1000 of given names, we may have applied loop for this program, . But if the program finds it in 10th iteration and at this point, we don't need to travel for remaining 990 iteration, we just need to terminate the loop and resume the program outside the loop. For this, only break statement is useful.The break statement is used in conjunction with condition. For example, In this program, i increases by 1 and when it encounters with number 5, it gets broken and the loop gets terminated.And thus, the output will be 1 2 3 4 C ontinue statement is used to omit a particular statement and proceed to next iteration. The continue statement escape the statement and move to next iteration. For example, if we have to escape 10th number from 1000...

Looping

Image
L oop is used in programming to execute any instruction repeatedly until a condition is satisfied. The computer has ability to perform a set of instructions repeatedly. This involves repetition of some portion of a program either for a specified number of times or till a given condition is satisfied. This repetition operation is known as looping. Purpose of Looping To execute statements for a specified number of times. To use a sequence of values. Types of Loop in C programming While Loop do while Loop For loop While Loop It execute one or more statements while the given condition remains true. It is useful when number of iterations is unknown. It is also called entry control or pre test loop. fig: Flowchart of while loop do while Loop do while loops are useful where loop is to be executed at least once. In do while loop condition comes after the body of loop. The loop executes one or more statements while the given condition is true. It is an exit control or post...

Program to print name of a day using switch case statement.

Image
  Explanation: Here number is inputted in variable 'day' and when it is equivalent with day in week and matches the preceeding case, it will print whatever in the block given.  

Program to check whether a entered letter is vowel or not using switch case statement

Image
Explanation Here Switch case plays a role of nested if else statement. It is an alternative way of if else statement. In this program, a variable c is declared to store alphabet entered by user. And now case is applied, if the given letter matches to the case then it enters to its block and print the given statement and then break the procedure. If the case is not matched it will check another case and if not matches all the cases then it will enter to in the default block. Output

Program to print division of a person.[Note: A person gets Distinction if s/he score 80% or above, First Division if s/he below 80% and above 60% , similarly 50% or above will be Second and if 32% and above will be Third division]

Image
E xplanation Here, per stores float percentage. It has been checked and if it is above 100 or below 31 it will print invalid that is stated in else statement at line 16. Nested if statement is used here so if the "per" value is  less or equal to 100 and if per is above 31 it enters inside and is checked with value 80,60,50 to print distinction, first and second division respectively and for third division,it will be sure if the above condition is false.

Program to find greatest number among any three different numbers inputted by User.

Image
  Explanation :Here three variables are declared a,b and c. The numbers in a,b and c are checked considering a is the greatest it is compared with b and c. If is greater in both condition, it will be greatest and if not, surely b or c will be greatest and are compared using else if. If b is greater than c then b will be greatest number and if it is also not then c is the greatest number as neither a nor b is greatest. Output Exercise : Make a program to state the smallest number among any three different inputted numbers.

Progarm to state that a person can cast vote or not. [Note: A person can cast vote if he is 18 years old or above]

Image
  Explanation, Here name is string so as it is defined as character of array 50. The specifier is %s is used to take scanf and value in age is checked with 18 and if it is equal to 18 or greater than this s/he can cast vote and if it is less than 18 and s/he cannot vote.   Exercise:  Make a program that can say he is old or yound. [Note if s/he is greater than age 40 than he is old and if he less than 40 then he is young.]

Program to find the greater number from two different numbers.

Image
Here, two numbers are requested from the user and placed in 'a' and 'b' variable using scanf. The value of the variable are checked using 'if' structure. If value in a is greater than value of a will be displayed as greater else value of b is displayed as greater number in comparision. Output

Program to read degrees in Fahrenheit and convert it into degree.

Image
Explanation Here, user input degree in Fahrenheit and we have used formula of degree Celsius to calculate and the result is placed in variable c. Output     Exercise: Convert degree in Celsius to degree Fahrenheit. 

Program To convert inputted time in minutes to Hours and minutes

Image
Explanation This is the program that user input time in minutes and the program convert it to hours and minutes. Here user input time in minutes and it resides in variable min using scanf. The inputted value is divide by 60(as 60 is equal to 1 hour) which yields integer value and discard value after decimal and put it into variable h, and then the same 'min' is now divided by 60 using modulus sign which yield remainder that become minutes and is now displayed using correct specifier in printf. Output   

Program to find area and perimeter of rectangle.

Image
Explanation Here, length and breadth of rectangle are inputted by user and placed in l and b using scanf. Using the correct formula of area and perimeter, we are able to generate the output. The area and perimeter anre float and the output should be of 2 decimal value, so we have used .2f  specifier. Output Here the input length and breadth is provided separated by comma as specifier used in scanf. Exercise: Find the area of circle where PI=3.1458.