Thursday, 27 February 2014

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 1 2 1 1 2 3 1 1 2 3 5

#include<stdio.h>
main()
{  
    int a=1,b=1,i=1,n,temp,j;
    printf("\nEnter number of rows=");
    scanf("%d",&n);
    printf("\nThe fibonacci series is\n");
    j=1;
    while(j<=n)
    {
        a=1;
        b=1;
        i=1;
        while(i<=j)
        {
            printf("%10d",a);
            temp=a;
            a=b;
            b=temp+a;
            i++;
        }
        printf("\n");
        j++;
    }
}

Tuesday, 25 February 2014

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

#include<stdio.h>
main()
{   
    int num,temp,sum=0,i=1;
    printf("\nEnter a number=");
    scanf("%d",&num);
    temp=num;
    while(i<=num/2)
    {
        if(num%i==0)
            sum=sum+i;
        i++;
    }
    if(temp==sum)
        printf("\n%d is a perfect number",temp);
    else
        printf("\n%d is not a perfect number",temp);
}

Monday, 24 February 2014

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

#include<stdio.h>

void insert(int a[][100],int s)
{
    int i,j;
    for(i=0;i<s;i++)
    {
        for(j=0;j<s;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
}

void show(int a[][100],int s)
{
    int i,j;
    for(i=0;i<s;i++)
    {
        for(j=0;j<s;j++)
        {
            printf("%6d", a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

int sum_diagonal(int a[][100],int s)
{
    int i,j,sum=0;
    for(i=0;i<s;i++)
    {
        for(j=0;j<s;j++)
        {
            if(i==j)
                sum=sum+a[i][j];
        }
    }
    return sum;
}

main()
{
    int a[100][100],size;
   
    printf("\nEnter size of matrix=");
    scanf("%d",&size);   
    printf("\nEnter elements of the matrix\n");
    insert(a,size);
    printf("\nThe matrix\n");
    show(a,size);   
    printf("\nThe sum of diagonal elements=",sum_diagonal(a,size));
}

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

#include<stdio.h>
main()
{   
    int a[100][100],size,i,j,sum=0;
    printf("\nEnter size of the matrix=");
    scanf("%d",&size);
    printf("\nEnter the matrix elements=");
    for(i=0;i<size;i++)
    {
        for(j=0;j<size;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("\nThe matrix is\n");
    for(i=0;i<size;i++)
    {
        for(j=0;j<size;j++)
        {
            printf("%6d",a[i][j]);
        }
        printf("\n");
    }
    for(i=0;i<size;i++)
    {
        sum=sum+a[i][i];
    }
    printf("\nThe sum of principal diagonal elements is %d",sum);
}

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

#include<stdio.h>
main()
{   
    int n1,n2,num1,num2,lcm,rem;
    printf("\nEnter two numbers=");
    scanf("%d%d",&num1,&num2);
    printf("\nThe two numbers are %d and %d",num1,num2);
    if(num1>num2)
    {
        n1=num1;
        n2=num2;
    }
    else
    {
        n1=num2;
        n2=num1;
    }
    while(n2%n1!=0)
    {
        rem=n2%n1;
        n2=n1;
        n1=rem;
    }
    lcm=(num1*num2)/n1;
    printf("\nH.C.F. of %d and %d is %d",num1,num2,n1);
    printf("\nL.C.M. of %d and %d is %d",num1,num2,lcm);
}

Saturday, 22 February 2014

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}

#include<stdio.h>
main()
{   
    int array1[100],array2[100],result[200],n1,n2,i,j,k,f;
    printf("\nEnter number of terms of the first set=");
    scanf("%d",&n1);
    printf("\nEnter the elements of the first set=");
    for(i=0;i<n1;i++)
    {
        scanf("%d",&array1[i]);
    }       
    printf("\nEnter number of terms of the second set=");
    scanf("%d",&n2);
    printf("\nEnter the elements of the second set=");
    for(i=0;i<n2;i++)
    {
        scanf("%d",&array2[i]);
    }
    printf("\nThe elements of the first set=");
    for(i=0;i<n1;i++)
    {
        printf("%d ",array1[i]);
    }
    printf("\nThe elements of the second set=");
    for(i=0;i<n2;i++)
    {
        printf("%d ",array2[i]);
    }
    i=0;
    j=0;
    k=0;
    while(i<n1 && j<n2)
    {
        if(array1[i]==array2[j])
        {
            result[k++]=array1[i++];
            j++;
        }
        else if(array1[i]<array2[j])
            result[k++]=array1[i++];
        else
            result[k++]=array2[j++];
    }
    while(i<n1)
    {
        result[k++]=array1[i++];       
    }
    while(j<n2)
    {
        result[k++]=array2[j++];       
    }       
    printf("\nThe result=");
    for(i=0;i<k;i++)
    {
        printf("%d ",result[i]);
    }   
}

Friday, 21 February 2014

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

#include<stdio.h>
main()
{   
    int n,i=1;
    printf("\nEnter the number=");
    scanf("%d",&n);
    printf("\nThe factors are=");
    while(i<=n)
    {
        if(n%i==0)
            printf("%d ",i);       
        i++;
    }
}

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

#include<stdio.h>
main()
{   
    int a=1,b=1,i=1,n,temp;
    printf("\nEnter number of terms=");
    scanf("%d",&n);
    printf("\nThe fibonacci series is\n");
    while(i<=n)
    {
        printf("%d ",a);
        temp=a;
        a=b;
        b=temp+a;
        i++;
    }
}

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

#include<stdio.h>
main()
{   
    int n,i=1;
    printf("\nEnter a number=");
    scanf("%d",&n);
    printf("\nThe multiplication table is\n");
    while(i<=10)
    {
        printf("\n%d x %d=%d",n,i,n*i);
        i++;
    }
}

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.




Thursday, 20 February 2014

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}.

#include<stdio.h>
main()
{   
    int a[100],b[100],u[200],n1,n2,i,j,k,f;
    printf("\nEnter number of terms of the first set=");
    scanf("%d",&n1);
    printf("\nEnter the elements of the first set=");
    for(i=0;i<n1;i++)
    {
        scanf("%d",&a[i]);
    }
   
   
    printf("\nEnter number of terms of the second set=");
    scanf("%d",&n2);
    printf("\nEnter the elements of the second set=");
    for(i=0;i<n2;i++)
    {
        scanf("%d",&b[i]);
    }
    printf("\nThe elements of the first set=");
    for(i=0;i<n1;i++)
    {
        printf("%d ",a[i]);
    }
    printf("\nThe elements of the second set=");
    for(i=0;i<n2;i++)
    {
        printf("%d ",b[i]);
    }

    j=0;
    for(i=0;i<n1;i++)
    {
        f=0;
        for(k=0;k<n2;k++)
        {
            if(a[i]==b[k])
            {
                u[j++]=b[k];
            }
        }                   
    }
    printf("\nThe intersaction=");
    if(j==0)
        printf("NULL");
    else
    {
        for(i=0;i<j;i++)
        {
            printf("%d ",u[i]);
        }
    }
}

Suppose that two arrays, “a” and “b”, contain sets of integers. Neither of these sets contains any duplication. Write a C function 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}.

#include<stdio.h>
main()
{   
    int a[100],b[100],u[200],n1,n2,i,j,k,f;
    printf("\nEnter number of terms of the first set=");
    scanf("%d",&n1);
    printf("\nEnter the elements of the first set=");
    for(i=0;i<n1;i++)
    {
        scanf("%d",&a[i]);
    }
   
   
    printf("\nEnter number of terms of the second set=");
    scanf("%d",&n2);
    printf("\nEnter the elements of the second set=");
    for(i=0;i<n2;i++)
    {
        scanf("%d",&b[i]);
    }
    printf("\nThe elements of the first set=");
    for(i=0;i<n1;i++)
    {
        printf("%d ",a[i]);
    }
    printf("\nThe elements of the second set=");
    for(i=0;i<n2;i++)
    {
        printf("%d ",b[i]);
    }
   
    for(i=0;i<n1;i++)
    {
        u[i]=a[i];
    }
   
    for(j=0;j<n2;j++)
    {
        f=0;
        for(k=0;k<n1;k++)
        {
            if(a[k]==b[j])
            {
                f=1;
                break;
            }
        }
        if(f==0)
            u[i++]=b[j];
    }
    printf("\nThe union=");
    for(j=0;j<i;j++)
    {
        printf("%d ",u[j]);
    }   
}

Wednesday, 19 February 2014

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

#include<stdio.h>
main()
{   
    int num;
    printf("\nEnter a number=");
    scanf("%d",&num);
    if(num%2==0)
        printf("\n%d is an even number");
    else   
        printf("\n%d is an odd number");
}

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.

#include<stdio.h>
main()
{   
    int a[100],pos[100],neg[100],n,i,j,k;       
    printf("\nEnter number of terms=");
    scanf("%d",&n);
    printf("\nEnter the elements=");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);   
    printf("\nThe original array=");
    for(i=0;i<n;i++)
        printf("%d ",a[i]);
    j=0;
    k=0;
    for(i=0;i<n;i++)
    {       
        if(a[i]<0)
        {
            neg[j++]=a[i];
        }
        else
        {
            pos[k++]=a[i];
        }   
    }
    for(i=0;i<k;i++)
        a[i]=pos[i];
    for(k=0;i<n;i++)
        a[i]=neg[k++];
    printf("\nThe final array=");
    for(i=0;i<n;i++)
        printf("%d ",a[i]);
}

Tuesday, 18 February 2014

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.

#include<stdio.h>

int sum_factor(int m)
{
    int i=1,sum=0;
    while(i<=m)
    {
        if(m%i==0)
        {
            sum=sum+i;
        }
        i++;
    }
    return sum;
}

main()
{
    int n;
    printf("\nEnter the number=");
    scanf("%d",&n);   
    printf("Sum of the factors of %d=%d",n,sum_factor(n));
}

Tuesday, 11 February 2014

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

#include<stdio.h>
main()
{
    int a[100],me,mo,n,i;
    printf("\nEnter number of elements=");
    scanf("%d",&n);
    printf("\nEnter the elements=");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++)
    {
        if(a[i]%2!=0)
            continue;
        me=a[i];
        break;
    }
    for(;i<n;i++)
    {
        if(a[i]%2==0 && a[i]>me)
            me=a[i];
    }
   
    for(i=0;i<n;i++)
    {
        if(a[i]%2==0)
            continue;
        mo=a[i];
        break;
    }
    for(;i<n;i++)
    {
        if(a[i]%2!=0 && a[i]>mo)
            mo=a[i];
    }
    printf("Largest even number=%d\n",me);
    printf("Largest odd number=%d",mo);
}

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

#include<stdio.h>
main()
{
    int n,i=1,f=1;
    printf("\nEnter a number=");
    scanf("%d",&n);
    while(i<=n)
    {
        f=f*i;
        i++;
    }
    printf("\nfactorial of %d is %d",n,f);       
}

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

#include<stdio.h>
main()
{
    int n,i,f=0;
    printf("\nEnter a number=");
    scanf("%d",&n);
    if(n==1)
        printf("\n%d is neither a prime number nor a composite number",n);
    for(i=2;i<=n/2;i++)
    {
        if(n%i==0)
        {
            f=1;
            break;
        }
    }
    if(f==1)
        printf("\n%d is a composite number",n);
    else
        printf("\n%d is a prime number",n);
       
}
#include <stdio.h>
void insert(int a[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
}

void show(int a[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }
}

int max(int a[],int n)
{
    int i,m=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]>m)
            m=a[i];
    }
    return m;
}

int min(int a[],int n)
{
    int i,m=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]<m)
            m=a[i];
    }
    return m;
}

