Pages

C Programming language questions and Answers

C Programming language questions and Answers

Basic Programs

Q.    Write a program to print "Hello Word".


Q.    Write a program to declare a variable and print its Value.


Q.    Write a program to find:
    i.    Addition of two numbers.
    ii.    Subtraction of two numbers.
    iii.    Multiplication of two numbers.
    iv.    Division of two numbers.


Q. Write a C Program to do swapping of two numbers using third variable.


Q. Write a C Program to find area and circumference of circle.


Q.    Program to calculate area of a triangle. Base and height given by the user.


Q. Write a C program to calculate area of rectangle. Length and breadth given by the user.


Q. Write a C program to find sum and average of three real numbers.


Q.         Write a C program to exchange two numbers using temporary variable.


Q.         Write a program to simple interest, compound interest and amount.


Q.        Write a C program to exchange two numbers without using temporary variable.


Q.        Write a C Program to convert temperature from Fahrenheit to degree centigrade.
c=(f-32)*5/9


Q.         While purchasing certain items a discount of 10% is offered if the quantity purchased is more than 2000. Write a program to calculate the total expanses, if quantity and price per item are entered through keyboard.


Branching Programs

Q.    Write a C program to absolute value of a number given by the user.


Q. Write a C program to determine whether a number is even or odd.


Q.     Write a C program to find out whether a year is leap year or not.


Q.         Write a C program to to find greater between two numbers. The numbers are given by the user.


Q.         Write a C program to find out greatest of three numbers. The numbers are given by the user.


Q.         Write to calculate the gross salary of an employee of an organization with the conditions:
          i)    if the basic > 25000, DA will be 60%, and house rent is 10% 
          ii)    if the basic <= 25000 but >15000, DA will be 65%, and house rent is 15%
          iii)    if the basic <= 15000 , DA will be 70%, and house rent is 2000.
    [gross salary=basic+DA+house rent].



Q.         Write a program to find out the average marks of five subjects given by the user. Also find out the results based of the average marks obtained by the student based on the following conditions                               
    a. “Fail” if mark of any subject is less than 30.
    b. “Simple Pass” if average marks is less than 45 but greater than or equal to 30
    c. “Second Division” if average marks is less than 65 but greater than or equal to 45
    d. “First Division” if average marks is greater than or equal to 65.
    e. “Invalid” if mark of any subject is less than 0 or greater than 100.


Q.         Write a ‘C’ program to calculate and display the monthly income of a salesperson corresponding to the value of monthly sales input in the scanf() function, let us consider the following commission schedule: (Note: use if-else statement) 

Monthly SalesIncome
Greater than or equal to Rs.50,000 375 plus 16% of sales
Less than Rs. 50,000 but Greater than or equal to Rs. 40,000 350 plus 14% of sales
Less than Rs. 40,000 but Greater than or equal to Rs. 30,000 325 plus 12% of sales
Less than Rs. 30,000 but Greater than or equal to Rs. 20,000 300 plus 9% of sales
Less than Rs. 20,000 but Greater than or equal to Rs. 10,000 250 plus 5% of sales
Less than Rs. 10,000 200 plus 3% of sales



Q.         Write a program using switch-case to provide the options for the addition, subtraction, multiplication and division of two user given integer numbers.                
    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division
    If user reads any other choice then a message “Invalid Choice” should be displayed on the monitor.


Loop Programs

Q.     Write a C program to read an integers and print the multiplication table upto 10.


Q.    Write a program to display your name upto 10 times using while loop.


Q.     Write a program to calculate the sum of all the numbers from 1 to 50 using for loop.


Q.     Write a program to print even numbers from 1 to 50.


Q. Write a C program to determine whether a number is prime or composite.


Q.    Write a C program to display all prime numbers less than the number entered by the user.(Using function).


Q.    Write a function "int count_factor(int)"  to print the factors and return number of factors of a given number.

     Then using that function  write another function "void isprime(int)" to check if it is a prime number or not.


Q. Write a program in C to find the value of x^n where x and n are positive integers.


Q.     Write a C program to calculate sum of first n odd integers.


