Main

http://open.popnc.com/pnc/16280

Thursday, 26 December 2019

C Language Overview


C Language Overview
The C programming language is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard.

The UNIX operating system, the C compiler, and essentially all UNIX applications programs have been written in C. The C has now become a widely used professional language for various reasons. 

  1.   Easy to learn
  2.   Structured language
  3.    It produces efficient programs.
  4.     It can handle low-level activities.
  5.     It can be compiled on a variety of computer platforms.
Facts about C

  1.   C was invented to write an operating system called UNIX.
  2.   C is a successor of B language, which was introduced around 1970. 
  3.     The language was formalized in 1988 by the American National Standard Institute. (ANSI).
  4.     The UNIX OS was totally written in C by 1973.
  5.     Today, C is the most widely used and popular System Programming Language.
  6.     Most of the state-of-the-art softwares have been implemented using C.
  7.     Today's most ][popular Linux OS and RBDMS MySQL have been written in C.
Why to use C?
C was initially used for system development work, in particular the programs that make up the operating system. C was adopted as a system development language because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be:
  Operating Systems
  Language Compilers
  Assemblers
  Text Editors
  Print Spoolers
  Network Drivers
  Modern Programs
 Databases
  Language Interpreters
 Utilities
C Programs
A C program can vary from 3 lines to millions of lines and it should be written into one or more text files with extension ".c"; for example, hello.c. You can use "vi", "vim" or any other text editor to write your C program into a file.
This tutorial assumes that you know how to edit a text file and how to write source code using any programming language.

Monday, 23 December 2019

2 Marks Question with answer of C Language

1) How can the number of bytes allocated to each basic data type to be determined for a particular C compiler? 
➤➤ C provides sizeof operator to determine the number of bytes allocated to each basic data type. The sizeof() operator gives the size of the type passed as argument to it. 
e.g.: sizeof(int) gives 2 
 
2) What is data type? What are the data types in C language? 

➤➤Data types are the keywords used in one of the two purposes:
 (i) Starts a variable declaration to specify what amount of memory       is to be allocated for a variable and what type of data 
     (or value) it can store.
 (ii) Starts a function declaration to specify what type of data          the function can return. C provides following data types:            int, long, float, double, char 
 
3) Distinguish between constant and variable. 
➤➤The difference between constant and variable are: 
(i) A constant (or literal) is a sequence of characters to represent a constant value which may be assigned to a variable, passed to a function argument or used in an expression. On the other hand a variable is an identifier (symbol or name) of a memory location and used to store value of specific type.
(ii) Constants are system defined whereas variables are user defined. 
 
4) Is ‘*’ a unary or binary operator or both? Justify your answer with example.
 ➤➤ The ‘*’ is both unary and binary operator. 
When ‘*’ is used as ‘multiplication operator’, it operates on two operand and so called binary operator. But when ‘*’ is used as ‘pointer indirection/dereference operator’, it operates on a single operand (pointer) to obtain the data of the memory location whose address is contained in it and so called unary operator. 
 
5) Is ‘&’ a unary or binary operator or both? Justify.
 ➤➤ The ‘&’ is both unary and binary operator.
 When ‘&’ is used as ‘bitwise AND operator, it operates on two operand and so called binary operator. But when ‘&’ is used as ‘reference/address-of operator’, it operates on a single operand and so called unary operator. 
 
6) What is comma operator? What is its purpose? 
➤➤The comma (,) operator is used to separate consecutive identifiers in a variable declaration. It is also used to chain statements together inside a for-loop statement. 
 
7) What is the syntax of conditional operation statement? What is its function? 
➤➤The syntax of conditional operation statement is 
(expression1)? expression2 : expression3 ;
 If expression1 is true then expression2 is evaluated and give the results. Otherwise expression3 is evaluated and give the result. 
 
8) How can the conditional operator be combined with relational operator to form an ‘if-else’ type statement?
 ➤➤The conditional operator can be nested two form an ‘if-else’ type statement. For instance the following statement find the largest among three integers a,b,c. 
large=(a>b && a>c)?a:(b>a&&b>c)?b:c; 
 
9) For each of the statement below assuming y=20 before of the statement, what are the values of x after execution. 
(i) x=y==y--; (ii) x=5*y++ 
➤➤ For (i) y-- gives 20 then decreases the value of y to 19. So y==y-- is false and gives 0 which is assigned to x. Therefore value of x is 1.
 For (ii) y++ gives 20 which is multiplied by 5 and assigned to x. Therefore value of x is 100. 
 
10) For each of the statement below assume that x=100 prior to execution of the statement. In each case what are the value of y after execution.
 (i) y=x=x++; (ii) y=x==x++;
 ➤➤ For (i) x++ gives 100 and increases the value of x to 101, but 100 is assigned to x which is again assigned to y. Therefore value of y is 100.
 For (ii) x++ gives 100 and increases the value of x to 101, so x==x++ is false and gives 0 which is assigned to y. Therefore value of y is 0. 
 
11) Explain the meaning of each if the following is the first line of a function definition (i) long f(long a) 
(ii) float f(float a, float b)
➤➤For (i) f is a function that takes a long value as argument and returns long value.
 For (ii) f is a function that takes two float value as argument and returns a float value. 
 
12) What is the purpose of the scanf() function? Compare it with getchar() function. 
➤➤ The scanf function scans/reads a series of input fields form the input-stream and assigned to the address passed as argument to it. 
Comparison of scanf() and getchar(): 
(i) The scanf() takes a list of arguments to store scanned data. On the other hand getchar() takes no argument. 
(ii) The scanf() reads a series of input fields from stdin. Whereas getchar() reads a single character from stdin. 
(iii) On success, scanf() returns the number of input fields successfully scanned; whereas getchar() returns the character read. On error, both of them return EOF. 
 
13) What is the difference between branching and looping statements? ➤➤The difference between branching and looping statements are:
 (i) A branching/jump statement allows the program control to jump to another part of the program directly. e.g.: break, continue, goto, return. On the other hand a looping/iteration statement allows a program to repeat some statements based on some conditions. e.g.: while, do-while, for. 
(ii) The branching statement does not contain other statements; whereas the looping statements may contain one or more other statements. 
 
14) What is the difference between while and do-while statement?
 ➤➤ The only difference between do-while and while is that the while statement evaluates its condition at the top of the loop so that if it is false then the statements within the loop will not execute; whereas the do-while evaluates its condition at the bottom of the loop so that the statements within the do block are always executed at least once. 
 
15) Can any of the three expressions in for loop be omitted? Justify. 
➤➤Yes, any of the three expression of the for-loop can be omitted; but, we must provide the null statement (;) in place of the omitted expression. 
 
16) What is difference between break and continue statement? 
➤➤ The difference between break and continue statements are:
 (i) The break statement is used to terminate the innermost switch, for, while, or do-while statement. On the other hand the continue statement is used to skip the current iteration of the innermost loop and evaluates the condition part (updating-part in case of for-loop) of the loop.
 (ii) The break statement can be used terminate both the loops and switch statement; whereas the continue statement can only be used within a loop. 
 
17) What is need of return statement? 
➤➤The return statement is used to terminate the current function and pass control to the caller of the function and also returns value to the caller if desired.
 It has the following general form:
 return;           In case of not returning value. 
 return value;     In case of returning value to the caller. 

18) What is pointer? Give example. 
➤➤A pointer is a variable which is used to store memory-address of another variable of same type and can be used to access both the address and value of that variable.   
 Syntax: 
 type *pointer = & variable;
 e.g.:                            

 int x = 10;                         
 int *y = &x; 


19) What is an array? How array can be declared?
 ➤➤ An array is a collection of homogeneous type variables stored in contiguous memory locations and share the same array name. 
Syntax of array declaration:
data-type array-name[size];  
e.g.: 
int x[5]; 

20) What is the relationship between array name and pointer? 
➤➤ The array name is actually a pointer pointed to the first memory location of allocated fixedsize contagious memory-locations for an array.
 e.g.: 