main()
{
    int a[100],n,ld;
    printf("\nEnter number of terms=");
    scanf("%d",&n);
    printf("\nEnter the array elements=");
    insert(a,n);
    printf("\nThe array=");
    show(a,n);
    ld=max(a,n)-min(a,n);
    printf("\nThe largest difference between two numbers=%d",ld);
}

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

#include<stdio.h>
main()
{
    int a[100],n,sum=0,i;
    printf("\nEnter number of elements=");
    scanf("%d",&n);
    printf("\nEnter the elements=");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("\nThe array is=");
    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }
   
    for(i=0;i<n;i++)
        sum=sum+a[i];
    printf("\nThe sum is=%d\n",sum);   
}

Thursday, 6 February 2014

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

#include <stdio.h>
void insert(int a[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
}

void show(int a[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }
}

int max(int a[],int n)
{
    int i,m=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]>m)
            m=a[i];
    }
    return m;
}

int min(int a[],int n)
{
    int i,m=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]<m)
            m=a[i];
    }
    return m;
}

main()
{
    int a[100],n,ld;
    printf("\nEnter number of terms=");
    scanf("%d",&n);
    printf("\nEnter the array elements=");
    insert(a,n);
    printf("\nThe array=");
    show(a,n);
    ld=max(a,n)-min(a,n);
    printf("\nThe largest difference between two numbers=%d",ld);
}

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

