C - Family Computers Index This pdf s uitable for hard copy (print copy for 2 - sides ) For Computer Pro gramming Learners Designed in C - Language C Programs List By C - Family C omputers , Vijayawada. C - Family Computers Index S.NO CHAPTER PAGE Problems 1 Basic Programs 1 35 2 If - Else programs 9 44 3 Loops 17 104 4 Nested Loops 37 50 5 Arrays 43 36 6 Functions 51 25 7 Recursion 59 44 8 Pointers 71 30 9 Strings 83 50 10 Structures 93 15 11 Files 101 12 On Behalf of C - Family Computers, Vijayawada, India, pin:520010, Ph: 9440 - 030405; alt: 8500 - 117118, For feedback: srihari.vijayawada@gmail.com For self - practice exercises on C language programming, around 500 simple exercises are given to the beginners or level - 1 programmers. C-Family Computers 1 chapter-1 (Basic Programs) * the division 20/8 gives 2, but not 2.5, whereas 20.0/8 gives 2.5 * if two values are integer then C-language gives only integer part of result and fraction is omitted * the division int/int gives result as int, whereas float/int gives float value result * output type of any expression is highest type operand in the expression. For example, if ‘x’ is int -type and ‘y’ is float-type then ‘ x+y ’ gives float-type result value. Following programs are basic level programs, so do not use any control structures like if, if-else, while-loop, for-loop , arrays,... etc. --------------------------------------------------------------------------------------------------------------------------------------------------- 1) write a program to accept a value (N) from keyboard and print its opposite sign value. Process: read N from keyboard, multiply it with -1 to get opposite sign value and print on the screen. ip: 19 ip: -19 op: -19 op: 19 --------------------------------------------------------------------------------------------------------------------------------------------------- 2) code to accept a number N from keyboard and print N for 3 times, but the next value of N is double of the previous value. Note: Try without using multiplication operator and using only single variable. ip: 6 ip: 100 op: 6, 12, 24 op: 100, 200, 400 Hint:we can solve this problem by writing three printf() statements or using single printf(), for doubling n=n+n --------------------------------------------------------------------------------------------------------------------------------------------------- 3) code to print N-5, N, N+5 (3 values) where N is input. (do with single variable N in the program, do not take two or more variables) Hint: we can solve problem using single printf() or three printf() statements. ip: N=26 op: 21, 26, 31 --------------------------------------------------------------------------------------------------------------------------------------------------- 4) code to print 3x 2 +2x+1 value for 3 times, where ‘x’ is input from keyboard for first time, for 2 nd & 3 rd time, the x value is double of previous value.(2x, 4x). Note: try using two variables. op: 6 (if x is 1) 17 (when x becomes 2x) 57 (when 2x becomes 4x) 209 (when 4x becomes 8x) ... ---------------------------------------------------------------------------------------------------------------------------------------------------- 5) code to print 3x+2 value for 3 times, for first time, the ‘x’ values is input, for second or third time, the ‘x’ value is output of previous sum. output1=3*x+2, output2=3*output1+2, output3=3*output2+2 Note: Try using single variable ‘x’ in the progra m. ip: x=1 ip: x=5 op: 5, 17, 53. op: 17, 53, 161 --------------------------------------------------------------------------------------------------------------------------------------------------- 6) code to accept a number N from keyboard and print N for 3 times, but the next value of N is half of the previous value. Note: use the number 2, and do not use any other number. ip: 64 ip: 100 ip: 70 op: 64, 32, 16 op: 50, 25, 12 op: 70, 35, 17 Hint: we can solve this problem using single printf() or three printf() statements. --------------------------------------------------------------------------------------------------------------------------------------------------- C-Family Computers 2 chapter-1 (Basic Programs) 7) code to accept two numbers as numerator & denominator and print remainder without using modulus operator (%). For example, if input is 22 & 4 then remainder is 2. Logic: use operators *, - and / Hint: 22/4 gives result 5 (not 5.5) The purpose of this program to teach you to * the division int/int gives result int, whereas float/int gives float. * output type of any sum is highest type operand in the expression. For example, if ‘x’ is int - type and ‘y’ is float- type then ‘x+y’ gives float-type result value. --------------------------------------------------------------------------------------------------------------------------------------------------- 8) code to accept radius of circle from keyboard and print area and perimeter ip: radius: 5 op: area =78.5 perimeter=31.4 Process: Area of circle is “pie*radius*radius”, perimeter of circle is “2*pie*radius” Note: The symbol ‘pie’ or ‘pie value’ is not available or not known to C-language, so use the constant value 3.14 (22/7) in the equation, for example: “area=3.14*radius*radius” Algorithm: step1: scan ‘radius’ as input step2: find ‘ area ’ // area=3.14*radius*radius step3: find ‘ perimeter ’ // perimeter=2*3.14*radius step4: print ‘ area ’ and ‘ perimeter ’ step5: stop. --------------------------------------------------------------------------------------------------------------------------------------------------- -9) code to accept area of circle and find radius (input is area, output is radius) ip: area: 78.5, op: radius of circle is: 5 Process: radius = sqrt(area/3.14) Note: use sqrt() function to get square root, also add header file “#include<math.h>” to the program --------------------------------------------------------------------------------------------------------------------------------------------------- -10) code to find Fahrenheit from a given Celsius temperature ip: Celsius : 34 op: Fahrenheit : 93.2 Process: Celsius to Fahrenheit: F (C × 9/5) + 32 --------------------------------------------------------------------------------------------------------------------------------------------------- -11) code to find simple interest(si) from a given principle, rate of interest and time ip: say principle is 1000, rate of interest is 2.00, time is 12 (12months) op: simple interest = 240.00 Process: step1: scan( p, t, r ) as input values. step2: calculate si=p*t*r/100. step3: print( si ) --------------------------------------------------------------------------------------------------------------------------------------------------- 12) code to accept employee basic salary from keyboard and print net salary as shown below. (hra HouseRentAllowence; da DearnessAllowence, ta TravellingAllowence) hra 24.3% on basic salary da 8.5% on basic salary ta 10% on basic salary net salary = basic salary + hra + da + ta; C-Family Computers 3 chapter-1 (Basic Programs) Algorithm: step1: take basicSalary, netSalary, HRA, DA, TA as variables step2: read basicSalary as input step3: calculate hra // hra=24.3*basicSalary/100 step4: calcucate da // da=8.5*basicSalary/100 step5: calcucate ta // ta =10*basicSalary/100 step6: find netSalary // netSalary=basicSalary+hra+da+ta step7: print netSalary step8: stop --------------------------------------------------------------------------------------------------------------------------------------------------- *13) code to accept two values into variables (x, y) and print after swapping(exchanging) them, use third variable while swapping x , y. ip: 12 34 op: 34 12 Algorithm: step1. take variables x , y for input // for swapping take extra variable ‘t emp ’ (temporary/supporting variable) step2. scan x ,y step3. s tore x to ‘temp’ step4. store y to x step5. s tore ‘temp’ to y step6. print(x, y) step7. Stop Method2: without using ‘temp’ variable subtract one with another value (x=x-y), using this difference, we can swap x, y values. For example Let x=10, y= 4 x=x-y; // now x is 6 y=x+y; // y got 10, this is value of x x=y-x; // x got 4, this is value of y This logic works for any values of x , y -------------------------------------------------------------------------------------------------------------------------------------------------- *14) If 3 values entered through keyboard, write a program to find sum and average of them. Note: take only two variables in the program (let ‘ N ’ , ‘sum’ are variables) ip: enter value1: 10 enter value2: 20 enter value3: 30 op: sum=60, average=20 Process: read 3 values one by one into ‘ N ’ using three scanf() statements, after scanning every single value add it to ‘sum’ , a lgorithm is as follows: step1: read first input into N step2: save N value to sum. (store N to ‘ sum ’ ) step3: read second input into N step4: add N to ‘ sum ’ // ‘sum’ already contained first value, so add second input N to sum. step5: read third input into N step6: add N to ‘ sum ’ step7: print(sum, sum/3) step7: stop -------------------------------------------------------------------------------------------------------------------------------------------------- C-Family Computers 4 chapter-1 (Basic Programs) 15) The BigBite shop owner priced 3/- per each egg and also giving bonus eggs as given below Bonus1) one extra bonus egg for every 5 eggs purchased, if customer purchased 15 eggs then he get 3 extra eggs as bonus1, if he purchased 35 eggs then he get 7 eggs). Bonus2) again two more extra bonus eggs, if customer purchased 100 eggs as one lot, that is, for every 100 eggs he get 2 eggs. For example, if he purchased 100 eggs then customer get 2 eggs as bonus2. If he purchased 230 then he gets 4 eggs. Here the input of the program is number of eggs wanted by customer and output is pay Amount and number of eggs to be delivered to the customer. ip: 120 (N=120, no.of eggs wanted by customer) op: total cost of 120 eggs is: 360rs/- total eggs delivered: 120+24+2 146 ( N + bonus1 + bonus2 ) take variables names as: N, pay, totalEggsDelivered. --------------------------------------------------------------------------------------------------------------------------------------------------- -16) A fruit seller told 5 apples cost is 65rs/- and the customer wanted to buy apples for 100rs/-. Here each apple costs 13rs/-, and he can get nearly 7 apples for 100rs/- and change return is 9rs/- Now generalize this problem by writing a program, for example, if N apples cost is C, then what is the cost of each apple and how many apples customer can get for amount M, tell if any change to be returned. List of variables names: input: N no.of apples, C Cost of N apples, M Amount the customer can spent (buying amount). output: eachAppleCost cost of each apples, countOfApplesCustomerGainedforM total no.of apples customer can get nearly for amount M, changeReturn change to be returned to the customer. ip: N=5, C=68rs, M=1000rs op: each apple cost is 13.60rs he can get 73 apples for 1000rs- he get back 8rs/- change roughly. Note: use format string “%.2f” in printf() statement, for example printf(“%.2f”, 23.123456) 23.12 if you use type-casting/type-conversion concept we can get result accurately. Example for type-casting: (int)10.45 = 10, (float)14= 14.00, let a=45.67 then (int)a=45 --------------------------------------------------------------------------------------------------------------------------------------------------- *17) Code to accept a time in H : M : S format as input and print output in seconds format. Hint: Here we have to scan time like 3 values, for example: scanf(“%d%d%d”, & H, &M, &S); ip: 2 40 50 (2-hr , 40-min , 50-sec) ip: 0 2 50 (0-hr , 2-min, 50-sec) op: 9650 seconds op: 170 seconds Logic: each minutes has 60 seconds each hour has 3600 seconds variable list: H hours, M minutes, S seconds, N output value of seconds (take N as long int ) N = H*3600L + M*60 + S; Note: 3600 takes as int value, but 3600L takes as long int value. ------------------------------------------------------------------------------------------------------------------------------------------------- *18) Code to accept a time(N) in seconds format and print output in time format (H:M:S format) ip: 7270 op: 2-hours, 1-minutes, 10-seconds Logic: divide N with 3600 and take the quotient as hours, (1hour=3600seconds) divide N with 3600 and take the remainder(R). This R is the remaining seconds left after taking hours from N. Again divide R with 60 and collect quotient and remainder. The quotient would be minutes and remainder would be seconds. -------------------------------------------------------------------------------------------------------------------------------------------------- C-Family Computers 5 chapter-1 (Basic Programs) 19) Code to accept a sample time and increment it by N seconds, here N is also an input. ip: 12 59 58 (sample time taken from keyboard : Hours=12, Min=59, Seconds=58) 124 (sample no.of seconds to increment, N=124) op: 13hr 2min 2sec (time after incrementing N seconds) Method1: 1. take variables H,M,S and N as variables 2. scan(H,M,S and N) values from keyboard. 3. add N to S (to increment the given time by N seconds) 4. Now S may go out of 59 seconds, so takeout minutes from S by dividing S with 60 (S/60) add this quotient(minutes) to M, later assign remaining seconds to S (S=S%60) 5: Now M may go out of 59 minutes, so do as above said. Method2: 1. Take variables H,M,S and N as variables 2. Convert total time into seconds (k). 3. Now add N with total time of seconds (K). 4. Now distribute this K into H,M,S as said in above problem (this method2 is simple than method1) ( try using these two methods ) -------------------------------------------------------------------------------------------------------------------------------------------------- *20) Code to accept two shifts of working time of an employee and find total time worked in two shifts. ip: 12 50 58 (shift-1 worked duration : Hours=12, Min=50, Seconds=58 ) 2 53 55 (shift-2 worked duration : H= 2, M=53, S=55 ) op: 15hr 44min 53sec (total duration worked in two shifts) -------------------------------------------------------------------------------------------------------------------------------------------------- *21) Code to accept only two digit number(N) like 47 and print sum of digits (4+7) ip: 47 ip: 84 op: 4+7 11 op: 8+4 12 Logic: divide the input N( Let N=47) with 10, collect remainder & quotient . The quotient will be 4 and remainder will be 7. This is as shown below, -------------------------------------------------------------------------------------------------------------------------------------------------- 22) Code to accept only two digit number(N) like 47 and print reverse of it (74) ip: 47 ip: 84 op: 74 op: 48 if input is 47 then output generated as 7 * 10 + 4 74 -------------------------------------------------------------------------------------------------------------------------------------------------- 10) 47 (4 quotient 40 --------------- 7 remainder N/10 47/10 4 N%10 47%10 7 now do sum=N/10+N%10; // sum=4+7 C-Family Computers 6 chapter-1 (Basic Programs) *23) Code to accept 3-digit number(N) like 247 and print sum of all digits 2+4+7, following text may help you Let N=2345 2345%10 gives 5 as remainder 2345/10 gives 234 as quotient 2345%100 gives 45 as remainder 2345/100 gives 23 as quotient 2345%1000 gives 345 as remainder 2345/1000 gives 2 as quotient 2345%10000 gives 2345 as remainder 2345/1000 gives 0 as quotient Hint1: N=247 * N%10 247%10 7 * (N%100)/10 (247%100)/10 (47)/10 4 OR (N/10)%10 (247/10)%10 (24)%10 4 * N/100 2 -------------------------------------------------------------------------------------------------------------------------------------------------- 24) Code to accept a number and print its boundaries surrounded by ten multiples eg1) if input is 34 t hen print output as “surrounded by 30 - 40” eg2) if input is 89 then print output as “surrounded by 80 - 90” Hint: 34/10 3 (not 3.4), so use formula 34/10*10 30 --------------------------------------------------------------------------------------------------------------------------------------------------- -25) Code to accept a number (N) and print next nearest divisible of 5. eg1) if input is 34 then next nearest 5 divisible is 35 eg2) if input is 46 then next nearest 5 divisible is 50 eg3) if input is 40 then next nearest 5 divisible is 40 (not 45) Logic: divide the input N with 5 and take the remainder, with the help of remainder we can solve it. --------------------------------------------------------------------------------------------------------------------------------------------------- 26) If a four-digit number is input through the keyboard, write a program to obtain the sum of the first and last digits of input. ip: 4567 ip: 1234 ip: 2000 ip: 4+7=11 op: 1+4 5 op: 2+0 2 --------------------------------------------------------------------------------------------------------------------------------------------------- 27) If a four-digit number is input through the keyboard, write a program to print middle two-digits ip: 4567 ip: 1234 ip: 56 op: 23 --------------------------------------------------------------------------------------------------------------------------------------------------- 28) If a four-digit number(N) is input through the keyboard, write a program to swap first and last digit and print them. (Let last digit of N is not zero) ip: 3456 ip: 3778 op: 6453 op: 8773 -------------------------------------------------------------------------------------------------------------------------------------------------- C-Family Computers 7 chapter-1 (Basic Programs) *29) Write a program to accept 4-digit single number from keyboard and print sum of all digits ip: 4567 op: 4+5+6+7 step1: Divide N with 10 and take the remainder, the remainder is always last-digit when a number divide with 10, here for N=4567, the remainder is 7, add this 7 to vari able ‘sum’. step2: To get next digit(6), now remove current last-digit 7 from N by doing N=N/10. after N=N/10, the N value becomes 4567 to 456 step3: Again divide N with 10, and take the last- digit 6 as remainder, add this 6 to ‘sum’. Repeat this process for 4 times. We can do above program in other method as sum = ( n%10 ) + ( n/10%10 ) + ( n/100%10 ) + ( n/1000 ) sum = ( 7 ) + ( 6 ) + ( 5 ) + ( 4 ) ------------------------------------------------------------------------------------------------------------------------------------------- 30) Write a program to accept 4-digit single number from keyboard and print reverse of it ip: 4567 op: 7654 Logic: like previous program logic, but replace “sum=sum+n%10” with “sum=sum*10+n%10” to get reverse value (initially sum=0). sum = sum * 10 + n%10 = 0 * 10 + 7 7 = 7 * 10 + 6 76 = 76 * 10 + 5 765 = 765 * 10 + 4 7654 = 7654 ------------------------------------------------------------------------------------------------------------------------------------------ *31) Write a program to accept 4-digit binary value N and print equaling decimal value. ip: 1101 op: (1*2 3 ) + (1*2 2 ) + (0*2 1 ) + (1*2 0 ) 8 + 4 + 0 + 1 13 step1: divide N with 10 and get last digit as remainder, here remainder of 1101 is (1) step2: multiply this remainder ‘1’ with 2 0 and add to ‘sum’ . Like sum=sum + (1*2 0 ) step3: to get next-digit of N, remove current last-digit from N, by doing N=N/10, [1101/10 110] step4: repeat these steps for 4 times. We can’t take or type values 2 0 , 2 1 , 2 2 , 2 3 , 2 4 ... directly in the program, so take these values as 1, 2, 4, 8, 16,.... ------------------------------------------------------------------------------------------------------------------------------------------- Let us see, how digits are added to ‘sum’. Initially, N=4567, sum=0 sum = sum + N%10 N=N/10 step1: sum = 0 + 4567%10 0 + 7 7 N=4567/10 456 step2: sum = 7 + 456%10 7 + 6 13 N=456/10 45 step3: sum = 13 + 45%10 13+5 18 N=45/10 4 step4: sum = 18 + 4%10 18+4 22 N=4/10 0 sum = 1 8 + 4 finally sum=22 C-Family Computers 8 chapter-1 (Basic Programs) *32) Write a program to find binary number from given decimal number (Let the input is below 15) step1: divide N with 2 and take the remainder(R) step2: now multiply remainder(R) with 10 0 and add to variable ‘sum’ * sum=sum+ R*10 0 ] step3: now cut down N to N/2 as shown in the picture. [ 13/2 6 ] step3: repeat above steps for 4 times. [ here for 10 0 , 10 1 ,10 2 ... take 1, 10, 100 ... ] -------------------------------------------------------------------------------------------------------------------------------------------------- 33) If a four-digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits. If the number 2391 then the output should be displayed as 3502 ------------------------------------------------------------------------------------------------------------------------------------------------- -34) If a four-digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits. For example if the number 2391 then the output should be displayed as 3402 (do not forward ‘ carry ’ to next digits when summing) logic: it is little bit difficult but possible, to stop carry to the next digit, take each digit and increment by 1 and then divide the digit with 10 and get remainder, using these remainders form the output number. ------------------------------------------------------------------------------------------------------------------------------------------------- -35) Following program prints given single digit in English words, for example void main() { char *arr *+=,“zero”, “one”, “two”, ... “nine”- printf(“output is : %s ”, arr[2] ); // two printf(“ \noutput is: %s ”, arr[9] ); // nine } op: output is : two output is : nine Now try to extend this program to print 4-digit number. ip: 3456 op: three four five six ------------------------------------------------------------------------------------------------------------------------------------------------ 2 13 2 6 - 1 2 3 - 0 2 1 - 1 0 - 1 10 3 *1 + 1 0 2 *1 + 10 1 *0 + 10 0 *1 1101 C-Family Computers 9 if-else Programs 01) If integer, N is input through keyboard, write a program to print its absolute value [ mod(N) ] the input number may be +ve/ – ve entered by user, but the output should be +ve. Method : if input is – ve then convert into +ve by multiplying it with -1. // If(N<0) then N is -Ve; ip: – 12 ip: 14 op: 12 op: 14 ---------------------------------------------------------------------------------------------------------------------------------------------------- 02) If two integers are input through keyboard, write a program to find difference b/w them. The output difference must be +ve. Method1: subtract one with another value, if difference is – ve, then convert into +ve by multiplying with -1. Method2: subtract small from bigger value. ip: 12 18 ip: 18 12 op: 6 op: 6 --------------------------------------------------------------------------------------------------------------------------------------------------- 03) if two integers are input through keyboard, write a program to find biggest among them. ip: 12 45 ip: 15 53 op: 45 op: 53 ---------------------------------------------------------------------------------------------------------------------------------------------------- 04) If basic salary of employee scanned through keyboard, find net salary as given below If basic salary <= 3000 then DA is 5% on basic [ DA means Dearness Allowance] TA is 9% on basic [ TA means Travelling Allowance] If basic salary > 3000 then DA is 9% on basic TA is 12% on basic HRA(house rent allowance) is 24%, this is common for both below & above 3000 salaried employees. Now find net salary as sum of all allowances [net salary = basic salary + hra + da + ta] ---------------------------------------------------------------------------------------------------------------------------------------------------- 05) Program to find given number N is an odd or even. Method : divide the input number with 2, and check the remainder, if remainder is zero then print as “even” otherwise “odd”. (Use % operator to get remainder). ip: 45 ip: 44 op: odd op: even ---------------------------------------------------------------------------------------------------------------------------------------------------- 06) Program to accept a single number(N), the number may have 2 or 3 digits, print its reverse. ip: 234 ip: 27 op: 432 op: 72 logic: if N<100 then it is 2-digit number or else it is 3-digit number. ---------------------------------------------------------------------------------------------------------------------------------------------------- 07) Program to accept a single number, the number may have 2 or 3 digits, then find the number and its reverse are equal or not? ip: 234 ip: 272 ip: 44 op: not equal op: equal op: equal ---------------------------------------------------------------------------------------------------------------------------------------------------- C-Family Computers 10 if-else Programs 08) Accept a value from keyboard and find whether it is +ve/ – ve/zero ip: 12 ip: -12 ip: 0 op: +ve op: -ve op: zero method1 : try without using ‘else’ keyword (write 3 independent if -statement) method2 : try using normal nested-if style method3 : try using if-else-if ladder style ---------------------------------------------------------------------------------------------------------------------------------------------------- 09) Code to accept salary from keyboard and find tax. if salary<=10000 then tax is zero if salary >10000 and <=20000 then tax is 5% on salary if salary >20000 then tax is 8% on salary. ip: enter salary: 9000 ip: enter salary: 20000 ip: enter salary:42000 op: tax = 0/- op: tax = 1000/- op: tax = 3,360/- method1 : try without using ‘else’ keyword (write 3 independent if -statement) method2 : try using normal nested-if style and as well as ladder-style --------------------------------------------------------------------------------------------------------------------------------------------------- 10) Code to accept salary from keyboard and find tax, tax is 5% on salary but minimum tax is rs:200/- For ex, if salary is 20,000/- then 5% tax is 1000/-, this is greater than minimum tax 200, so no change in tax If salary is 1000/- then 5% tax is 50/- , but we have to collect 200/- as minimum, so change tax value to 200/- Method: first calculate tax as 5% on salary, if tax is <200 then replace tax=200 as minimum. ip: salary: 20000 ip: salary: 1000 op: tax=1000/- op: tax=200/- --------------------------------------------------------------------------------------------------------------------------------------------------- 11) if three integers are input through keyboard, then find how many – ve values exist. ip: -12 34 -42 ip: 52 64 -72 ip: 62 44 42 op: count=2 op: count=1 op: count=0 ---------------------------------------------------------------------------------------------------------------------------------------------------- 12) if three integers are input through keyboard, then find at least one value is – ve or not? ip: -12 34 -42 ip: 52 64 -72 ip: 62 44 42 op: “yes, –ve exist” op: “yes, –ve exist” op: “no, –ve is not exist” method-1) find using logical operators method-2) find without using logical operators (ladder style) method- 3) find without using logical operators and else keyword (use ‘bool' logic) ---------------------------------------------------------------------------------------------------------------------------------------------------- 13) Code to accept 3 values from keyboard, here some values are +ve/-ve entered by the user, later find sum & product of +ve values only. (don’t take zero neither +ve nor – ve) ip: -2 3 4 op: sum of +ve: 3+4 7 product of +ve: 3*4 12 --------------------------------------------------------------------------------------------------------------------------------------------------- 14) Code to find given input number (N) is in b/w 10 and 20 or not? ip: 12 ip: 25 op: yes, it is op: no, it is not method1: using logical operators method2: without using logical operators (nested-if) method3: without using logical operators and ‘else’ keyword. (use ‘bool’ logic) --------------------------------------------------------------------------------------------------------------------------------------------------- C-Family Computers 11 if-else Programs 15) A number N which is b/w 10-100 input through keyboard and find whether it is prime or not? case1: If the input N is not in limits of 10-100 then show an error message called “invalid input”. case2: If N is in limits then find prime-ness by dividing N with 2,3,5,7. If N is divided with any of these numbers then it is said to be not prime , or else prime (Try with and without using logical operators) Note: Prime numbers divide only with 1 and itself, i.e., it does not divide with any other number such as 2,3,4,5,6,7,8,....N - 1. If not divided with these numbers then we can say it is ‘prime’. Logic: if not divided with 2 then it will not be divided with 4,6, 8,10...(all 2 multiples) similarly, if not divided with 3 then it will not be divided with 6,9,12,15...(all 3 multiples) So it is better check prime-ness with 2,3,5,7 for N below 100. ip: 17 ip: 15 ip: 97 op: yes, prime op: no, not prime op: prime ---------------------------------------------------------------------------------------------------------------------------------------------------- 16) The railway dept charges rs:2/- per every kilometer(km) travelled by passenger, and also giving discount. The input of program is distance in ‘ km ’ travelled by passenger and output is fare (amount to be charged). The discount is as follows If km <= 50 the discount is 0/ – If km > 50 the discount is 30% on above 50 kilometers travelled. for example, if km is 40 then: discount = 0 // here km<=50 if km is 90km then: discount = charge*(90-50)*30/100; // 30% discount on above 50 km travelled. fare= km*charge – discount The traveller has one more discount, if fare > 200 then he get 50% discount on above rs200/- amount. ip: km=40 ip: km=80 ip: 350 op: fare is 80/- op: fare is 142/- op: fare is 360/- ---------------------------------------------------------------------------------------------------------------------------------------------------- 17) Government providing nearly ticket free travelling for hill station by rope, for children whose age is <13 years, the ticket cost for <13 years people is 10/- fixed. If age>=13 then cost of ticket depends upon weight of person, the cost is 2/- per every 5 kilograms. Now write a program to scan age of person, if age<13 then then take cost as 10/- or else scan weight of a person and calculate cost as (weight/5)*2/- ---------------------------------------------------------------------------------------------------------------------------------------------------- 18) Write a program to find person is eligible for job or not? If age of a person>30 then he is eligible, if age <=30 then scan input for education years, if edu- years>=16 then say ‘eligible’ otherwise ‘not eligible’. ip: enter age: 49 ip: enter age: 23 ip: enter age: 29 op: eligible enter education years:17 enter education years: 10 op: eligible op: not eligible ---------------------------------------------------------------------------------------------------------------------------------------------------- 19) In Japan, population is diminishing, the government encouraging population by cutting down tax to zero those who have more than 1 child. Keep this in mind, write a program to accept employee salary and calculate tax, if salary <=20000 then tax is zero, or else 30% tax on above 20000/- salary. For example, if salary is 50000 then taxable salary is 50000-20000 30000. Method : For this program, first scan salary as input, if salary<20000 then set tax to zero, if salary > 20000 then scan input for no.of children he has, if children >=2 then set tax=0 or else calculate tax. For this program input is ‘salary’ and output is ‘tax’ ip: enter salary: 5000 ip: enter salary: 50000 ip: enter salary:50000 op: tax=0 enter no.of children:3 enter no.of children:1 op: tax=0 op: tax= 9000 --------------------------------------------------------------------------------------------------------------------------------------------------- C-Family Computers 12 if-else Programs 20) Any year is input through keyboard, determine whether the year is a leap-year or not. Method : if year is divisible by 4 then it is “leap year” otherwise “not a leap year”. ip: 2005 ip: 2008 op: Non Leap year op: Leap year ---------------------------------------------------------------------------------------------------------------------------------------------------- 21) Above program checks the leap-year with simple condition by 4, but for every 400 years one more extra leap year happened. Now find whether given year is leap-year or not? case1: If year divisible by 400 then said to be leap-year (eg:1600, 2000 are leap-years but not 1700,1800, 2100) case2: If year is divisible by 4 but not with 100 is also said to be leap-year, for eg:1996,2004,2008 case3: if above two cases are not satisfied then it is non-leap year. ip: 1800 ip: 1600 ip: 1400 op: non leap year op: leap year op: non leap year method1 : try using logical operators && and || method2 : try without using logical operators. (We have to use nested-if) ---------------------------------------------------------------------------------------------------------------------------------------------------- 22) If marks of a student are input from keyboard, write a program to print student is passed or failed; If student obtained>50 in all subjects then print “passed” or else “failed”. Logic: let student has 3 subjects; (Assume that 50 is the pass mark out of 100) 1) find using logical operators 2) find without using logical operators (use ‘nested - if’ keyword) 3) find without using logical operators and ‘else’ statement (use bool keyword) ip: 51 60 70 ip: 30 60 70 ip: 90 52 60 op: passed op: failed op: passed ---------------------------------------------------------------------------------------------------------------------------------------------------- 23) If marks of 2 subjects are input through a keyboard, write a program to print result. Logic: Generally, to get pass mark, student must obtain >= 50 marks in 2 subjects, but university gave an exemption to the students. If he got 10 marks less in any one subject out of 2 then he is also passed. That is, he must get 50 marks in one subject and 40 in other subject. ip: 70 46 ip: 77 66 ip: 45 45 ip: 46 59 ip: 70 74 op: passed op: passed op: failed op: passed op: passed method1: use logical operators method1: without using logical operators method1: use Boolean logic operators ---------------------------------------------------------------------------------------------------------------------------------------------------- 24) If marks of 3 subjects of a student are input through keyboard and find result if student obtained <35 in any one or more subjects then print “failed” Otherwise print “A -grade/B-grade/C- grade” based on average. If average >= 60 then print “ passed in A - grade” If average 50 to 60 then print “passed in B - grade” If average <50 then print “passed in C - grade” ip: 80 90 90 ip: 45 60 50 ip: 40 36 41 ip: 20 40 50 op: passed in A-Grade op: passed in B-Grade op: passed in C-Grade op: Failed ---------------------------------------------------------------------------------------------------------------------------------------------------- 25) If three integers are input through keyboard, find biggest among them. 1.use logical operators and ‘else’ keyword 2.use logical operat ors but do not use ‘else’ keyword ( write 3 separate if-statements) ---------------------------------------------------------------------------------------------------------------------------------------------------- C-Family Computers 13 if-else Programs 26) Find whether the given character is upper case alphabet or lower case alphabet or digit or any other character? ip: A ip: a ip:9 ip: & op: upper case alphabet op: lower case alphabet op: digit op: special symbol Character ASCII Values ----------------------------------------- A-Z 65-90 a-z 97-122 0-9 48-57 the sample code is: char ch; printf(“enter character :”); scanf(“%c”, &ch): if(ch>=’A’ && ch<=’Z’) or if(ch>=65 && ch<=90) pr intf(“upper case alphabet”); ------ ------ ---------------------------------------------------------------------------------------------------------------------------------------------------- 27) Write a program to scan an alphabet from keyboard and print its opposite case, if alphabet is not given then print such character as it is. ip: A ip: a ip: 8 op: a op: A op: 8 The ASCII values for A-Z is 65-90, a-z is 97-122, so the difference is 32. ‘A’+32 ’a’ and also ‘A’+1 ’B’ ‘a’ -32 ’A’ and also ‘B’+1 ’C’ --------------------------------------------------------------------------------------------------------------------------------------------------- *28) Write a program to accept 4 values from keyboard and print biggest. method1: solve in normal nested-if style. method2: solve in if-else-if ladder style. method3: solve in selection style ( use following steps ) 1) Let us take four variables A,B,C,D for input, also take X to store output of big value. 2) Let us assume A is big, so store A value to X 3) Now compare X with B, if B is bigger than X, then store B value to X ( replace A value ) 4) Now compare X with C, if C is bigger than X, then store C value to X ( later with D) 5) Finally, the big value in X, so print it -------------------------------------------------------------------------------------------------------------------------------------------------- 29) If N is input through keyboard (the input b/w 0 to 99999), write a program to find how many digits exist. ip: 234 ip: 3456 ip: 12234 ip: 3 op: 3 op: 4 op: 5 op: 1 method1: using ladder-style, for example, if(N<10) count=1; else if(N<100) count=2; .... method2: without using ‘else’ keyword (use logical operators , we get 5 independent if-statements) --------------------------------------------------------------------------------------------------------------------------------------------------- C-Family Computers 14 if-else Programs 30) A shop keeper sell each pen for 3/- cost and giving discount, calculate bill amount after discount. input is number of pens(N) purchased by customer and bill calculated as “bill=N*3” if( bill<=100) discount is 0% if( bill>100 && bill <=200) discount is 10% if( bill>200 && bill <=500) discount is 20% if( bill>500) discount is 30% finally bill is bill-discount; ---------------------------------------------------------------------------------------------------------------------------------------------------- *31) If two dates are input from keyboard, write a program to find latest date among them. Take input dates as 6 values (d1,m1,y1 as date1 ) and (d2,m2,y2 as date2) ip: 29-2-2012 30-2-2010 op: date-1 is latest Method1: first compare years, if( y1>y2) then say date-1 is latest, else if(y1<y2) then say date-2 is latest, if y1==y2 then compare months, if months equal then compare days. Method2: Compose date1 and date2 (3-values) into single value. (eg: k1=y1*10000+m1*100+d1) Now compare k1 and k2 and find latest (this is simple than method1) ------------------------------------------------------------------------------------------------------------------------------------------------- 32) The C-Family library charges a fine for every book late returned. For