/*consider the following program which declare an array and access the value like pointer */ 
#include<stdio.h> 
void main()
{  
int a[]={23,45,67,12};
  int i;  
for(i=0; i<4; i++)
 {   
printf(“%d  ”,*(a+i)); 
 }
}
The compiler automatically converts a[i] to *(a+i) when accessing the (i+1)th value of a. 
   



 21) Distinguish between (i) int* ip[10] and int (*ip)[10] (ii) int p(char *a) and int *p(char *a) 
➤➤For (i) int*ip[10] is an integer array of 10 pointers; whereas     int (*ip)[10] is a pointer to an integer array of 10 elements. 
   For (ii) int p(char *a)  is a function that takes a character       pointer as arguments and returns an integer value; whereas int      *p(char *a) is a function that takes a character pointer as           arguments and returns a pointer to integer. 

22) What is a null string and what is its length? Explain with example.
➤➤A null string is nothing except of a pair of double quotes “”. That is it contains only one ASCII character – the null character ’\0’. 

23) Does ‘x’ differ from “x”? Justify. 
➤➤’x’ is a character constant. Whereas “x” is a string; i.e. a character array whose last character is ’\0’. This can be shown as follows                                                              

That means “x” is equivalent to {’x’,’\0’}. 

24) State the difference between strcmp and stricmp. 
➤➤strcmp() compares two strings; but it distinct the uppercase and lowercase letter. That means strcmp() does not ignore the case. For example strcmp(“Hello”, “hello”) does not gives 0. 
  On the other hand stricmp() compares two strings without regard to case. That means stricmp() ignores the case. For example stricmp(“Hello”, “hello”) gives 0. 

25) Distinguish between malloc and calloc function.
 ➤➤ malloc() and calloc() has two main differences: 
(i) malloc() takes only one argument which is the size of the block of memory space to be allocated. Whereas calloc() takes two arguments – one is the number of memory block is to be allocated and the other is the size of each of the block.
 (ii) malloc() does not initialize the memory allocated, whereas calloc() initializes the allocated memory to ZERO. 

26) What are arguments in a function? 
 The variables or value passed in the parenthesized list in a function invocation are known as arguments. 
e.g.: void show(n); //here n is an argument for the function show() 

27) What is the difference between formal arguments and actual arguments?
➤➤The variables declared in the parenthesized list in a function definition are known as formal arguments or parameters.
 e.g.: 
void show(int x){ //here x is a formal argument of the function show() 
printf(“%d”, x);
 } 

On the other hand the variables or value passed in the parenthesized list in a function invocation are known as actual arguments or simply arguments.
 e.g.: 
void show(n); //here n is an actual argument for the function show() 


28) What is prototype declaration?
➤➤The prototype declaration (or function prototyping) is the method of declaring a function before it is defined. 
It has the following general form:
 return-type function-name(argument-types-separated-by- comma); 

29) How is an array name interpreted? How it is passed to function? ➤➤The array name is actually a pointer pointed to the first memory location of allocated fixedsize contagious memory-locations for an array. The compiler automatically converts a[i] to *(a+i) when accessing the (i+1)th value of a. 

Like other variables an array can also be passed to a function. But, it must be remembered that passing an array to a function is always call by reference. So, modifying the element of the array within the function will actually modify the actual array. 
It has either of the following general form: 
return-type function-name(type arrayName[])
 {  //…body
 }
 or,  return-type function-name(type *arrayName){    //…body   
 } 

30) Define recursion.
➤➤Recursion is the process of invoking a function itself until some specific-conditions (also known as base conditions or base cases) are reached. When the base case is satisfies, the recursion terminated immediately by returning control to the caller one by one. 

31) Differentiate between structure and union.
➤➤There are two main differences between a structure and a union. (i) A structure allocates separate memory space for each of its members. On the other hand a union allocates a single memory space for all of its members. 
(ii) A structure can store value for all of its members at a time. But, a union can only store value for its one member at time. The union can only remember the last stored value of its member. 

32) In what general category do the #define and #include statement fall? 
➤➤ The #define and #include both are the preprocessor directives of C. 

33) What is the purpose of a header file? Is the use of a header file absolutely necessary?
➤➤The header files are used to store library functions of C. So, when a program needs to use library functions, it must include a header file that contains the required function. 

34) How can you link multi-file programs in C considering some of the called functions are in a different file? 
➤➤Multiple files of C program can be linked together and used in a single program by including them using #include directives. A C file can be included within another using one of the following ways:
 #include<file-name>
 It search the file in the specified include directory. e.g.:   #include<stdio.h> 
 #include”file-name”
 It search the file in the path preceded the file-name. If no path   is given then it searches the file in the source directory.
 e.g.:
 #include” D:\TC\X.C”  - the file X.C is in the TC directory in the   D drive.  

35) Mention the name of different types of storage class variable. 
➤➤ There are four different storage classes in C. These are:
 (i) Automatic storage class
 (ii) External storage class 
 (iii) Static storage class 
 (iv) Register storage class 

36) What is the purpose of register storage class? What is the scope of register variables? 
➤➤A variable which is accessed and/or updated frequently can be declared as a register variable. A register is a fastest memory of a computer. So, the register variables are used for their fastest response. The variables declared as register must not need to store in one of the system register; but, the system ensure that they can be accessible as fast as possible. 
 Syntax:
 register type identifier;
 e.g.: 
 register int x = 10; 

37) How the static variables are defined? 38) What is the purpose of static variable? What is its scope?
➤➤ Static variables are defined within a function in the same manner as automatic variables, except that the variable declaration must begin with the static keyword. Like automatic variables, the static variables are local to the function where they declared. But, unlike automatic variables, static variables retain their values throughout the life of the program.
 e.g.:
 void count()
 { 
 static int n = 1;
 printf(“%d”,n);
 n++; 
 }
 Now whenever the function count() is called, it prints the value   of  n that holds the no. of times the call has been made. 

39) How the extern variables are defined? 
➤➤ The variables declared outside all of the functions are known as external variables. The variables declared within a function with the extern modifier are also external variables. An external variable has the scope global to the program. That is why they are also called global variables. 
 e.g.:
 int a=10; 
 void msg()
 {  
 extern int b =10;
 printf(“a=%d, b=%d”,a,b);
 }
 Here in the above program a and b both are external variables. 
  
40) How is program execution initiated when parameters are passed to a program through command line?
➤➤The command line arguments are the strings separated by white-spaces and given by the user at the time of running a C program from command line. To read command line arguments the main() function must have the following form:
 void main(int argc, char *argv[]){ 
 …
 }
 Where argc will indicate the number of arguments passed and argv   holds the strings entered. 

41) Write down the statements that interchange the value of the two variables a and b, without using third variable. 
➤➤ a = a + b; //let a=10 and b=20, therefore now a=10+20=30
    b = a - b; //Now b=30-20=10
    a = a - b; //Now a=30-10=20 

42) Define an algorithm. 
➤➤ An algorithm is a precise specification of a sequence of instructions to be carried out in order to solve a given problem. Each instruction tells what task is to be performed. Actually an algorithm is a series of mathematical steps written in simple English sentence.  

43) What is data structure?
➤➤The data structure is the organizing of data in the memory and provides a specific logical or mathematical model to access those data efficiently. 

44) What is stack? 
➤➤ A stack is a linear list in which insertion and deletion of the elements must take place at one end, called top of the stack. That is why it is also known as Last In First Out (LIFO) system. 

45) What is a queue?
➤➤A queue is a linear list in which insertion can take place at one end, called rear of the queue and deletion can take place at other end, called front of the queue. That is why it is also known as First In First Out (FIFO) system. 

46) What is link list? 
➤➤A linked list is a linear list of nodes in which each node linked with its next node by containing its address. 

47) Define a graph. 
➤➤A graph is a non linear data structure that consists of a collection of nodes that are linked together using a relationship which is not necessarily hierarchical. 

48) Define a binary tree. Give example. 
➤➤A binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right". e.g.:  








49) Define a strictly binary tree. Give example. 
➤➤⃞⃞⃞  A strictly / full / proper binary tree (or 2-tree) is a tree in which every node other than the leaves has two children. e.g.:  








