Program in C
Program in C
! \ n ) ; 2 write a program to accept a string and an integer, and print the string as many times as the value of the integer this procedure should continue until the user presses q to quit ? 3 Explain the difference between while and do while loops with respect to the minimum number of times the body of the loop is executed. 4 write a program that accepts a number from 0 to 9 along with a string to be displayed a specified number of times. Use the switch case construct. ? 5 write a program to accept characters until q is pressed Display each character immediately after it is accepted. Also store the characters in an array, to be displayed before the program terminates. 6 Explain the significance of the break statement in the switch case construct what would result in its absence? 7 write a program that accepts 0, 1, or 2 if 0 is entered by the user, accept the necessary parameters (radius height, etc.) to calculate the volume of a cylinder inputs 1 and 2 correspond to cylinder and cone, respectively. Note that the above process must go on until the user enters q to terminate the program [Hint: use a switch case construct within a while or do while loop] 8 when is the default keyword useful in a switch case construct? Modify the program written for question 6 so that an error message is displayed whenever an input other than 0, 1 or 2 is entered. 9 modify the sample programs given in this chapter by replacing a while loop by a for loop and vice- versa? 10 write a program to generate the first hundred tems of the Fibonacci series and print their sum and average?
11why do you think the use of the go to statement is generally discouraged? Under what conditions are they specially useful? 12 Write a program that determines whether or not the input string is a palindrome. 13write a program to input a string, sum up the ASCII values of all its character, and output the resulting value. Use a single loop? (Hint : use the getchar function to read the input character by character and test for for the newline) 14 what is the output of the following statements: for ( I = 0; I < 10 ; I ++); printf ( % d / n , I ); ( Hint : notice the semicolon at the end of the for construct) 15 write the output of the following statements: for ( i = 10; i ++; i < 100) printf ( % d / n , i ); ( To understand the output better, make i an unsigned char) 16 Explain the role of the initialization, test and update expressions in a for loop. When are each of them executed? 17 Explain the difference between a break statement and a continue statement with examples. 18 what are the types of constants that can be used in a switch case construct? 19 following is a while construct to input a series of numbers , and exit the loop when the sum of the numbers entered so far is divisib by 5. int i, sum = 0; write (1) /* In finite loop * / scanf ( % d , & i ); sum + = i ;
if (sum % 5 = = 0) break ; } Rewrite the statements using a do while construct. Do not use the break statement. Dose it make any difference if you input all numbers on a single line separated by spaces or input them by pressing the enter key after each number? 20A break statement is used to exit a loop or a switch case structure. What happens if the break Statement is used outside any such construct? Differentiate between the break and continue statement ?
Exercises 9 write a program using conditional operators to determine whether a year entered through the keyboard is a leap year or not. 10 the factorial of an integer n is the product of consecutive integer from 1 to n. that is factorial n = n ! = n x ( n-1) x (n-3) xx 3 x2 x1. write a C program to find the factorial value of n. 11 write a C program to print the quotient of an integer number without using / . 12 write a program to print all the even and odd numbers of a certain range as indicated by the user. 13 write a C program to convert the binary equivalent of an integer number without using array. 14 write a C program to find the prime factors of a number given by the user.
15 write a C program to check whether a number is a power of 2 or not. 16write a program to find the GCD of two numbers. 17 write a program to find the sum of digits of a number given by the user. 18 write a C program to calculate the sum of prime numbers in a range. 19 write a C program to print the sum of the following series of up to n terms where n is given by the user. 20 write a C program to print the sum of the following series up to n terms where n is given by the user. x x 3 / 3! + x 5/5! - ( the value of x is given by the user.) 21 write a C program to print the following series : 0 11 235 58 13 . The number of terms to be printed should be given by the user. 22 write a C program to print the numbers that do not appear in the Fibonacci series the number of such terms to be printed should be given by the user. 23 write a C program to convert a decimal number into any base. 24 write a C program to check whether a number is a Krishnamurthy number or not A Krishnamurthy number is one whose sum of factorial of digits equals the number. 25 write a program to print the second largest number among a list of numbers without using array.
26 write program to print the sum of the following series ( with and without pow ( ) library function ). i) s = 1+ x + x2 + x3 + x4 + n terms ii) s = - x + x 2 x3 + x4 + . n terms iii) s = 1 + x + x2 /2! + x3 /3 ! + n terms iv) s = 1 + ( 1+2 ) + (1+ 2+3) + n terms v) s =1 x + x2/ 2! x3 /3 ! + n terms vi) s = x x3 /3 + x5 /5 x7/7 + n terms vii) s =2 + 22 + 222 + 2222 + n terms viii) s = 1 + x/4 + x2 /8 +n terms ix) s = x x2 /2 + x3 /3 x4 /4 n terms 27 write a program to print the prime numbers in a range. 28 Given a number, write a program using while loop to reverse the digits of the number. For example, the number 12345 should be written as 54321. 29 write a program to print the following triangle (i) * ** *** * * * * up to nth line
(ii) * ** *** * * * * up to nth line (iii) 1 12 123 1234 up to nth line (v) 1 22 333 4444 5 5 5 5 5 up to nth line 30 write a program to check whether a number is a prime number or not. 31 write a program to print all the prime numbers of a certain range given by the user. 32 write a program to print the floyd s triangle.
33. write a program to add the prime numbers of a certain range given by the user.
ARRAY PROGRAMMING
1). Write C program for the following a) Store a list of integer numbers in a array and print the following. (i) (ii) (iii) (iv) The maximum value the minimum value the range Hint this is computed as maximum minimum The average value
Hint To compute this, add all the numbers together into sum and count them all in count. The average is sum /count. b) swap the kth and (k+1) th elements in an integer array . k is given by the user ( c) find the binary equivalent of an integer number using array. ( d) find similar elements in an array and compute the number of times they occur.
(e) Find the intersection of two sets of numbers. (f ) Enter n numbers and store in an array and rearrange the array in the reveres order. (g) Sort the numbers stored in an array in descending order. (h) Arrange the numbers stored in an array in such a way that the array will have the odd numbers followed by the even numbers. (i) Find the frequency of digits in a set of numbers. (j) Remove the duplicates from an array. (k) Merge two sorted arrays into another array in a sorted order. (I) compare two arrays containing two sets of numbers. (m) Rearrange an array in reverse order without using a second array . 2) . Write a C program to read a text and count all the occurrences of a particular letter given by the user. 17. write a C program that will capitalize all the letters of a string. 18. Write a C program to check whether a string given by the user is a palindrome or not.
19. Write a C program that counts the total numbers of vowels and their frequency. 20. Write a C program to remove the white space (blank space) from a string. 21. Write a C program to print a sub string within a string 22. Write a C program that will read a word and rewrite it in alphabetical order. 23 Write a C program that deletes a word from a sentence. Note that the word may appear any number of times. 24. Write a C program that will analyze a line of text and will print the number of words, the number of consonants, and the number of vowels in the next. 25. Write a C program to find a string within a sentence and replace it with another string. 26. Write a C program that will insert a word before a particular word of a sentence. 27. Write a C program that takes the name of a person as input and prints the name in an abbreviated fashion e. g., manas ghosh as M.G. 28. Write a C program that reads in a string such as 20C 15F and outputs the temperature to the nearest degree using the other scale. 29. Write a C program that takes the name of a person as input and prints the first letter of the first name and middle name (if
any), and the title as it is, e.g. , Raj kumar santoshi as R.K. santoshi. 30. write a C program that reads a line of text and counts all occurrences of a particular word. 31. Write a program to convert each character of a string into the next alphabet and print the string. 32. Write a program that accepts a word from the user and prints it in the following way. For example, if the word is COMPUTER, the program will print it as C CO COM COMP COMPU COMPUT COMPUTE COMPUTER