#include <stdio.h>
main()
{
    int x,n,result=1,i=1;
    printf("\nEnter the value of x=");
    scanf("%d",&x);
    printf("\nEnter the value of n=");
    scanf("%d",&n);
    while(i<=n)
    {
        result=result*x;
        i++;
    }
    printf("\n %d^%d=%d",x,n,result);
}

Wednesday, 5 February 2014

C programming MCQ

Q1.    The following is a program
    #include<stdio.h>
    main()
    {
        int x=0;
        while(x<=10)
        for( ; ; )
            if( ++ x%10 = =0)
                break;
            printf(“x = %d”, x);
    }
    What will be the output of the above program?
A)    Will print x = 10
B)    Will give compilation error
C)    Will give runtime error
D)    Will print x = 20

Q2.    Consider the following variable declaration
    Union x
    {
        int i;
        float f;
        char c;
    }y;
    if the size of i, f and c are 2 bytes, 4 bytes and 1 byte respectively then the size of the variable y is:-
A)    1 byte
B)    2 bytes
C)    4 bytes
D)    7 bytes

Q3.    Pick up the odd one out from the following
A)    x=x–1
B)    x-=1
C)    x--
D)    x=-1

 Q4.    What is the value of ‘average’ after the following program is executed?
    main()
    {
        int sum, index;
        float average;
        sum = 0;
        for( ; ; )
        {
            sum = sum + index;
            ++ index;
            if (sum > = 100) break;
        }
        average = sum / index;
    }
A)    91/13
B)    91/14
C)    105/14
D)    105/15

Q5.    Suppose i, j, k are integer variables with values 1, 2, 3 respectively. What is the value of the following expression?
    ! (( j + k ) > (i + 5 ))

A)    6
B)    5
C)    1
D)    0

Q6.     If a = -11 and b = -3. What is the value of a % b?
A)    -3
B)    -2
C)    2
D)    3

Q7.    If c is a variable initialized to 1, how many times will the following loop be executed?
    while(( c > 0 && (c < 60))
    {
        c++;
    }
A)    61
B)    60
C)    59
D)    1

Q8.    Which one of the following describes correctly a static variable?
A)    This cannot be initialized.
B)    This is initialized once at the commencement of execution and cannot be changed at run time.
C)    This retains its value through the life of the program.
D)    This is same as an automatic variable but is placed at the head of a program.

Q9.    What will be the output of the following program?
    main()
    {
        int a, *ptr, b, c;
        a = 25;
        ptr = &a;
        b = a + 30;
        c = *ptr;
        printf(“%d %d %d”, a, b, c);
    }
A)    25, 25, 25
B)    25, 55, 25
C)    25, 55, 25
D)    None of the above

Q10.    If a = 0×aa and b = a << 1 then which of the following is true
A)    b=a
B)    b = 2a
C)    a = 2b
D)    b=a-1

Fill in the blanks

Q1.     Data is recorded on the tape in the form of ____________ pulses.
Q2.     ____________ memory consists of the first 640 kilobytes of RAM.
Q3.     The innermost layer of the operating system is ________.
Q4.     ____________ are logical concentric circles.
Q5.     A(n) __________ consists of hidden programming instructions that are buried in an applications or systems program; it can damage data, files and  entire computer systems.
Q6.     A(n) ___________ is the basic information about each page; usually the page numbers the title of publication and the date of publication etc.
Q7.     To insert a page break, press ___________.
Q8.     A(n) ___________ is a row of dots or dashes that is inserted between two  text elements.
Q9.     _________ are added to margins, thereby increasing the white space and decreasing the text area for specific paragraphs.
Q10.    The _______ view helps in getting the detailed picture of each slide.

True False

Q1.    Main memory is a software component.
Q2.    A computer's CD-ROM drive can read data disks, but it cannot play audio CDs.
Q3.    The first successful GUI appeared on the Macintosh computer in 1984.
Q4.     A utility program can be used to improve the function of an operating system in some way.
Q5.     During the formatting process, a disk's surface is divided into sectors, which are then
further divided into rings, called tracks.
Q6.     Bold, italics and bold italics are available for all fonts.
Q7.     All sections in a document necessarily use the same margins.
Q8.     You can add dates to worksheets, but spreadsheets cannot use dates in calculations.
Q9.     In a spreadsheet, ordinary text is called a "label."
Q10.    Presentation programs let you create notes for each slide, which are visible only to you.

MCQ

Q1.    Number system using strings of 0’s and 1’s is termed as:
A)    Decimal number system
B)    Binary number system
C)    Hexadecimal number system
D)    Octal number system

Q2.    Which one of the following is not hardware?
A)    Magnetic Tape
B)    VDU terminal
C)    Printer
D)    Assembler

Q3.    Multiple choice examination answer sheets can be evaluated automatically by:
A)     OMR
B)     OCR
C)     MICR
D)     Scanner

Q4.     To create large-size drawings, architects and engineers often use a device called a:
A)     Plotter
B)     Band printer
C)     Line printer
D)     Laser printer

Q5.    To launch a program in a command-line interface, you must:
A)    Click an icon
B)    Type one or more memorized commands
C)    Use a menu
D)    None of the above

Q6.    The part of an operating system, which permanently resides in main memory is
A)    Job scheduling module
B)    Kernel
C)    Translator
D)    None of the above

Q7.    Microsoft Office 97 includes the following:
A)     Word 97
B)     Outlook 97
C)     Excel 97
D)     All of the above

Q8.    To insert clip art, pictures from disk into a Word Document, which of the following method is used?
A)    Insert, File command
B)    Insert, Picture command
C)    Insert Object command
D)    Add, Picture command

Q9.    Which of the following is not a tool for analyzing spreadsheet data?
A)     What-if analysis
B)     Mail merge
C)     Goal seeking
D)     Sorting

Q10.     Which method can you use to navigate from one slide to another in a slide show?
A)     Press the Esc key
B)     Click the mouse button
C)     Change the channels on the TV
D)     None of the above

Program to find Sum and Average of Three Real Numbers

#include <stdio.h>
main()
{
    float a, b, c, sum, avg;
    printf("\nEnter value of three numbers: ");
    scanf("%f %f %f", &a, &b, &c);
    sum = a + b + c;
    avg = sum / 3;
    printf("\nSum = %f", sum);
    printf("\nAverage = %f", avg);
}

Tuesday, 4 February 2014

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

#include<stdio.h>

main()
{
    int a[100],n,i;
    printf("\nEnter number of terms=");
    scanf("%d",&n);
    printf("\nEnter the integers=");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
   
    printf("\nThe array elements are=");
    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }
}

Monday, 3 February 2014

Write C Program to find area and circumference of circle.

#include<stdio.h>

main()
{
    int rad;
    float PI=3.14,area,ci;
    printf("\nEnter radius of circle: ");
    scanf("%d",&rad);
    area = PI * rad * rad;
    printf("\nArea of circle : %f ",area);
    ci = 2 * PI * rad;
    printf("\nCircumference : %f ",ci);   
}