Q.        Write a ‘C’ function that returns the k-th digit from the right in the positive integer n. For example, digit(829,1) returns 9, digit(829,3) returns 8. If k is greater than the number of digits in n then the function is to return –1. Include appropriate documentation in your program.


Q. Write a C program using while loop to find factorial of a positive number.


Q.     Write a C program to print the factors of a number given by the user.


Q.         Write a C program to print the first n number of terms of fibonacci series.


Q. Write a C function "int sum_factor(int m)" that accepts one positive integer m and returns the sum of all factors of m. e.g. if m=28 then the function returns 1+2+4+7+14+28=56.


Q.    Write a program to check whether a number is palindrom or not.


Q.        Write a C program to reverse a number.


Q.        Write a C program to reverse a number using function.


Q.        Write a C program to reverse a number using do-while loop.


Q.        Write a C program to calculate the sum of all digits of a given integer.


Q.        Write a program to determine the sum of the following series:
    S = 1 – 3 + 5 – 7 + ...(n terms)
Read the value of n from the user.


Q.        Write a C program to check whether a number is armstrong or not.


Q.        Write a ‘C’ program to check whether a given number is perfect or not.
   
    A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.
    e.g., 496=1+2+4+8+16+31+62+124+248.


Q.    Write a C program to find HCF and LCM of two given numbers.


Q.    Write a C program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by a given integer x.




Q.    Write a program to print the following pyramid.   
[ e.g. if n=6 then ]

    *
    *    *
    *    *    *
    *    *    *    *
    *    *    *    *    *
    *    *    *    *    *    *



Q.    Write a program to print the following pyramid.   
[ e.g. if n=6 then ]

    *    *    *    *    *    *
    *    *    *    *    *
    *    *    *    *
    *    *    *
    *    *
    *



Q.    Write a C program to print the following output using for loop.
1
2 2
3 3 3
4 4 4 4



Q.        Write a ‘C’ program to to generate a triangle with fibonacci numbers (series given bellow) as elements of every row.
    e.g., forn=5, the output should be,
  
         1
         1         1
         1         1         2
         1         1         2         3
         1         1         2         3         5



Q.        Write a C program to print the Floyd’s triangle given below                  
         1        
         2 3
         4 5 6
         7 8 9 10
         … … … … …


Q.        Write a C function to generate the following figure for n=4.                  
         1        
         1 3
         1 3 5
         1 3 5 7
The value of n is passed to the function as an argument.


Array Programs

Q.    Write a ‘C’ program to insert n integer in an array and display them.


Q.    Write a C program to read n elements in an array and find the minimum element of the array.


Q.    Write a C program to read n elements in an array and find the maximum element of the array.


Q.    Write a 'C' program to find the largest difference between two numbers of an integer array using function.


Q.    Write a c program to find the sum of the elements of an integer array.


Q.    Write a C program to reverse an array.


Q. Write a C program to read 'n' integers and then send all negative elements of the array to the end without altering the original sequence.


Q.    Write a C program to print the largest even number and largest odd number from a list of numbers entered through keyboard.


Q.    Write a C program to rear n elements in an array and calculate the sum of all the elements of the array.


Q.   Write the following function:

        int search(int a[], int n, int x);

    Where a is an array to be searched, n is the number of elements in the array, and x is the search key. “search” should return 1 if x matches some element of a, 0 if it doesn’t. Use pointer arithmetic to visit array elements. Include appropriate documentation in your program.


Q.    Write the following function:
        int search(int a[], int n, int x);
    Where a is an array to be searched, n is the number of elements in the array, and x is the search key. “search” should return 1 if x matches some element of a, 0 if it doesn’t. Use pointer arithmetic to visit array elements. Include appropriate documentation in your program.


Q.    Suppose that two arrays, “a” and “b”, contain sets of integers. Neither of these sets contains any duplication. Write a C program to find out the union of these two sets. For example, if “a” is {2, 7, 1, 5} and “b” is {9, 2, 8, 5} then their union is {2, 7, 1, 5, 9, 8}.


