The greatest mistake you can make in life is to be continually fearing you will make one
Showing posts with label C Programs. Show all posts
Showing posts with label C Programs. Show all posts

Monday, 17 May 2010

C Programs based on Command Line Arguments

  1. Write a program to read a text file provided at command prompt and print the content of the file
  2. Write a program to read a text file provided at command prompt and print each word in reverse order For example if the file contains WELCOME TO C output should be C OT EMOCLEW

  3. Write a program using command line arguments to search for a word in a file and replace it with the specified word.The usage of the program is shown below
    C > change < oldword > < newword > < filename >
  4. Write a program that canbe used at command prompt as a calculating utility.The usage of the program is shown below.
    C>calc < switch > < n > < m >
    where n and m are 2 integer operands. switch can be any one of the arithmeticor comparison operators. If arithmetic operator is supplied,the output should be the result of the operation.If comparison operator is supplied then the output should be True or false

Friday, 12 March 2010

C Interview Questions

1.How to write a c program without using semicolon?
Solution

1.#include<stdio.h>
main()
{
if(printf("Welcome")){}
}
output
Welcome
2.#include<stdio.h>
main()
{
while(printf("Welcome"),0){}
}
output
Welcome
The while loop will execute until the condition is true. so the statement will print continuously to avoid that comma operator is used. The comma operator ignores the 1st argument and return the 2nd one. so the value o is given to while loop and the while loop is executed only once

2.Write a c program to print the number of digits in a decimal number?
#include<stdio.h>
int main()
{
    int n,count=0,x;
    printf("ENter the decimal number\n");
    scanf("%d",&n);
    x=n;
    while(n>0)
    {
        count++;
        n=n/10;
    }
    printf("Number %d has %d digits\n",x,count);
}
output
ENter the decimal number
2345
Number 2345 has 4 digits

3.Write a C program to print the two's complement of a decimal number?
 Solution
#include<stdio.h>
int main()
{
    int n,x;
    printf("Enter a number\n");
    scanf("%d",&n);
    x=n;
    n=(~n)+1;
    printf("Two's complement of %d=%d",x,n);
    return 0;
}
Output
Enter a number 81
Two's complement of 81=-81

Wednesday, 10 March 2010

C programs based on Array

1.Write a program to copy the contents of one array in to another in the reverse order.
2.If an array name contains n elements,then write a program to check if name[0]=name[n-1],name[1]=name[n-2] and so on.
3.Write a program to initialize an integer array of 10 elements,pass the entire array to a function multiply() and in multiply(),multiply each element of array by 10 and return the control to main() and print the new array elements in main().
4.20 numbers are entered from the keyboard into an array. Write a program to find out how many of them are positive,how many are negative,how many are even and how many are odd.
5.Write a program to search a given number is present in the array or not
6. Implement the selection sort,bubble sort and insertion sort algorithms on a set of 10 numbers

Monday, 1 March 2010

C programs based on Preprocessor

  1. Write a macro definition to test whether a character entered is a small case letter or not.
  2. Write a macro definition to test whether a character entered is a upper case letter or not.
  3. Write a macro definition to test whether a character entered is a alphabet or not. use the macro defined in 1 and 2.
  4. Write a macro definition to obtain the bigger of two numbers.
  5. Write macro definitions with arguments for calculation of area and perimeter of a triangle,a square and a circle.
  6. Write a macro definition to find arithmetic mean of two numbers.
  7. Write a macro definition to find absolute value of a number.
  8. Write a macro definition to convert a uppercase alphabet to lowercase
  9. Write macro definitions with arguments for calculation of Simple Interest and amount.
  10. Define a macro swap(t,x,y) that interchanges two arguments of type t.