Fundamentals C Programs

1.  A program to evaluate the equation y=xn when n is a non-negative integer. #include<stdio.h>
#include<conio.h> 
void main()
{
 int count, n;
 float x,y;
 printf(“Enter the values of x and n:”);
 scanf(“%f%d”, &x,&n);
 y=1.0;
 count=1; 
 while(count<=n)
{
y=y*x; count++;
}
printf(“x=%f; n=%d; x to power n=%f”, x, n,y);
}

Output:

Enter the values of x and n: 2.5 4 X=2.500000; n=4; x to power n= 39.062500
2.  A program to print the multiplication table from 1*1 to 12*10. #include<stdio.h>
#include<conio.h>
            #define COLMAX 10
#define ROWMAX 12 void main()
{
int row, column, y; row=1;
 printf(“MULTIPLICATION TABLE ”);
 printf(“------------------------------------ ”);
do
{
column=1;
do
{
y=row*column; printf(“%d”, y); column=column +1;
}
while(column<=COLMAX); printf(“\n”);
row=row+1;
}
while(row<=ROWMAX);
printf(“---------------------------------------------- ”)
}

Output:

MULTIPLICATION TABLE

1        2        3        4        5        6        7        8        9     10
2        4        6        8        10      12      14      16      18      20
3        6        9        12      15      18      21      24      27      30
4        8        12      16      20      24      28      32      36      40
5        10      15      20      25      30      35      40      45      50
6        12      18      24      30      36      42      48      54      60
7        14      21      28      35      42      49      56      63      70
8        16      24      32      40      48      56      64      72      80



9        18      27      36      45      54      63      72      81      90
10      20      30      40      50      60      70      80      90      100
11        22      33      44      55      66      77      88      99      110
12      24      36      48      60      72      84      96      108    120
3.       Program uses a for loop to print the powers of 2 table for the power 0 to 20, both positive and negative.
#include<stdio.h> #include<conio.h> 
void main()
{
long int p; int n; double q;
printf(“------------------------------------------------------------- ”);
printf(“ 2 to power n        n       2 to power -n”);
printf(“------------------------------------------------------------- ”);
p=1;
for(n=0; n<21; ++n)
{
if(n==0)
P=1;
else
p=p*2;



                     }
             }

Output:



q=1.0/(double)p;
printf(“10ld 10%d %20.12lf”, p,n,q);
}
printf(“---------------------------------------------- ”);




2 to power n                             n                          2 to power -n

1
0
1.000000000000
2
1
0.500000000000



4
2
0.250000000000
8
3
0.125000000000
16
4
0.062500000000
32
5
0.031250000000
64
6
0.015625000000
128
7
0.007812500000
256
8
0.003906250000
512
9
0.001953125000
1024
10
0.000976562500
2048
11
0.000488281250
4096
12
0.000244140625
8192
13
0.000122070313
16384
14
0.000061035156
32768
15
0.000030517578
65536
16
0.000015258789
131072
17
0.000007629395
262144
18
0.000003814697
524288
19
0.000001907349
1048576
20
0.000000953674
4.       A class of n students take an annual examination in m subjects. A program to read the marks obtained by each student in various subjects and to compute and print the total marks obtained by each of them.
#include<stdio.h> #include<conio.h>
#define FIRST 360
# define SECOND 240
 void main()
{
int n, m, i, j, roll number, marks, total; 
printf(“Enter number of students and subjects”); scanf(“%d%d”, &n,&m);
printf(“\n”);
 for(i=1; i<=n; ++i)
{
printf(“Enter roll number:”);



scanf(“%d”, &roll_number);
 total=0;
printf(“Enter marks of %d subjects for ROLL NO”, m, roll number); for(j=1; j<=m; j++)
{
scanf(“%d”, &marks); total=total+marks;
}
printf(“TOTAL MARKS =%d”, total); if(total>=FIRST)
printf(“(First division)”);
 else if (total>=SECOND) printf(“(Second division)”);
 else printf(“(***FAIL***)”);
}
}

Output:

Enter number of students and subjects 3 6
Enter roll_number: 8701
Enter marks of 6 subjects for ROLL NO 8701 81 75 83 45 61 59
TOTAL MARKS =404 (First division) Enter roll_number:8702
Enter marks of 6 subjects for ROLL NO 8702 51 49 55 47 65 41
TOTAL MARKS =308(Second division) Enter roll_number: 8704
40 19 31 47 39 25
TOTAL MARKS=201(*** FAIL ***)
5.  The program illustrates the use of the break statement in a C program.
   #include<stdio.h>
#include<conio.h>
 void main()
{
int m;
float x, sum, average;
printf(“This program computes the average of a set of computers”); printf(“Enter values one after another”);
printf(“Enter a NEGATIVE number at the end”);
 sum=0;
for(m=1;m<=1000; ++m)
{
scanf(“%f”,&x); if(x<0)
break; 
sum+=x;
}
average=sum/(float)(m-1); printf(“\n”);
printf(“Number of values =%d”,m-1); printf(“sum=%f”, sum); 
printf(“Average=%f”, average);
}

Output:

This program computes the average of a set of numbers Enter values one after another
Enter a NEGATIVE number at the end 21 23 24 22 26 22 -1
Number of values =6 Sum= 138.000000
Average=23.000000
6.       A program to evaluate the series  1/1-x= 1+x+x2 +x3  +.... +xn.
#include<stdio.h> #include<conio.h> #define LOOP 100
#define ACCURACY 0.0001
void main()
{
int n;
float x, term, sum; 
printf(“input value of x :”); scanf(“%f”, &x);
sum=0;
for(term=1, n=1; n<=LOOP; ++n)
{
sum+=term; if(term<=ACCURACY)
 goto output;
term*=x;
}
printf(“FINAL VALUE OF N IS NOT SUFFICIENT TO ACHIEVE DESIRED ACCURACY”);
goto end;
 output:
printf(“EXIT FROM LOOP”);
printf(“sum=%f; no. of terms=%d”, sum,n); end:
;
}

Output:

Input value of x: .21 EXIT FROM LOOP
Sum=1.265800; No. of terms=7 Input value of x: .75
EXIT FROM LOOP
Sum=3.999774; No. of terms=34 Input value of x:.99
FINAL VALUE OF N IS NOT SUFFICIENT TO ACHIEVE DESIRED ACCURACY
7.  Program illustrates the use of continue statement. #include<stdio.h>
#include<conio.h> #include<math.h> 
void main()
{
int count, negative;
 double number, sqroot;
printf(“enter 9999 to STOP”); 
count=0;
negative=0; while(count<=100)
{
printf(“enter a number:”); scanf(“%lf”, &number); if(number==9999)
break; if(number<0)
{
printf(“Number is negative ”); negative++;
continue;
}
sqroot=sqrt(number);
printf(“Number=%lf square root=%lf ”, number, sqroot); count++;
}
printf(“Number of items done =%d”, count); printf(“Negative items=%d”, negative);
 printf(“END OF DATA”);
}

Output:

Enter 9999 to STOP 
Enter a number: 25.0 Number=25.000000
 Square root =5.000000
 Enter a number: 40.5
 Number =40.500000 
Square root=6.363961
 Enter a number:-9
           Number is negative
            Enter a number: 16 
            Number= 16.000000 
           Square root=4.000000 
            Enter a number: -14.75 
           Number is negative 
           Enter a number: 80
           Number=80.000000 
           Square root=8.944272

Enter a number: 9999
 Number of items done=4 Negative items=2
END OF DATA

8.  Program to print binomial coefficient table. #include<stdio.h>
#include<conio.h> #define MAX 10
 void main()
{
int m, x, binom; printf(“m x”);
for (m=0; m<=10; ++m)
printf(“----------------------------------------------------------- ”);
m=0;
do
{
printf(“%2d”, m); X=0; biom=1;
 while(x<=m)
{
if(m= =0 || x= =0) printf(“%4d”, binom);
 else
{
binom=binom* (m-x+1)/x; printf(“%4d”, binom);
}
x=x+1;
}
printf(“\n”);
M=m+1’
}
while(m<=MAX);
printf(“-------------------------------------------------------- ”);
}

Output:


Mx
0
1
2
3
4
5
6
7
8
9
10
0
1










0
1
1









2
1
2
1








3
1
3
3
1







4
1
4
6
4
1






5
1
5
10
10
5
1





6
1
6
15
20
15
6
1




7
1
7
21
35
35
21
7
1



8
1
8
28
56
70
56
28
8
1


9
1
9
36
84
126
126
84
36
9
1

10
1
10
45
120
210
252
210
120
45
10
1

9.  Program to draw a histogram. #include<stdio.h> 
    #include<conio.h>
#define N 5 void main()
{
int value[N]; 
int i, j, n, x;
for(n=0; n<N; ++n)
{
printf(“enter employees in group-%d: ”, n+1); scanf(“%d”, &x);
value[n]=x; 
printf(“%d”, value[n]);
}
printf(“\n”);
printf(“|\n”); 
for(n=0; n<N; ++n)
{
for(i=1; i<=3; i++)
{
if(i= =2)
printf(“ Group-%d | “, n+1);
 else
printf(“|”);
for(j=1;
 j<=value[n]; ++j)
 printf(“*”);
if(i= =2)
printf(“(%d)”, value[n]); 
else
printf(“\n”);
}
printf(“|\n”);
}
}

Output:

Enter employees in Group -1:12 12
Enter employees in Group -2:23 23
Enter employees in Group -3:35 35
Enter employees in Group -4:20 20



Enter employees in Group -5:11 11

|
|************ Group-1                  |************ (12)
|************
|
|*********************** Group-2           |***********************(23)
|***********************
|
|*********************************** Group-3           |***********************************(35)
|***********************************
|
|******************** Group-4           |********************(20)
|********************
|
|*********** Group-5             |***********(11)
|***********
|

10.   Program of minimum cost problem. #include<stdio.h>
#include<conio.h>
 void main()
{
float p, cost, p1, cost1; 
for(p=0; p<=10; p=p+0.1)
{
cost=40-8*p+p*p;
 if(p= =0)
{
cost1=cost; continue;
}
if(cost>=cost1) break; cost1=cost; 
p1=p;
}
p=(p+p1)/2.0; cost=40-8&p+p*p;
printf(“MINIMUM COST=%.2f AT p=%.1f”, cost, p);
}

Output:

MINIMUM COST = 24.00 AT p=4.0

11.  Program for plotting of two functions (y1=exp(-ax); y2=exp(-ax2 /2)). #include<stdio.h>
#include<conio.h> #include<math.h> 
void main()
{
int i;
float a, x, y1, y2; a=0.4;
printf(“                                          Y------->                           ”  );
 printf(“0----------------------------------------------------------------------- ”);
for(x=0; x<5; x=x+0.25)
 y1=(int) (50*exp(-a*x)+0.25);
y2=(int) (50*exp(-a*x*x/2)+0.5); if(y1= =2)
{
if(x= = 2.5)

                     printf(“X |”);
             else printf(“|”);
for(i=1; i<=y1-1; ++i) printf(“”);
printf(“#”); continue;
}
if(y1>y2)
{
if(x= =2.5) 
printf(“X |”);
 else
printf(“ |”);
for(i=1; i<=y2-1; ++i) printf(“ ”);
printf(“*”);
for(i=1; i<=(y1-y2-1); ++i) printf(“_”);
printf(“0”); continue;
} if(x==2.5)
printf(“X|”); 
else
printf(“ |”);
for(i=1; i<=y1-1; ++i) printf(“ ”);
printf(“0”);
for(i=1; i<=(y2-y1-1); ++i) printf(“_”);
printf(“*”);
}
}



Output:

Y--- >
0--------------------------------------------------------------------------------------------------------
|                                                                                                       #
|                                                                                                   0---*
|                                                                                             0---*
|                                                                                           0---*
|                                                                                    0---*
|                                                                                 0---*
|                                                                          0---*
|                                                                 0---*
|                                                            0---*
|                                                      0---*
|                                                       #


12.        Write a program using a single –subscribed variable to evaluate the following expressions:




Ã¥ X
 
10

2
 
Total =          2


the values of x1, x2,..... are read from the terminal.


i = 1

#include<stdio.h> #include<conio.h>
void main()
{
int i;
float x[10], value, total;
printf(“ENTER 10 REAL NUMBERS”);
for(i=0; i<10; i++)
{
scanf(“%f”, &value); x[i]=value;
}
total=0.0;
for(i=0; i<10; i++)
total = total + x[i]*x[i]; printf(“\n”);
for(i=0; i<10; i++)
printf(“x[%2d] = %5.2f”, i+1, x[i]); printf(“total=%.2f”, total);
}

Output:

ENTER 10 REAL NUMBERS
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.10
X[1]=1.10
X[2]=2.20
X[3]=3.30
X[4]=4.40
X[5]=5.50
X[6]=6.60
X[7]=7.70
X[8]=8.80
X[9]=9.90 X[10]=10.10

Total = 446.86

13.   Given below is the list of marks obtained by a class of 50 students in an annual examination. 43 65 51 27 79 11 56 61 82 09 25 36 07 49 55 63 74 81 49 37 40 49 16 75 87 91 33 24 58 78 65 56 76 67 45 54 36 63 12 21 73 49 51 19 39 49 68 93 85 59
Write a program to count the number of students belonging to each of the following groups of marks: 0-9,10-19,20 -29, ,100.

#include<stdio.h> #include<conio.h> 
#define MAXVAL 50
#define COUNTER 11
 void main()
{
float value [MAXVAL];
 int i, low, high;
int group[COUNTER]={0,0,0,0,0,0,0,0,0,0,0};
for(i=0; i<MAXVAL; i++)
{
scanf(“%f”, &value[i]);
++group[(int)(value[i]/10)];
}
printf(“\n”);
printf(“GROUP RANGE FREQUENCEY”);
 for(i=0; i<COUNTER; i++)
{
low=i*10; 
if(i= =10) high=100; else high=low+9;
printf(“%2d%3d to %3d%d”, i+1, low, high, group[i]);
}
}

Output:

43 65 51 27 79 11 56 61 82 09 25 36 07 49 55 63 74 81 49 37 40 49 16 75 87 91 33 24 58 78
65 56 76 67 45 54 36 63 12 21 73 49 51 19 39 49 68 93 85 59                (Input data)


GROUP
RANGE
FREQUENCY
1
0 to 9
2
2
10 to 19
4
3
20 to 29
4
4
30 to 39
5
5
40 to 49
8
6
50 to 59
8
7
60 to 69
7



8
70 to 79
6
9
80 to 89
4
10
90 to 99
2
11
100 to 100
0

14.   Write a program for sorting the elements of an array in descending order.
      #include<stdio.h>
#include<conio.h> 
void main()
{
int *arr, temp, i, j, n; clrscr();
printf(“enter the number of elements in the array”); scanf(“%d”, &n);
arr=(int*)malloc(sizeof(int)*n); for(i=0;i<n;i++)
{
for(j=i+1; j<n; j++)
{
if(arr[i]<arr[j])
{
temp=arr[i]; arr[i]=arr[j]; arr[j]=temp;
}
}
printf(“Elements of array in descending order are”);
 for(i=0; i<n; i++);
getch();
}

Output:

Enter the number of elements in the array: 5 Enter a number: 32
Enter a number: 43 Enter a number: 23



Enter a number: 57 Enter a number: 47

Elements of array in descending order are: 57
47
43
32
23

15.   Write a program for finding the largest number in an array .
      #include<stdio.h>
#include<conio.h>
 void main()
{
int *arr, i, j, n, LARGE; clrscr();
printf(“Enter the number of elements in the array”); scanf(“%d”, &n);
arr=(int*) malloc(sizeof(int)*n); for(i=0; i<n; i++)
{
printf(“Enter a number”); scanf(“%d”, &arr[i]);
} LARGE=arr[0];
for(i=1; i<n;i++)
{
if(arr[i]>LARGE) LARGE=arr[i];
}
printf(“The largest number in the array is : %d”, LARGE); getch();
}



Output:

Enter the number of elements in the array:5 Enter a number: 32
Enter a number: 43 Enter a number: 23 Enter a number: 57 Enter a number: 47
The largest number in the array is : 57

16.   Write a program for removing the duplicate element in an array. 
      #include<stdio.h>
#include<conio.h> #include<stdlib.h> 
void main()
{
int *arr, i, j, n, x, temp; clrscr();
printf(“Enter the number of elements in the array”); scanf(“%d”, &n);
arr=(int*) malloc(sizeof(int)*n); for(i=0; i<n; i++)
{
printf(“Enter a number”); scanf(“%d”, &arr[i]);
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr[i]>arr[j])
{
temp=arr[i]; arr[i]=arr[j]; arr[j]=temp;
}
}
}
printf(“Elements of array after sorting”); for(i=0; i<n; i++)
printf(“%d”, arr[i]);
i=0;
j=1;
while(i<n)
{
if(arr[i]==arr[j])
{
for(x=j; x<n-1; x++) arr[x]=arr[x+1];
n-;
}
else
{
i++; 
j++;
}
}
printf(“Elements of array after removing duplicate elements”);
 for(i=0; i<=n; i++)
printf(“%d”, arr[i]); 
getch();
}

Output:

Enter the number of elements in the array:5 Enter a number: 3
Enter a number: 3 Enter a number: 4 Enter a number: 6 Enter a number: 4



Elements of array after sorting:
3
3
4
4
6
Elements of array after removing duplicate elements: 3
4
6

17.   Write a program for finding the desired kth smallest element in an array.
      #include<stdio.h>
#include<conio.h>
 void main()
{
int *arr, i, j, n, temp, k; clrscr();
printf(“enter the number of elements in the array”); scanf(“%d”, &n);
arr=(int*)malloc(sizeof(int)*n); for(i=0;i<n;i++)
{
printf(“Enter a number”); scanf(“%d”, &arr[i]);
}
for(j=i+1; j<n; j++)
{
if(arr[i]<arr[j])
{
temp=arr[i]; arr[i]=arr[j]; arr[j]=temp;
}
}
}
printf(“Elements of array after sorting”); for(i=0; i<n; i++);
printf(“%d”, arr[i]);
printf(“Which smallest element do you want to determine”); scanf(“%d”, k);
if(k<=n)
printf(“Desired smallest element is %d”, arr[k-1]);
 else
printf(“Please enter a valid value for finding the particular smallest element”); getch();
}

Output:

Enter the number of elements in the array:5 Enter a number: 33
Enter a number: 32 Enter a number: 46 Enter a number: 68 Enter a number: 47
Elements of array after sorting:
32
33
46
47
68
Which smallest element do you want to determine? 3 Desired smallest element is 46

18.   Program to sort a list of numbers and to determine median. #include<stdio.h>
#include<conio.h> #define N 10
            void main()
{
int i, j, n;
float median, a[N], t;
printf(“Enter the number of items”); scanf(“%d”, &n);
printf(“Input %d values ”, n); for(i=1; i<=n; i++) 
scanf(“%f”, &a[i]);
for(i=1; i<=n-1; i++)
{
for(j=1; j<=n-1; j++)
{
if(a[j]<=a[j+1]) t=a[j]; a[j]=a[j+1]; a[j+1]=t;
}
else continue;
}
}
if(n%2 = =0) 
median=(a[n/2]+a[n/2+1])/2.0; 
else
median=a[n/2+1]; 
for(i=1; mi<=n; i++) printf(“%f”, a[i]);
printf(“median is %f”, median);
}

Output:

Enter the number of items 5
Input 5 values
1.111 2.222 3.333 4.444 5.555



5.555000 4.444000 3.333000 2.222000 1.111000
Median is 3.333000

Enter the number of items 6
Input 6 values
3 5 8 9 4 6

9.000000 8.000000 6.000000 5.000000 4.000000 3.000000
Median is 5.500000

19.   Program to calculate standard deviation. #include<stdio.h>
#include<conio.h> #include<math.h> 
#define MAXSIZE 100 
void main()
{
int i, n;
float value [MAXSIZE], deviation; sum=sumsqr=n=0;
printf(“Input values: input -1 to end”); for(i=1; i<MAXSIZE; i++)
{
scanf(“%f”, &value[i]); if(value[i]= =-1)
break; sum+=value[i]; n+=1;
}
mean=sum/(float)n; 
for(i=1; i<=n; i++)
{
deviation=value[i]-mean; sumsqr+=deviation* deviation;
}
variance=sumsqr/(float)n; stddeviation=sqrt(variance); 
printf(“Number of items: %d”, n); printf(“Mean: %f”,mean);
printf(“Standard deviation:%f”, stddeviation);
}

Output:

Input values: input -1 to end 65 9 27 78 12 20 33 49 -1

Number of items: 8 Mean: 36.625000
Standard deviation: 23.510303

20.   Program to evaluate responses to a multiple-choice test. #include<stdio.h>
#include<conio.h> 
#define STUDENTS 3
#define ITEMS 25 
void main()
{
char key[items+1], response[ITEMS+1]; 
int count, i, student, n, correct[ITEMS+1]; printf(“Input key to the items”);
for(i=0; i<ITEMS; i++) scanf(“%c”, &key[i]);
scanf(“%c”, &key[i]); key[i]=’\0’;
for(student=1; student<=STUDENTS; student++)
{
count=0;
printf(“\n”);
printf(“Input responses of student -%d”, student); 
for(i=0; i<ITEMS; i++)
scanf(“%c”, &response[i]);
scanf(“%c”, &response[i]); 
response[i]=’\0’;
for(i=0; i<ITEMS; i++) correct[i]=0;
for(i=0; i<ITEMS; i++) if(response[i]= = key[i])
{
count=count+1; count[i]=1;
}
printf(“\n”);
printf(“student-%d”, student);
printf(“score is %d out of %d”, count, ITEMS); printf(“Response to the items below are wrong”); n=0;
for(i=0; i<ITEMS; i++) if(correct[i]= =0)
{
printf(“%d”, i+1); n=n+1;
}
if(n= =0) printf(“NIL”);
printf(“\n”);
}
}

Output:

Input key to the items abcdabcdabcdabcdabcdabcda



Input responses of student-1 abcdabcdabcdabcdabcdabcda

student-1
score is 25 out of 25
response to the following items are wrong NIL

Input responses of student-2 abcddcbaabcdabcdddddddddd

student-2
score is 14 out of 25
Response to the following items are wrong 5 6 7 8 17 18 19 21 22 23 25

Input responses of student-3 aaaaaaaaaaaaaaaaaaaaaaaa

student-3
score is 7 out of 25
Response to the following items are wrong
2 3 4 6 b7 8 10 11 12 14 15 16 18 19 20 22 23 24

21.   Program for production and sales analysis. #include<stdio.h>
#include<conio.h>
 void main()
{

int M[5][6], s[5][6], c[6], Mvalue[5][6], Svalue [5][6],Mweek[5], Sweek[5], Mproduct[6], Sproduct[6], Mtotal, Stotal, i, j, number;
printf(“Enter products manufactured week wise”);
            printf(“M11, M12,-, M21, M22, - etc”);
for(i=1; i<=4; i++)
 for(j=1; j<=5; j++) scanf(“%d”, &M[i][j]);
printf(“Enter products sold week wise”); printf(“S11, S12,-, S21,S22,- etc”);
for(i=1; i<=4; i++)
 for(j=1; j<=5; j++) scanf(“%d”, &S[i][j]);
printf(“enter the cost of each product”); for(j=1;j<=5; j++)
scanf(“%d”, &C[j]);
 for(i=1; i<=4; i++)
 for(j=1; j<=5; j++)
{
Mvalue[i][j]=M[i][j]*c[j];
Svalue[i][j]=S[i][j]*c[j];
}
for(i=1; i<=4; i++)
{
Mweek[i]=0; Sweek[i]=0;
 for(j=1; j<5; j++)
{
Mweek[i] +=Mvalue[i][j];
Sweek[i]+= Svalue[i][j];
}
}
for(j=1; j<=5; j++)
{
Mproduct[j]=0; Sproduct[j]=0;
for(i=1; i<=4; i++)
{
Mproduct[i] +=Mvalue[i][j];
Sproduct[i]+= Svalue[i][j];
}
Mtotal=stotal=0; for(i=1; i<=4; i++)
{
Mtotal+=Mweek[i];
Stotal+=Sweek[i];
}
            printf(“\n\n”);
printf(“Following is the list of things you can ”);
printf(“request for enter appropriate item number and press RETURN key”); printf(“1. Value matrices of production & sales”);
printf(“2. Total value of weekly production & sales”); printf(“3. Product-wise monthly value of production& sales”); printf(“4. Grand total value of production & sales”); printf(“5.Exit”);
number=0; while(1)
{
printf(“ENTER YOUR CHOICE:”);
scanf(“%d”, &number); printf(“\n”);
if(number= =5)
{
printf(“GOOD BYE”); break;
}
switch(number)
{
case 1:
printf(“VALUE MATRIX OF PRODUCTION”);
for(i=1; i<=4; i++)
{
printf(“Week (%d), i”);

for(j=1; j<=5; j++)
 printf(“%7d”, Mvalue); printf(“\n”);
}
printf(“VALUE MATRIX OF SALES”);
for(i=1; i<=4; i++)
{
printf(“Week(%d)”, i); 
for(j=1; j<=5; j++) 
printf(“%7d”, Svalue[i][j]); printf(“\n”);
}
break; case 2:
printf(“TOTAL WEEKLY PRODUCTION & SALES”); printf(“                  PRODUCTION SALES”);
printf(“                                  --------------         ----       ”);
for(i=1; i<=4; i++)
{
printf(“week(%d”, i);
printf(“%7d%7d”, Mweek[i], Sweek[i]);
}
break; case 3:
printf(“PRODUCT WISE TOTAL PRODUCTION & SALES”); printf(“             PRODUCTION SALES”);
printf(“
--------------
----
”);
for(j=1; j<=5; j++)



{



printf(“Product(%d”,ji);



printf(“%7d%7d”, Mproduct[j], Sproduct[j]);
}
break; case 4:
printf(“GRAND TOTAL OF PRODUCTION SALES”);



printf(“Total production=%d”, Mtotal); break;
default:
printf(“Wrong choice, select gain”); break;
}
}
Printf(“Exit from the program”);
}

Output:

Enter products manufactured week wise M11, M12, ----, M21, M22, etc
11
15
12
14
13
13
13
14
15
12
12
16
10
15
14
14
11
15
13
12

Enter products sold week wise S11,  S12, ----, S21, S22,------------------------------ etc
10     13      9        12      11
12     10      12      14      10
11       14      10      14      12
12     10      13      11        10

Enter cost of each product 10 20 30 15 25

Following is the list of things you can request for enter appropriate item number and press RETURN key.
1.  Value matrices of production & sales
2.  Total value of weekly production & sales
3.  Product-wise monthly value of production & sales
4.  Grand total value of production & sales
5.  Exit



Enter your choice: 1
VALUE MATRIX OF PRODUCTION

Week (1)
110
300
360
210
325
Week (2)
130
260
420
225
300
Week (3)
120
320
300
225
350
Week (4)
140
220
450
210
300

VALUE MATRIX OF SALES

Week (1)
100
260
270
180
275
Week (2)
120
200
360
210
250
Week (3)
110
280
300
210
300
Week (4)
120
200
390
165
250

Enter your choice: 2
TOTAL WEEKLY PRODUCTION & SALES PRODUCTION SALES

Week(1)
1305

1085
Week(2)
1335

1140
Week(3)
1305

1200
Week(4)
1315

1125

Enter your choice: 3
PRODUCT WISE TOTAL PRODUCTION & SALES PRODUCTION  SALES

Product(1)
500

450
Product(2)
1100

450
Product(3)
1530

450
Product(4)
855

450
Product(5)
1275

1075

Enter your choice: 4
GRAND TOTAL OF PRODUCTION SALES
Total production=5260



Total sales=4550
ENTER YOUR CHOICE:5 GOOD BYE
Exit from the program

22.   Write a program to read a series of words from a terminal using scanf function. 
      #include<stdio.h>
#include<conio.h> 
void main()
{
char word1[40], word2[40], word3[40], word4[40]; printf(“enter text:”);
scanf(“%s%s”, word1, word2);
 scanf(“%s”, word3);
scanf(“%s”, word4);
printf(“\n”);
printf(“word1=%s\nword2=%s\n”, word1, word2); printf(“word3=%s\nword4=%s\n”, word3, word4”);
}

Output:

Enter text:
Seventh Street, sakthinagar, erode

Word1=seventh Word2=street Word3=sakthinagar Word4=erode

23.        Program to read a line of text from terminal. #include<stdio.h>
#include<conio.h> void main()
{
char line[81], character;
                     int c;
                     c=0;
printf(“Enter text. Press<return> at end”);
 do
{
charcter=getchar(); line[c]=character;
 c++;
}
while(character!=’\n’); 
c=c-1;
line[c]=’\0’; 
printf(“\n%s\n”, line);
}

Output:

Enter text.press<return> at end Programming in c is interesting Programming in c is interesting Enter text. Press <Return> at end

24.   Write a program to copy one string into another and count the number of characters copied.
      #include<stdio.h>
#include<conio.h> 
void main()
{
char string1[80], string2[80];
 int i;
printf(“Enter a string\n”); printf(“?”);
scanf(“%s, string2”);
for(i=0; string2[i]!=’\0’; i++) string1[i]=string2[i];
string1[i]=’\0’;
printf(“\n”);
printf(“%s\n”, string1);
printf(“number of charcters = %d\n”, i);
}

Output:

Enter a string
? Manchester Manchester
Number of characters=10

Enter a string
?westminister

Westiminister
Number of characters=11

25.   Program for printing of the alphabet set in decimal and character form.
      #include<stdio.h>
#include<conio.h> 
void main()
{
char c; printf(“\n\n”);
for(c=65; c<=122; c=c+1)
{
if(c>90&&c<97) continue;
printf(“|%4d-%c”, c,c);
}
printf(“|n”);
}

Output:

|65-A|66-B|67-C|68-D|69-E|70-F



|71-G|72-H|73-I|74-J|75-K|76-L
|77-M|78-N|79-O|80-P|81-Q|82-R
|83-S|84-T|85-U|86-V|87-W|88-X
|89-Y|90-Z|97-A|98-B|99-C|100-d
|101-e|102-f|103-g|104-h|105-i|106-j
|107-k|108-l|109-m|110-n|111-o|112-p
|113-q|114-r|115-s|116-t|117-u|118-v
|119-w|120-x|121-y|122-z|

26.   Program to concatenation of strings. #include<stdio.h>
#include<conio.h>
 void main()
{
int i, j, k;
char first_name[10]={ANANDA};
char second_name[10]={MURUGAN};
 char last_name[10] = {SELVARAJ}; 
 char name[30];
for(i=0; first _name[i]!=‘\0’; i++) name[i]=first_name[i]; name[i]=’ ’;
for(j=0; second _name[j]!=‘\0’;j++) name[i+j+1]=second_name[j]; 
name[i+j+1]=’ ’;
for(k=0; last _name[k]!=‘\0’;k++) name[i+j+k+2]=‘\0’; 
printf(“\n\n”);
printf(“%s\n”, name);
}

Output:

ANANDA MURUGAN SELVARAJ



27.   Program to illustration of string handling functions. #include<stdio.h>
#include<conio.h> #include<string.h>
void main()
{
char s1[20], s2[20], s3[20];
int x, l1,l2,l3;
printf(“enter two string constants”); printf(“?”);
scanf(“%s %s”, s1,s2); x=strcmp(s1,s2); if(x!=0)
{
printf(“strings are not equal”); strcat(s1, s2);
}
else
printf(“strings are equal”); strcpy(s3,s1); l1=strlen(s1); l2=strlen(s2); l3=strlen(s3);
printf(“s1=%s length =%d characters”, s1,l1); printf(“s2=%s length =%d characters”, s2,l2); printf(“s3=%s length =%d characters”, s3,l3);
}

Output:

Enter two string constants
? ananda murugan

Strings are not equal
S1=ananda murugan length=13 characters



S2=murugan           length=7 characters S3=ananda murugan length=13 characters

Enter two string constants
? anand anand Strings are equal
S1=anand length=5 characters S2=anand length=5 characters S3=anand length=5 characters

28.   Write a program that would sort a list of names in alphabetical order.
      #include<stdio.h>
#include<conio.h> #define ITEMS 5
#define MAXCHAR 20
 void main()
{
char string[ITEMS][MAXCHAR], dummy[MAXCHAR];
 int i=0, j=0;
printf(“enter names of %d items”, ITEMS); while(i<ITEMS)
scanf(“%s”, string[i++]); 
for(i=1; i<ITEMS; i++)
{
for(j=1; j<=ITEMS-i; j++)
{
if(strcmp(string[j-1), string[j]>0)
{
strcpy(dummy, string[j-1]); strcpy(string[j-1], string[j]); strcpy(string[j], dummy);
}
}
}



printf(“alphabetical list”); for(i=0; i<ITEMS; i++) printf(“%s”, string[i]);
}

Output:

enter names of 5 times

Ananda
murugan renuka devi shri alphabetical list
Ananda devi murugan renuka shri

29.   Programs for counting of characters, words and lines in a text. #include<stdio.h>
#include<conio.h> 
void main()
{
char line[81], ctr;
int i, c, end=0, characters =0, words=0. lines=0; printf(“KEY IN THE TEXT”);
printf(“GIVE ONE SPACE AFTER EACH WORD WHEN COMPLETED, PRESS RETURN”);
while(end= =0)
c=0;
while((ctr=getchar()) !=’\n’) line[c++]=ctr;
line[c]=’\0’;
if(line[0] = =’\0’) break;
else
{
words++;
for(i=0; line[i]!=’\0’; i++)
if(line[i] = =’ ‘ || line[i] = = ‘\t’) words++;
}
lines=lines+1;
characters =characters + strlen(line);
}
printf(“\n”);
printf(“number of lines=%d”, lines);
 printf(“number of words=%d”, words); printf(“number of characters=%d”, characters);
}

Output:

KEY IN THE TEXT
GIVE ONE SPACE AFTER EACH WORD WHEN COMPLETED, PRESS RETURN

Admiration is a very short-lived passion. Admiration involves a glorious obliquity of vision.
Always we like those who admire us but we do not like those whom we admire. Fools admire, but men of sense approve

Number of lines=4 Number of words=36 Number of characters=205

30.   Program to alphabetize a customer list. #include<stdio.h>
#include<conio.h>
#define CUSTOMERS 10
 void main()
{
char first_name[20] [10], second_name[20][10], surname[20][10], name[20][20], telephone[20][10], dummy[20];
int i,j;
printf(“input names and telephone numbers”); printf(“?”);
for(i=0; i<CUSTOMERS; i++)
{
scanf(“%s %s %s %s”, first_name[i], second_name[i], surname[i], telephone[i]); strcpy(name[i], surname[i]);
strcat(name[i], “ ,”); dummy[0]=first_name[i][0]; dummy[1]=’\0’; strcat(name[i], dummy); strcat(name[i], “.”);
dummy[0]=second_name[i][0]; dummy[1]=’\0’;
strcat(name[i], dummy);
}
for(i=1; i<=CUSTOMERS-1; i++) for(j=1; j<=CUSTOMERS-i; j++)
if(strcmp(name[j-1], name[j])>0)
{
strcpy(dummy, name[j-1]); strcpy(name[j-1], name[j]); strcpy(name[j], dummy);
strcpy (dummy, telephone[j-1]); strcpy(telephone[j-1], telephone[j]); strcpy(telephone[j], dummy);
}
printf(“CUSTOMERS LIST IN ALPHABETICAL ORDER”);



for(i=0; i<CUSTOMERS; i++)
printf(“%-20s\t%-10s”, name[i], telephone[i]);
}

Output:

Input names and telephone numbers
? anandamurugan 9486153102
Renukadevi 9486644542

CUSTOMERS LIST IN ALPHABETICAL ORDER

anandamurugan 9486153102
Renukadevi 9486644542

31.   Program for functions with no arguments and no return values #include<stdio.h>
#include<conio.h>
 void printline(void);
 void value (void); 
void main()
{
printline(); 
value(); printline();
}
void printline(void)
{
int i;
for(i=1; i<=35; i++) printf(“%c”, ‘-‘); printf(“\n”);
}
void value(void)
{
int year, period;



float inrate, sum, principal; printf(“principal amount?”); scanf(“%f”, &principal); printf(“interest rate?”); scanf(“%f”, &inrate); printf(“period?”); 
scanf(“%d”, &period); sum=principal;
year=1;
 while(year<=period)
{
sum=sum*(1+inrate); year=year+1;
}
printf(“\n%8.2f %5.2f %5d %12.2f\n”, principal, inrate, period, sum);
}


Output:

Principal amount? 5000

Interest rate? Period?
0.12

5

5000.00
0.12
5
8811.71


32.   Program for functions with arguments but no return values. #include<stdio.h>
#include<conio.h>
 void printline(char c);
void value (float, float, int); 
void main()
{

                   float principal, inrate; int period;
printf(“Enter principal amount, interest”); printf(“rate and period”);
scanf(“%f %f %d”, & principal, &inrate, &period); printline(‘c’);
}
void printline(char ch)
{
int i;
for(i=1; i<=52; i++) printf(“%c”, ch);
printf(“\n”);
}
void value(float p, float r, int n)
{
int year; float sum; sum=p; year=1;
while(year<=n)
{
sum=sum*(1+r); year=year+1;
}
printf(“%f\t%f\t%d\t%f\n”, p, r,n,sum);
}

Output:

Enter principal amount, interest rate, and period 5000 0.12 5
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ 5000.000000         0.120000     5         8811.708984
cccccccccccccccccccccccccccccccccccccccccccccccccccccc



33.   Program for functions with arguments and return values. #include<stdio.h>
#include<conio.h>
void printline(char ch, int len);
 value (float, float, int);
void main()
{
float principal, inrate, amount; 
int period;
printf(“enter principal amount, interest”); printf(“rate, and period”);
scanf(“%f%f%d”, &principal, &inrate, &period); printline(‘*’, 52);
amount=value(principal, inrate, period); printf(“\n%f\t%f\t%f\t%d\t%f\n\n”, principal, inrate, period, amount); printline();
}
void printline(char ch, int len)
{
int i;
for(i=1; i<=len; i++) printf(“%c”,ch);
printf(“\n”);
}
value(float p, float r, int n)
{
int year; float sum; sum=p; year=1;
while(year<=n)
{
Sum=sum*(1+r);



year=year+1;
}
return(sum);
}

Output:

Enter principal amount, interest rate, and period 5000  0.12           5
**************************************************************************** 5000.000000                  0.1200000    5        8811.00000
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = =
34.        Write a function power that computes x raised to the power y for integers x and y and returns double-type value.
#include<stdio.h> #include<conio.h>
void main()
{
int x,y;
double power (int, int); printf(“enter x,y: ”);
scanf(“%d%d”, &x,&y);
printf(“%d to power %d is %f”, x,y, power (x,y));
}
double power (int x, int y);
P=1.0;
if(y>=0) 
while(y--) p*=x; else
while(y++) p/=x; return(p);
}



Output:

Enter x,y: 16         2
16 to power 2 is 256.000000

Enter x,y : 16 -2
16 to power -2 is 0.003906

35.   Write a program to show how user-defined function is called. #include<stdio.h>
#include<conio.h>
 void main()
{
int x=1, y=2, z; 
z=add(x,y);
printf(“z=%d”, z);
}
add(a,b);
{
return(a+b);
}

Output:

z=3

36.   Write a program to define user-defined function. Call them at different places.
      #include<stdio.h>
#include<conio.h>
 void y();
voidy()
{
printf(“Y”); void main()
{
             void a(), b(), c(), d(); return();
clrscr();
y();
a();
b();
c();
d();
}
void a()
{
printf(“A”); y();
}
void b()
{
printf(“B”); a();
}
void c()
{
a();
b();
printf(“C”);
}
void d()
{
printf(“D”); c();
b();
a();
}

Output:

YAYBAYAYBAYCDAYBAYCBAYAY



37.   Write a program to show how similar variable names can be used in different functions.
      #include<stdio.h>
#include<conio.h>
 void main()
{
int b=10, c=5; return();
printf(“In main() B=%d C=%d”, b,c); 
fun();
}
fun()
{
int b=20, c=10;
printf(“In fun() B=%d c=%d”, b,c);
}

Output:

In main() B=10 c=5 In fun() B=20 c=10

38.   Write a program to show the effect of global variables on different functions. 
      #include<stdio.h>
#include<conio.h> int b=10, c=5; 
void main()
{
clrscr();
printf(“In main () B=%d c=%d”, b,c);
 fun();
b++; c--;
printf(“Again In main () B=%d c=%d”, b,c);
}
fun()
b++; c--;
printf(“ In fun () B=%d c=%d”, b,c);
}

Output:

In main() B=10 c=5 In fun() B=11 c=4
Again In main() B=12 c=3

39.   Write a program to display message using user-defined function. #include<stdio.h>
#include<conio.h> 
void main()
{
void message(); message();
}
void message()
{
puts (“Have a nice day”);
}

Output:

Have a nice day

40.   Write a program to return more than one value from user defined function.
      #include<stdio.h>
#include<conio.h>
 void main()
{
int x,y, add, sub, change(int*, int*, int*, int*); clrscr();
printf(“Enter values of X and Y:”); scanf(“%d %d”, &x, &y); 
change(&x &y, &add, &sub); printf(“Addition:%d”, add); printf(“Subtraction:%d”, sub);
 return 0;
}
change(int * a, int*b, int*c, int d)
{
c=*a+*b;
*d=*a-*b;
}

Output:

Enter values of x & y: 5 4 Addition: 9
Subtraction:1

41.   Write a program to pass arguments to user-defined function by value and by reference. 
     #include<stdio.h>
#include<conio.h>
 void main()
{
int k, m, other(int, int*); clrscr();
printf(“Address of k&m in main(): %u%u”, &k&m); other(k&m);
return 0;
}
other(int k, int*m)
{
printf(“Address of k&m in other(): %u%u, &k,m”);
}



Output:

Address of k&m in main(): 65524 65522 Address of k&m in other(): 65518 65522

42.   Write a program to return only absolute value like abs() function. #include<stdio.h>
#include<conio.h> 
void main()
{
int uabs(int), x; clrscr();
printf(“enter a negative value:”);
 scanf(“%d”, x);
x=uabs(x);
 printf(“X=%d”, x); return 0;
}
uabs(int y)
{
if(y<0) return(y*-1); else return(y);
}

Output:

Enter a negative value: -5 X=5

43.        Write a program to calculate square and cube of an entered number. Use function as an argument.
#include<stdio.h> #include<conio.h> 
void main()
{
int m; clrscr();
printf(“Cube: %d”, cube(sqr(input())));
}
input()
{
int k; printf(“Number:”);
scanf(“%d”, &k); return k;
}
sqr(m)
{
printf(“Square:%d”, m*m);
 return m;
}
cube(m)
{
return m*m*m;
}

Output:

Number:2 Square: 4 Cube:8

44.   Write a program to assign return value of a function to another variable.
      #include<stdio.h>
#include<conio.h> 
void main()
{
int input(int); int x; clrscr(); x=input(x);



printf(“x=%d”, x);
}
input(int k)
{
printf(“Enter value of x=”); scanf(“%d”, &k);
 return(k);
}

Output:

Enter value of x=5 x=5

45.   Write a program to perform addition and subtraction of numbers with return value of function. 
      #include<stdio.h>
#include<conio.h> #include<math.h>
 void main()
{
int input(int);
int sqr(int); 
int x; 
clrscr();
x=sqr(1-input(x)+1); printf(“Square=%d, x”);
}
input(int k)
{
printf(“Enter value of x=”); scanf(“%d, &k”); return(k);
{
sqr(int m)
{



return (pow(m,2));
}

Output:

Enter value of x=5 Square=9

46.      Write a program to perform multiplication and division of numbers with return value of function.
#include<stdio.h> #include<conio.h> #include<math.h> 
void main()
{
int input(int); 
int sqr(int); int x; clrscr();
x=sqr(5*input(x)/2); printf(“Square=%d, x”);
}
input(int k)
{
printf(“Enter value of x=”); scanf(“%d, &k”); return(k);
{
sqr(int m)
{
return (pow(m,2));
}

Output:

Enter value of x=5 Square=144



47.   Write a program to use (++) operator with return value of function. 
      #include<stdio.h>
#include<conio.h> #include<math.h>
 void main()
{
int input(int);
 int sqr(int); 
irit x,y =0; 
clrscr();
x=sqr(++(y-(input(x))));
 printf(“Square=%d”, x);
}
input(int k)
{
printf(“Enter value of x=”); scanf(“%d”, &k); return(k);
}
sqr(int m)
{
return (pow(m,2));
}

Output:

Enter value of x=7 Square=64

48.   Write a program to use mod(%) with function. #include<stdio.h>
#include<conio.h>
 void main()
{
int j();
if(j()%2= =0)
            printf(“Number is even”); else
printf(“Number is odd”);
 return 0;
}
j()
{
int x; clrscr();
printf(“Enter a number”); scanf(“%d”, &x);
 return(x);
}

Output:

Enter a number:5 Number is odd

49.   Write a program to evaluate the equation s=sqr(a()+b()) using function. 
      #include<stdio.h>
#include<conio.h> 
void main()
{
int s=0, a(), b(),sqr(int); clrscr(); 
s=sqr(a()+b());
printf(“square of sum=%d”, s);
 return 0;
}
a()
{
int a;
printf(“Enter value of a:”); scanf(“%d”, &a); return(a);
}
b()
{
int b;
printf(“Enter value of b:”); scanf(“%d”, &b); return(b);
}
sqr(int x)
{
return(x*x);
}

Output:

Enter value of a:5 Enter value of b: 3 Square of sum=64

50.   Write a program to call user-defined function through if statement.
      #include<stdio.h>
#include<conio.h> 
void main()
{
int a();
clrscr();
if(a()%2= =0)
printf(“The number is even”); 
else
printf(“The number is odd”);
}
a()
{
int a;
printf(“Enter value of a:”);
scanf(“%d”,&a); return(a);
}

Output:

Enter value of a:5 The number is odd

51.   Write a program to call user-defined function through switch() statement.
      #include<stdio.h>
#include<conio.h> #include<math.h> #include<ctype.h>
void main()
{
int a(); 
int x=5;
clrscr();
switch(a())
{
case’s’:
printf(“Square of %d is %d”, x, pow(x,2));
break;
case’c’:
printf(“Cube of %d is %d”, x, pow(x,3)); break;
case’d’:
printf(“Double of %d is %d”, x, x*2); break;
default:
printf(“Unexpected choice printed as it is : %d”, x);
}
}
a()
{
char c=’c’;
printf(“Enter your choice square(s), cube(c), double(d):”); c=getche();
c=tolower(c); return(c);
}

Output:

Enter your choice square(s), cube(c), double (d):D Double of 5 is 10

52.   Write a program to call function through the for loop. #include<stdio.h>
#include<conio.h> #include<process.h>
 void main()
{
int plus(int), m=1; clrscr();
for(; plus(m); m++)
{
printf(“%3d”, m);
}
}
plus (int k)
{
if(k= =10)
{
exit(1);
return NULL;
}
else return(k);
}

Output:

1 2 3 4 5 6 7 8 9

C Language Overview

C Language Overview T he C programming language is a general-purpose, high-level language that was originally developed by Dennis M. Ri...