Q.    Write a function that accepts an array and constant parameters and count how many elements of array are less than the constant, equal to it and greater than it. Return these three values either by way of pass by reference parameter or a three element array parameter.


Q.    Suppose that two arrays, “a” and “b”, contain sets of integers. Neither of these sets contains any duplication. Write a C program to find out the intersaction of these two sets. For example, if “a” is {2, 7, 1, 5} and “b” is {9, 2, 8, 5} then their union is {2, 5}.


Q.        Write a C program to create a third array by using two existing sorted array, third array must automaticall get created in sorted order without having duplicacy of number.
     e.g.,     array1={4,8,9,12,16,20} 
         array2={1,3,4,5,8,9,14} 
         result={1,3,4,5,8,9,12,14,16,20}


Q.    Write a ‘C’ program to remove duplicates from an ordered array. e.g. if input is 1,1,1,3,4,4,5,8,8 then output should be 1,3,4,5,8.


Q.    Write a C program to find the second largest element of an array of n elements.


Q.    Write a function which accepts an array of size n containing integer values and returns average of all values. Call the function from main program.




Two dimension array Programs


Q.     Write a C program to create a matrix and display it.


Q.     Write a C program to create a matrix and display it.(Using function).


Q.     Write a ‘C’ program to find out sum of principal diagonal elements of a square matrix.


Q.    Write a C program to calculate the transpose of a matrix.


Q.     Write a ‘C’ program to find out sum of principal diagonal elements of a square matrix using function.


Q.    Write a C program to add two matrices.


Q.    Write a function that returns 1 if the two matrices passed to it as argument are equal and 0 otherwise.



String Programs

Q.    Write a program to find length of a string using library function.


Q.    Write a program to reverse a string using library function.


Q.    Write a program to concanate two strings using library function.


Q.    Write C functions that will work exactly as "strcpy()" without using any building string manipulation function.


Q.    Write C functions that will work exactly as "strcmp()" without using any building string manipulation function.


Q.    Write C functions that will work exactly as "strcat()" without using any building string manipulation function.


Q.    Write C functions that will work exactly as "strlen()" without using any building string manipulation function.


Q. Write a C function print_upper() to prints its character argument in uppercase. (using function).


Q.    Write a C program to find the number of vowels and consonants in a given string.


Q.    Write  a C program to sort n strings entered by the user.


Q.    Write a program to read in an array of names and to sort them in alphabetical order.


Q.    Write a C program to find frequency of a given character in a given string.
    e.g,
    str="assam"
    ch=s;
    frequency=2



Sorting Programs

Q.    Write a program to sort n integers using bubble sort.


Q.    Write a program to sort n integers using bubble sort using function.


Q.    Write a program to sort n integers using insertion sort.


Structure Programs

Q.        Define a structure for a student having name, roll number, and marks obtained in six subjects. Assume that “allstudents” is an array of students. Write a C program to print the name and roll numbers of the students who have secured highest marks in each subject.


Q.        Define a structure of employees of an organization with the following fields:
        Empno, Empname, Date_of_joining, Salary, Department
    Write a program which accepts details of ten employees and print them on the screen.


Q.        Write a C program to sort the records of n students on the basis of their height (descending order) if age of two or more employees are same then sort them on the basis of their weight (descanding order). Use the array of the following structure to have the records of students
         struct student
         {
                 int rollno;
                 float height,weight;
                 char name[50];
         };.


Linked List Programs

Q.    Define a self referential structure for representing a simple linked list of integers. Write a C function to split the list into two lists so that the first list contains all even numbered elements and the second list contains only odd numbered elements.
    For example, if the original list is {2, 8, 1, 13, 14, 28, 0} then the resultant list first list would be {2, 8, 14, 28, 0} and the second list would be { 1, 13}.


FILE Programs

Q.        Write a C program to write a sentence into a file.


Q.       Write a C program to count the of words in a file named "file.txt". [A word is a sequence of letters ending with a blank, or a tab, or an end of line marker or end of file or punctuation symbols such as ",", ".", "!" and "?".].


Function Programs

Q.       Write a recursive function to compute factorial of a number.




2 comments: