c learn fast
TRANSCRIPT
-
8/10/2019 c learn fast
1/92
-
8/10/2019 c learn fast
2/92
void main(){
long int ARR[10], OAR[10], EAR[10];int i,j=0,k=0,n;
printf("Enter the size of array AR\n");scanf("%d",&n);
printf("Enter the elements of the array\n");for(i=0;i
-
8/10/2019 c learn fast
3/92
12345678899900
111The elements of OAR are345899111The elements of EAR are12678900---------------------------------------*/
Sample 3
/* Write a C program to generate Fibonacci sequence ** Fibonacci sequence is 0 1 1 2 3 5 8 13 21 ... */
#include
void main(){
int fib1=0, fib2=1, fib3, limit, count=0;
printf("Enter the limit to generate the fibonacci sequence\n");scanf("%d", &limit);
printf("Fibonacci sequence is ...\n");printf("%d\n",fib1);printf("%d\n",fib2);count = 2; /* fib1 and fib2 are already used */
while( count < limit){fib3 = fib1 + fib2;
count ++;printf("%d\n",fib3);fib1 = fib2;fib2 = fib3;
}
} /* End of main() */
-
8/10/2019 c learn fast
4/92
/*-----------------------------------------------------------Enter the limit to generate the fibonacci sequence7Fibonacci sequence is ...0
112358-----------------------------------------------------------*/
Sample 4
/* Write a C program to insert a particular element in a specified position ** in a given array */
#include #include
void main(){
int x[10];int i, j, n, m, temp, key, pos;
clrscr();
printf("Enter how many elements\n");scanf("%d", &n);
printf("Enter the elements\n");for(i=0; i
-
8/10/2019 c learn fast
5/92
{for(j=i+1; j x[j]){
temp = x[i];x[i] = x[j];x[j] = temp;
}}
}
printf("Sorted list is\n");for(i=0; i
-
8/10/2019 c learn fast
6/92
/*-------------------------------------OutputEnter how many elements5Enter the elements
214678329Input array elements are214678329
Sorted list is214296783Enter the element to be inserted34Final list is21429346783-------------------------------------------------*/
Sample 5
/* Write a C program to accept an integer and reverse it */
#include
void main(){
long num, rev = 0, temp, digit;
-
8/10/2019 c learn fast
7/92
printf("Enter the number\n"); /*For better programming,choose 'long int' */scanf("%ld", &num);
temp = num;
while(num > 0){digit = num % 10;
rev = rev * 10 + digit;num /= 10;
}
printf("Given number = %ld\n", temp);printf("Its reverse is = %ld\n", rev);
}
/* ---------------------------OutputEnter the number123456Given number = 123456Its reverse is = 654321------------------------------*/
Sample 6
/* This program is to illustrate how user authentication ** is made before allowing the user to access the secured ** resources. It asks for the user name and then the ** password. The password that you enter will not be ** displayed, instead that character is replaced by '*' */
#include #include
void main(){
char pasword[10],usrname[10], ch;int i;
clrscr();
printf("Enter User name: ");gets(usrname);
-
8/10/2019 c learn fast
8/92
printf("Enter the password : ");
for(i=0;i
-
8/10/2019 c learn fast
9/92
-
8/10/2019 c learn fast
10/92
void main(){
float side, surfArea, volume;
printf("Enter the length of a side\n");scanf("%f", &side);
surfArea = 6.0 * side * side;
volume = pow (side, 3);
printf("Surface area = %6.2f and Volume = %6.2f\n", surfArea, volume);}
/*------------------------------------------
OutputEnter the llngth of a side4Surface area = 96.00 and Volume = 64.00
RUN2Enter the length of a side12.5Surface area = 937.50 and Volume = 1953.12------------------------------------------*/
Sample 9
/* Write a C program to accept a decimal number and conert it binary ** and count the number of 1's in the binary number */
#include
void main(){
long num, dnum, rem, base = 1, bin = 0, no_of_1s = 0;
printf("Enter a decimal integer\n");scanf("%ld", &num);
dnum = num;
while( num > 0 ){
rem = num % 2;
-
8/10/2019 c learn fast
11/92
if ( rem == 1 ) /*To count no.of 1s*/{
no_of_1s++;}bin = bin + rem * base;
num = num / 2 ;base = base * 10;}printf("Input number is = %d\n", dnum);printf("Its Binary equivalent is = %ld\n", bin);printf("No.of 1's in the binary number is = %d\n", no_of_1s);
} /* End of main() */
/*--------------------------------------------------Output
Enter a decimal integer75Input number is = 75Its Binary equivalent is = 1001011No.of 1's in the binary number is = 4
RUN2Enter a decimal integer128Input number is = 128Its Binary equivalent is = 10000000No.of 1's in the binary number is = 1
-----------------------------------------------------*/
Sample 10
/* Write a c program to find whether a given year ** is leap year or not */
#include
void main(){
int year;
printf("Enter a year\n");scanf("%d",&year);
-
8/10/2019 c learn fast
12/92
if ( (year % 4) == 0)printf("%d is a leap year",year);
elseprintf("%d is not a leap year\n",year);
}
/*------------------------------------------OutputEnter a year20002000 is a leap year
RUN2Enter a year20072007 is not a leap year
------------------------------------------*/Sample 11
/* Write a c program to swap the contents of two numbers ** using bitwise XOR operation. Don't use either the ** temporary variable or arithmetic operators */
#include
void main(){
long i,k;
printf("Enter two integers\n");scanf("%ld %ld",&i,&k);
printf("\nBefore swapping i= %ld and k = %ld",i,k);
i = i^k;k = i^k;i = i^k;
printf("\nAfter swapping i= %ld and k = %ld",i,k);
}
/*------------------------------------------Output
-
8/10/2019 c learn fast
13/92
Enter two integers23 34
Before swapping i= 23 and k = 34After swapping i= 34 and k = 23
------------------------------------------*/Sample 12
/* Write a c program to convert given number of days to a measure of time* given in years, weeks and days. For example 375 days is equal to 1 year* 1 week and 3 days (ignore leap year)*/
#include #define DAYSINWEEK 7
void main(){
int ndays, year, week, days;
printf("Enter the number of days\n");scanf("%d",&ndays);
year = ndays/365;week = (ndays % 365)/DAYSINWEEK;days = (ndays%365) % DAYSINWEEK;
printf ("%d is equivalent to %d years, %d weeks and %d days\n",ndays, year, week, days);
}
/*-----------------------------------------------OutputEnter the number of days375375 is equivalent to 1 years, 1 weeks and 3 days
Enter the number of dayy423423 is equivalent tt 1 years, 8 weeks and 2 days
Enter the number of days14971497 is equivalent to 4 years, 5 weeks and 2 days---------------------------------------------------*/
-
8/10/2019 c learn fast
14/92
Sample 13
/* Program to accepts two strings and compare them. Finally it prints ** whether both are equal, or first string is greater than the second *
* or the first string is less than the second string */#include#include
void main(){
int count1=0,count2=0,flag=0,i;char str1[10],str2[10];
clrscr();
puts("Enter a string:");gets(str1);
puts("Enter another string:");gets(str2);/*Count the number of characters in str1*/while (str1[count1]!='\0')
count1++;/*Count the number of characters in str2*/while (str2[count2]!='\0')
count2++;i=0;
/*The string comparison starts with thh first character in each string andcontinues with subsequent characters until the corresponding charactersdiffer or until the end of the strings is reached.*/
while ( (i < count1) && (i < count2)){
if (str1[i] == str2[i]){
i++;continue;
}if (str1[i] str2[i])
-
8/10/2019 c learn fast
15/92
{flag = 1;break;
}}
if (flag==0)printf("Both strings are equal\n");
if (flag==1)printf("String1 is greater than string2\n", str1, str2);
if (flag == -1)printf("String1 is less than string2\n", str1, str2);
getch();}/*----------------------------------------Output
Enter a string:happyEnter another string:HAPPYString1 is greater than string2
RUN2Enter a string:HelloEnter another string:HelloBoth strings are equal
RUN3Enter a string:goldEnter another string:silverString1 is less than string2----------------------------------------*/Sample 14
/* Program to accept two strings and concatenate them ** i.e.The second string is appended to the end of the first string */
#include #include
-
8/10/2019 c learn fast
16/92
void main(){
char string1[20], string2[20];int i,j,pos;
strset(string1, '\0'); /*set all occurrences in two strings to NULL*/strset(string2,'\0');
printf("Enter the first string :");gets(string1);fflush(stdin);
printf("Enter the second string:");gets(string2);
printf("First string = %s\n", string1);printf("Second string = %s\n", string2);
/*To concate the second stribg to the end of the stringtravserse the first to its end and attach the second string*/
for (i=0; string1[i] != '\0'; i++){
; /*null statement: simply trvsering the string1*/}
pos = i;
for (i=pos,j=0; string2[j]!='\0'; i++){
string1[i] = string2[j++];}
string1[i]='\0'; /*set the last character of string1 to NULL*/
printf("Concatenated string = %s\n", string1);}
/*---------------------------------------OutputEnter the first string :CD-Enter the second string:ROMFirst string = CD-Second string = ROMConcatenated string = CD-ROM
-
8/10/2019 c learn fast
17/92
----------------------------------------*/
Sample 15
/* Write a c program to find the length of a string *
* without using the built-in function */#include
void main(){
char string[50];int i, length = 0;
printf("Enter a string\n");gets(string);
for (i=0; string[i] != '\0'; i++) /*keep going through each */{ /*character of the string */
length++; /*till its end */}printf("The length of a string is the number of characters in it\n");printf("So, the length of %s =%d\n", string, length);
}
/*----------------------------------------------------OutputEnter a stringhelloThe length of a string is the number of characters in itSo, the length of hello = 5
RUN2Enter a stringE-Commerce is hot nowThe length of a string is the number of characters in itSo, the length of E-Commerce is hot now =21----------------------------------------------------------*/Sample 16
/* Write a c program to find the length of a string* without using the built-in function also check* whether it is a palindrome or not*/
-
8/10/2019 c learn fast
18/92
#include #include
void main(){
char string[25], revString[25]={'\0'};int i,length = 0, flag = 0;
clrscr();fflush(stdin);
printf("Enter a string\n");gets(string);
for (i=0; string[i] != '\0'; i++) /*keep going through each */{ /*character of the string */
length++; /*till its end */}printf("The length of the string \'%s\' = %d\n", string, length);
for (i=length-1; i >= 0 ; i--){
revString[length-i-1] = string[i];}/*Compare the input string and its reverse. If both are equalthen the input string is palindrome. Otherwise it isnot a palindrome */
for (i=0; i < length ; i++){
if (revString[i] == string[i])flag = 1;
elseflag = 0;
}
if (flag == 1)printf ("%s is a palindrome\n", string);
elseprintf("%s is not a palindrome\n", string);
}
/*----------------------------------------------------Output
-
8/10/2019 c learn fast
19/92
Enter a stringmadamThe length of the string 'madam' = 5madam is a palindrome
RUN2Enter a stringgoodThe length of the string 'good' = 4good is not a palindrome----------------------------------------------------------*/
Sample 17
/* Write a C program to accept a string and find the number of times the word 'the'* appears in it*/
#include#include
void main(){
int count=0,i,times=0,t,h,e,space;char str[100];
clrscr();puts("Enter a string:");gets(str);
/*Traverse the string to count the number of characters*/while (str[count]!='\0'){
count++;}/*Finding the frequency of the word 'the'*/for(i=0;i
-
8/10/2019 c learn fast
20/92
}/*-----------------------------------------------------OutputEnter a string:The Teacher's day is the birth day of Dr.S.Radhakrishnan
Frequency of the word 'the' is 2---------------------------------------------------------*/
Sample 18
/* Write a C program to compute the value of X ^ N given X and N as inputs */
#include #include
void main(){long int x,n,xpown;long int power(int x, int n);
printf("Enter the values of X and N\n");scanf("%ld %ld",&x,&n);
xpown = power (x,n);
printf("X to the power N = %ld\n");}
/*Recursive function to computer the X to power N*/
long int power(int x, int n){
if (n==1)return(x);
else if ( n%2 == 0)return (pow(power(x,n/2),2)); /*if n is even*/
elsereturn (x*power(x, n-1)); /* if n is odd*/
}/*-------------------------------------OutputEnter the values of X and N2 5X to the power N = 32
-
8/10/2019 c learn fast
21/92
RUN2Enter the values offX and N4 4X to the power N ==256
RUN3Enter the values of X and N5 2X to the power N = 25
RUN4Enter the values of X and N10 5X to the power N = 100000-----------------------------------------*/
Sample 19
/* Write a C program to accept two matrices and ** find the sum and difference of the matrices */
#include #include
int A[10][10], B[10][10], sumat[10][10], diffmat[10][10];int i, j, R1, C1, R2, C2;
void main(){
/* Function declarations */void readmatA();void printmatA();void readmatB();void printmatB();void sum();void diff();
printf("Enter the order of the matrix A\n");scanf("%d %d", &R1, &C1);
printf("Enter the order of the matrix B\n");scanf("%d %d", &R2,&C2);
if( R1 != R2 && C1 != C2){
-
8/10/2019 c learn fast
22/92
-
8/10/2019 c learn fast
23/92
void printmatA(){
for(i=0; i
-
8/10/2019 c learn fast
24/92
-
8/10/2019 c learn fast
25/92
3 69 12
Difference matrix is-1 -2-3 -4
-----------------------------------------------------*/
Sample 20
/* Write a C Program to accept two matrices and check if they are equal */
#include #include #include
void main(){int A[10][10], B[10][10];int i, j, R1, C1, R2, C2, flag =1;
printf("Enter the order of the matrix A\n");scanf("%d %d", &R1, &C1);
printf("Enter the order of the matrix B\n");scanf("%d %d", &R2,&C2);
printf("Enter the elements of matrix A\n");for(i=0; i
-
8/10/2019 c learn fast
26/92
for(i=0; i
-
8/10/2019 c learn fast
27/92
}
/*------------------------------------------------------OutputEnter the order of the matrix A
2 2Enter the order of the matrix B2 2Enter the elements of matrix A1 23 4Enter the elements of matrix B1 23 4MATRIX A is
1 2
3 4MATRIX B is1 23 4
Matrices can be comparedTwo matrices are equal-------------------------------------------------------*/
Sample 21
/* Write a C Program to check if a given matrix is an identity matrix */
#include
void main(){
int A[10][10];int i, j, R, C, flag =1;
printf("Enter the order of the matrix A\n");scanf("%d %d", &R, &C);
printf("Enter the elements of matrix A\n");for(i=0; i
-
8/10/2019 c learn fast
28/92
}printf("MATRIX A is\n");for(i=0; i
-
8/10/2019 c learn fast
29/92
2 2Enter the elements of matrix A1 00 1MATRIX A is
1 00 1It is identity matrix------------------------------------------*/
Sample 22
/* Write a C program to accept a matrix of given order and* interchnge any two rows and columns in the original matrix*/
#include void main(){
static int m1[10][10],m2[10][10];int i,j,m,n,a,b,c, p, q, r;
printf ("Enter the order of the matrix\n");scanf ("%d %d",&m,&n);
printf ("Enter the co-efficents of the matrix\n");for (i=0; i
-
8/10/2019 c learn fast
30/92
printf ("Enter the numbers of two columns to be exchnged\n");scanf ("%d %d",&p,&q);
printf ("The given matrix is \n");for (i=0;i
-
8/10/2019 c learn fast
31/92
Enter the numbers of two columns to be exchnged2 3The given matrix is1 2 45 7 9
3 0 6The matix after interchnging the two rows (in the original matrix)5 7 91 2 43 0 6
The matix after interchnging the two columns(in the original matrix)1 4 25 9 73 6 0
-------------------------------------------------------------------------*/
Sample 23
/* Write a C program to display the inventory of items in a store/shop ** The inventory maintains details such as name, price, quantity and ** manufacturing date of each item. */
#include #include
void main(){
struct date{
int day;int month;int year;
};
struct details{
char name[20];int price;int code;int qty;struct date mfg;
};
struct details item[50];int n,i;
-
8/10/2019 c learn fast
32/92
clrscr();
printf("Enter number of items:");scanf("%d",&n);fflush(stdin);
for(i=0;i
-
8/10/2019 c learn fast
33/92
Quantity:23price:40Manufacturing date(dd-mm-yyyy):12-03-2007Item name:Milk PowderItem code:345
Quantity:20price:80Manufacturing date(dd-mm-yyyy):30-03-2007Item name:Soap PowderItem code:510Quantity:10price:30Manufacturing date(dd-mm-yyyy):01-04-2007Item name:Washing SoapItem code:890Quantity:25
price:12Manufacturing date(dd-mm-yyyy):10-03-2007Item name:ShampoItem code:777Quantity:8price:50Manufacturing date(dd-mm-yyyy):17-05-2007
***** INVENTORY *****------------------------------------------------------------------------S.N.| NAME | CODE | QUANTITY | PRICE |MFG.DATE------------------------------------------------------------------------1 Tea Powder 123 23 40 12/3/20072 Milk Powder 345 20 80 30/3/20073 Soap Powder 510 10 30 1/4/20074 Washing Soap 890 25 12 10/3/20075 Shampo 777 8 50 17/5/2007------------------------------------------------------------------------
--------------------------------------------------------------------*/
Sample 24
/* Write a C program to convert the given binary number into its equivalent decimal */
#include
void main(){
int num, bnum, dec = 0, base = 1, rem ;
-
8/10/2019 c learn fast
34/92
-
8/10/2019 c learn fast
35/92
OutputEnter an integer1515 x 4 = 60
RUN2Enter an integer262262 x 4 = 1048---------------------------------*/Sample 26
/* Write a C program to find the sum of cos(x) series */
#include
#includevoid main(){
int n,x1,i,j;float x,sign,cosx,fact;
printf("Enter the number of the terms in aseries\n");scanf("%d",&n);
printf("Enter the value of x(in degrees)\n");scanf("%f",&x);
x1=x;
x=x*(3.142/180.0); /* Degrees to radians*/
cosx=1;
sign=-1;
for(i=2;i
-
8/10/2019 c learn fast
36/92
cosx=cosx+(pow(x,i)/fact)*sign;sign=sign*(-1);
}
printf("Sum of the cosine series = %7.2f\n",cosx);printf("The value of cos(%d) using library function = %f\n",x1,cos(x));
} /*End of main() */
/*--------------------------------------------------------OutputEnter the number of the terms in aseries5Enter the value of x(in degrees)60
Sum of the cosine series = 0.50The value of cos(60) using library function = 0.499882--------------------------------------------------------*/Sample 27
/* Writ a C programme to cyclically permute the elements of an array A. *i.e. the content of A1 become that of A2.And A2 contains that of A3 *& so on as An contains A1 */
#include
void main (){
int i,n,number[30];printf("Enter the value of the n = ");scanf ("%d", &n);printf ("Enter the numbers\n");for (i=0; i
-
8/10/2019 c learn fast
37/92
printf ("%d\n", number[i]);}/*-------------------------------------OutputEnter the value of the n = 5
Enter the numbers1030204518Cyclically permted numbers are given below30204518
10---------------------------------------------------*/Sample 28
/* Write a C program to accept a set of numbers and compute mean, ** varience and standard deviation */
#include #include
void main(){
float x[10];int i, n;float avrg, var, SD, sum=0, sum1=0;
printf("Enter howmany elements\n");scanf("%d", &n);
for(i=0; i
-
8/10/2019 c learn fast
38/92
/* Compute varaience and standard deviation */for(i=0; i
-
8/10/2019 c learn fast
39/92
for (i=0; i
-
8/10/2019 c learn fast
40/92
int number[30];int i,j,a,n;
printf ("Enter the value of N\n");scanf ("%d", &n);
printf ("Enter the numbers \n");for (i=0; i
-
8/10/2019 c learn fast
41/92
67423510
/*------------------------------------------------*/Sample 31
/* Write a C program to accept a list of data items and find the second *largest and second smallest elements in it. And also computer the average *of both. And search for the average value whether it is present in the *array or not. Display appropriate message on successful search. */
#include
void main (){int number[30];int i,j,a,n,counter,ave;
printf ("Enter the value of N\n");scanf ("%d", &n);
printf ("Enter the numbers \n");for (i=0; i
-
8/10/2019 c learn fast
42/92
printf ("The 2nd largest number is = %d\n", number[1]);printf ("The 2nd smallest number is = %d\n", number[n-2]);
ave = (number[1] +number[n-2])/2;
counter = 0;for (i=0; i
-
8/10/2019 c learn fast
43/92
Sample 32
/* Write a C program to accept an array of integers and delete the ** specified integer from the list */
#include
void main(){
int vectx[10];int i, n, pos, element, found = 0;
printf("Enter how many elements\n");scanf("%d", &n);
printf("Enter the elements\n");for(i=0; i
-
8/10/2019 c learn fast
44/92
}
printf("The resultant vector is \n");for(i=0; i
-
8/10/2019 c learn fast
45/92
5581Enter the element to be deleted55The resultant vector is
231081
--------------------------------------------------------*/Sample 33
/* Write a C programme (1-D Array) store some elements in it.Accept key& split from that point. Add the first half to the end of second half*/
#include void main (){
int number[30];int i,n,a,j;
printf ("Enter the value of n\n");scanf ("%d",&n);
printf ("enter the numbers\n");for (i=0; i
-
8/10/2019 c learn fast
46/92
printf ("%d\n",number[i]);}
} /* End of main() */
/*-------------------------------------------------
OutputEnter the value of n5enter the numbers3010405060Enter the position of the element to split the array2
The resultant array is4050603010----------------------------------------------------------*/Sample 34
/* Write a C program to find the frequency of odd numbers ** and even numbers in the input of a matrix */
#include
void main (){static int ma[10][10];int i,j,m,n,even=0,odd=0;
printf ("Enter the order ofthe matrix \n");scanf ("%d %d",&m,&n);
printf ("Enter the coefficients if matrix \n");for (i=0;i
-
8/10/2019 c learn fast
47/92
{++even;
}else
++odd;
}}printf ("The given matrix is\n");for (i=0;i
-
8/10/2019 c learn fast
48/92
#include
void main (){
static int ma[10][10];int i,j,m,n,a;
printf ("Enetr the order of the matix \n");scanf ("%d %d",&m,&n);
if (m ==n ){
printf ("Enter the co-efficients of the matrix\n");for (i=0;i
-
8/10/2019 c learn fast
49/92
}printf ("\n");
}}else
printf ("The givan order is not square matrix\n");} /* end of main() */
/*----------------------------------------------------OutputEnetr the order of the matix3 3Enter the co-efficients of the matrix1 2 34 5 6
7 8 9The given matrix is1 2 34 5 67 8 9
THe matrix after changing themain diagonal & secondary diagonal3 2 14 5 69 8 7
----------------------------------------------------------*/
Sample 36
/* Write a C program to find the sum of first 50 natural numbers ** using for loop */
#include
void main(){
int num,sum=0;
for(num = 1; num
-
8/10/2019 c learn fast
50/92
printf("Sum = %4d\n", sum);
}
/*---------------------------
OutputSum = 1275----------------------------*/ /* End of main() */
/* Write a C program to accept two integers and check if they are equal */
#include void main(){
int m,n;
printf("Enter the values for M and N\n");scanf("%d %d", &m,&n);
if(m == n )printf("M and N are equal\n");
elseprintf("M and N are not equal\n");
} /* End of main() */
/*------------------------------------outputEnter the values for M and N34 45M and N are not equal
-------------------------------------*/Sample 37
//* Write a C program to accept the height of a person in centermeter and ** categorize the person based on height as taller, dwarf and ** average height person */
#include void main(){
float ht;
-
8/10/2019 c learn fast
51/92
printf("Enter the Height (in centimetres)\n");scanf("%f",&ht);
if(ht < 150.0)printf("Dwarf\n");
else if((ht>=150.0) && (ht=165.0) && (ht 0 && y > 0)printf("point (%d,%d) lies in the First quandrant\n");
else if( x < 0 && y > 0)printf("point (%d,%d) lies in the Second quandrant\n");
else if( x < 0 && y < 0)printf("point (%d, %d) lies in the Third quandrant\n");
else if( x > 0 && y < 0)printf("point (%d,%d) lies in the Fourth quandrant\n");
else if( x == 0 && y == 0)printf("point (%d,%d) lies at the origin\n");
-
8/10/2019 c learn fast
52/92
} /* End of main() */
/*---------------------------------------------Output
RUN 1Enter the values for X and Y3 5point (5,3) lies in the First quandrant
RUN 2Enter the values for X and Y-4-7point (-7, -4) lies in the Third quandrant
---------------------------------------------*/
Sample 39
/* Write a C program to accept a figure code and find the ares of different ** geometrical figures such as circle, square, rectangle etc using switch */
#include
void main(){
int fig_code;float side,base,length,bredth,height,area,radius;
printf("-------------------------\n");printf(" 1 --> Circle\n");printf(" 2 --> Rectangle\n");printf(" 3 --> Triangle\n");printf(" 4 --> Square\n");printf("-------------------------\n");
printf("Enter the Figure code\n");scanf("%d",&fig_code);
switch(fig_code){
case 1: printf("Enter the radius\n");scanf("%f",&radius);area=3.142*radius*radius;
-
8/10/2019 c learn fast
53/92
printf("Area of a circle=%f\n", area);break;
case 2: printf("Enter the bredth and length\n");scanf("%f %f",&bredth, &length);
area=bredth *length;printf("Area of a Reactangle=%f\n", area);break;
case 3: printf("Enter the base and height\n");scanf("%f %f",&base,&height);area=0.5 *base*height;printf("Area of a Triangle=%f\n", area);break;
case 4: printf("Enter the side\n");
scanf("%f",&side);area=side * side;printf("Area of a Square=%f\n", area);break;
default: printf("Error in figure code\n");break;
} /* End of switch */
} /* End of main() *//*----------------------------------------------------OutputRun 1-------------------------1 --> Circle2 --> Rectangle3 --> Triangle4 --> Square
-------------------------Enter the Figure code2Enter the bredth and length26Area of a Reactangle=12.000000
Run 2-------------------------1 --> Circle
-
8/10/2019 c learn fast
54/92
2 --> Rectangle3 --> Triangle4 --> Square
-------------------------Enter the Figure code
3Enter the base and height57Area of a Triangle=17.500000
------------------------------------------------------*/Sample 40
/* Write a C Program to accept a grade and declare the equivalent descrption *
* if code is S, then print SUPER ** if code is A, then print VERY GOOD
** if code is B, then print FAIR
** if code is Y, then print ABSENT
** if code is F, then print FAILS
*/
#include #include #include
void main(){
char remark[15];char grade;
printf("Enter the grade\n");scanf("%c",&grade);
grade=toupper(grade); /* lower case letter to upper case */
switch(grade){
case 'S': strcpy(remark," SUPER");break;
-
8/10/2019 c learn fast
55/92
case 'A': strcpy(remark," VERY GOOD");break;
case 'B': strcpy(remark," FAIR");break;
case 'Y': strcpy(remark," ABSENT");break;
case 'F': strcpy(remark," FAILS");break;
default : strcpy(remark, "ERROR IN GRADE\n");break;
} /* End of switch */
printf("RESULT : %s\n",remark);
} /* End of main() */
/*-------------------------------------------OutputRUN 1Enter the gradesRESULT : SUPER
RUN 2Enter the gradeyRESULT : ABSENT-----------------------------------------------*/Sample 41
/* Write a C program to find the factorial of a given number */
#include void main(){
int i,fact=1,num;
printf("Enter the number\n");scanf("%d",&num);
-
8/10/2019 c learn fast
56/92
if( num
-
8/10/2019 c learn fast
57/92
printf("Sum of all digits=%d\n",sum);
} /* End of main() */
/*-----------------------------------------------------
OutputEnter the string containing both digits and alphabetgoodmorning25NO. of Digits in the string=2Sum of all digits=7
--------------------------------------------------------*/
Sample 43
/* Write a C program to accept an integer find the sum of the digits in it */
#include
void main(){
long num, temp, digit, sum = 0;
printf("Enter the number\n");scanf("%ld", &num);
temp = num;
while(num > 0){
digit = num % 10;sum = sum + digit;
num /= 10;}
printf("Given number =%ld\n", temp);printf("Sum of the digits %ld =%ld\n", temp, sum);
} /* End of main()*/
/*---------------------------------------Enter the number123456Given number =123456Sum of the digits 123456 =21----------------------------------------*/
-
8/10/2019 c learn fast
58/92
Sample 44
/* Write a C program to accept a matrix of order M x N and find the sum ** of each row and each column of a matrix */
#include void main (){
static int m1[10][10];int i,j,m,n,sum=0;
printf ("Enter the order of the matrix\n");scanf ("%d %d", &m,&n);printf ("Enter the co-efficients of the matrix\n");
for (i=0;i
-
8/10/2019 c learn fast
59/92
OutputEnter the order of the matrix3 3Enter the co-efficients of the matrix1 2 3
4 5 67 8 9Sum of the 0 row is = 6Sum of the 1 row is = 15Sum of the 2 row is = 24Sum of the 0 column is = 12Sum of the 1 column is = 15Sum of the 2 column is = 18
----------------------------------------------------*/
Sample 45
/* Write a C program to find accept a matric of order M x N and find ** the sum of the main diagonal and off diagonal elements */
#include
void main (){
static int ma[10][10];int i,j,m,n,a=0,sum=0;
printf ("Enetr the order of the matix \n");scanf ("%d %d",&m,&n);
if ( m == n ){
printf ("Enter the co-efficients of the matrix\n");
for (i=0;i
-
8/10/2019 c learn fast
60/92
for (j=0;j
-
8/10/2019 c learn fast
61/92
Sample 46
/* Write a C program to accept a matricx of order MxN and find the trcae and ** normal of a matrix HINT:Trace is defined as the sum of main diagonal ** elements and Normal is defined as squre root of the sum of all *
* the elements */#include #include
void main (){
static int ma[10][10];int i,j,m,n,sum=0,sum1=0,a=0,normal;
printf ("Enter the order of the matrix\n");
scanf ("%d %d", &m,&n);printf ("Enter the ncoefficients of the matrix \n");for (i=0;i
-
8/10/2019 c learn fast
62/92
Trace of the matrix is = 15
Run 2Enter the order of the matrix2 2
Enter the ncoefficients of the matrix2 46 8The normal of the given matrix is = 10Trace of the matrix is = 10
-----------------------------------------------------*/Sample 47
/* Write a C program to accept a matric of order MxN and find its transpose */
#include void main (){
static int ma[10][10];int i,j,m,n;
printf ("Enter the order of the matrix \n");scanf ("%d %d",&m,&n);
printf ("Enter the coefiicients of the matrix\n");for (i=0;i
-
8/10/2019 c learn fast
63/92
{for (i=0;i
-
8/10/2019 c learn fast
64/92
{scanf ("%d",&m1[i][j]);if (m1[i][j]==0){
++counter;
}}}if (counter>((m*n)/2)){
printf ("The given matrix is sparse matrix \n");}else
printf ("The given matrix is not a sparse matrix \n");
printf ("There are %d number of zeros",counter);
} /* EN dof main() *//*----------------------------------------------OutputEnter the order of the matix2 2Enter the co-efficients of the matix1 23 4The given matrix is not a sparse matrixThere are 0 number of zeros
Run 2Enter the order of the matix3 3Enter the co-efficients of the matix1 0 00 0 10 1 0The given matrix is sparse matrixThere are 6 number of zeros
-----------------------------------------------*/
Sample 49
/* Write a C program to accept a amtric of order MxN and sort all rows ** of the matrix in ascending order and all columns in descendng order */
#include
-
8/10/2019 c learn fast
65/92
void main (){
static int ma[10][10],mb[10][10];int i,j,k,a,m,n;
printf ("Enter the order of the matrix \n");scanf ("%d %d", &m,&n);
printf ("Enter co-efficients of the matrix \n");for (i=0;i
-
8/10/2019 c learn fast
66/92
-
8/10/2019 c learn fast
67/92
1 32 5
After arranging the columns in descending order5 23 1
-----------------------------------------------------*/Sample 50
/* Write a C program to accept a set of names and sort them in ** an alphabetical order, Use structures to store the names */
#include#include#include
struct person{char name[10];int rno;
};typedef struct person NAME;NAME stud[10], temp[10];
void main(){
int no,i;
void sort(int N); /* Function declaration */
clrscr();fflush(stdin);
printf("Enter the number of students in the list\n");scanf("%d",&no);
for(i = 0; i < no; i++){
printf("\nEnter the name of person %d : ", i+1);fflush(stdin);gets(stud[i].name);
printf("Enter the roll number of %d : ", i+1);scanf("%d",&stud[i].rno);temp[i] = stud[i];
}
-
8/10/2019 c learn fast
68/92
printf("\n*****************************\n");printf (" Names before sorting \n");/* Print the list of names before sorting */for(i=0;i
-
8/10/2019 c learn fast
69/92
Enter the name of person 1 : RajashreeEnter the roll number of 1 : 123
Enter the name of person 2 : John
Enter the roll number of 2 : 222Enter the name of person 3 : PriyaEnter the roll number of 3 : 331
Enter the name of person 4 : AnandEnter the roll number of 4 : 411
Enter the name of person 5 : NirmalaEnter the roll number of 5 : 311
*****************************Names before sortingRajashree 123John 222Priya 331Anand 411Nirmala 311
*****************************Names after sorting
*****************************Anand 411John 222Nirmala 311Priya 331Rajashree 123
*****************************----------------------------------------------------*/Sample 50
/* Write a C Program to convert the lower case letters to upper case ** and vice-versa */
#include #include #include
-
8/10/2019 c learn fast
70/92
void main(){
char sentence[100];int count, ch, i;clrscr();
printf("Enter a sentence\n");for(i=0; (sentence[i] = getchar())!='\n'; i++){;
}count = i;
printf("\nInput sentence is : %s ",sentence);
printf("\nResultant sentence is : ");
for(i=0; i < count; i++){ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]);
putchar(ch);}
} /*End of main()
/*-----------------------------------------OutputEnter a sentencegOOD mORNING
Input sentence is : gOOD mORNING
Resultant sentence is :Good Morning------------------------------------------*/
Sample 51
/* Write a C program to find the sum of two one-dimensional arrays using ** Dynamic Memory Allocation */
#include #include #include
void main(){
int i,n;
-
8/10/2019 c learn fast
71/92
-
8/10/2019 c learn fast
72/92
89Resultant List is79
1113
----------------------------------------*/Sample 52
/* Write a C program to find the sum of all elements of ** an array using pointersas arguments */
#include
void main(){
static int array[5]={ 200,400,600,800,1000 };int sum;
int addnum(int *ptr); /* function prototype */
sum = addnum(array);
printf("Sum of all array elements = %5d\n", sum);
} /* End of main() */
int addnum(int *ptr){
int index, total=0;
for(index = 0; index < 5; index++){
total += *(ptr+index);}return(total);
}/*-----------------------------------OutputSum of all array elements = 3000------------------------------------*/
-
8/10/2019 c learn fast
73/92
Sample 53
/* Write a C program to accept an array of 10 elements and swap 3rd ** element with 4th element using pointers. And display the results */
#include
void main(){
float x[10];int i,n;
void swap34(float *ptr1, float *ptr2 ); /* Function Declaration */
printf("How many Elements...\n");scanf("%d", &n);
printf("Enter Elements one by one\n");
for(i=0;i
-
8/10/2019 c learn fast
74/92
/*-------------------------------------------OutputHow many Elements...10
Enter Elements one by one102030405060708090100
Resultant Array...X[0] = 10.000000X[1] = 20.000000X[2] = 40.000000X[3] = 30.000000X[4] = 50.000000X[5] = 60.000000X[6] = 70.000000X[7] = 80.000000X[8] = 90.000000X[9] = 100.000000----------------------------------------------------*/
Sample 54
/* Write a C program to illustrate the concept of unions*/
#include #include
void main(){
union number{
int n1;float n2;
};
union number x;
-
8/10/2019 c learn fast
75/92
clrscr() ;
printf("Enter the value of n1: ");scanf("%d", &x.n1);
printf("Value of n1 =%d", x.n1);printf("\nEnter the value of n2: ");scanf("%d", &x.n2);printf("Value of n2 = %d\n",x.n2);
} /* End of main() */
/*------------------------------------OutputEnter the value of n1: 2
Value of n1 =2Enter the value of n2: 3Value of n2 = 0------------------------------------*/Sample 55
/* Write a C program to find the size of a union*/
#include
void main(){
union sample{
int m;float n;
char ch;};
union sample u;
printf("The size of union =%d\n", sizeof(u));
/*initialization */
u.m = 25;printf("%d %f %c\n", u.m, u.n,u.ch);
u.n = 0.2;
-
8/10/2019 c learn fast
76/92
printf("%d %f %c\n", u.m, u.n,u.ch);
u.ch = 'p';printf("%d %f %c\n", u.m, u.n,u.ch);
} /*End of main() */
/*-----------------------------------------OutputThe size of union =425 12163373596672.000000 -13107 0.200000 -13200 0.199999 p------------------------------------------*/Sample 56
/* Write a C program to create a file called emp.rec and store information *
* about a person, in terms of his name, age and salary. */#include
void main(){
FILE *fptr;char name[20];
int age;float salary;
fptr = fopen ("emp.rec", "w"); /*open for writing*/
if (fptr == NULL){
printf("File does not exists\n");return;
}
printf("Enter the name\n");scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
printf("Enter the age\n");scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
printf("Enter the salary\n");scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);
-
8/10/2019 c learn fast
77/92
fclose(fptr);
}
/*---------------------------------------------------------------------------
OutputEnter the namePrabhuEnter the age25Enter the salary25000-------------------------------------Please note that you have to open the file called emp.rec in the directory
Name = Prabhu
Age = 25Salary = 25000.00-----------------------------------------------------------------------------*/
Sample 57
/*Write a C program to illustrate as to how the data stored on the disk is read */
#include #include
void main(){
FILE *fptr;char filename[15];char ch;
printf("Enter the filename to be opened\n");gets(filename);
fptr = fopen (filename, "r"); /*open for reading*/
if (fptr == NULL){
printf("Cannot open file\n");exit(0);
}
ch = fgetc(fptr);
-
8/10/2019 c learn fast
78/92
while (ch != EOF){
printf ("%c", ch);ch = fgetc(fptr);
}
fclose(fptr);} /* End of main () *//*----------------------------------------------OutputEnter the filename to be openedemp.recName = PrabhuAge = 25Salary = 25000.00------------------------------------------------*/
Sample 58
/* This program accepts an array of N elements and a key. ** Then it searches for the desired element. If the search ** is successful, it displays "SUCCESSFUL SEARCH". ** Otherwise, a message "UNSUCCESSFUL SEARCH" is displayed. */
#include void main(){
int table[20];int i, low, mid, high, key, size;
printf("Enter the size of an array\n");scanf("%d", &size);
printf("Enter the array elements\n");for(i = 0; i < size; i++){
scanf("%d", &table[i]);}
printf("Enter the key\n");scanf("%d", &key);
/* search begins */
low = 0;high = (size - 1);
-
8/10/2019 c learn fast
79/92
while(low
-
8/10/2019 c learn fast
80/92
int num;struct node *ptr;
};
typedef struct node NODE;
NODE *head, *first, *temp=0;int count = 0;int choice = 1;first = 0;
while(choice){
head =(NODE*) malloc(sizeof(NODE));printf("Enter the data item\n");scanf("%d", &head-> num);
if(first != 0){
temp->ptr = head;temp = head;
}else{
first = temp = head;}fflush(stdin);printf("Do you want to continue(Type 0 or 1)?\n");scanf("%d", &choice);
} /* End of while */
temp->ptr = 0;temp = first; /* reset temp to the beginning*/printf("\nstatus of the linked list is\n");
while(temp!=0){
printf("%d=>", temp->num);count++;temp = temp -> ptr;
}printf("NULL\n");printf("No. of nodes in the list = %d\n", count);
} /* End of main*/
-
8/10/2019 c learn fast
81/92
/*-----------------------------------------------OutputEnter the data item10Do you want to continue(Type 0 or 1)?
1Enter the data item34Do you want to continue(Type 0 or 1)?1Enter the data item56Do you want to continue(Type 0 or 1)?0
status of the linked list is
10=>34=>56=>NULLNo. of nodes in the list = 3----------------------------------------------*/Sample 60
/* Write a C program to illustrate the operations of singly linked list */
#include #include #include #include #define MAX 30
struct EMP{
int empno;char empName[MAX];char designation[MAX];struct EMP *next;
};
/*********************************************************************//* Function to insert a node at the front of the linked list. *//* front: front pointer, id: employee ID, name: employee name *//* desg: Employee designation *//* Returns the new front pointer. *//*********************************************************************/
-
8/10/2019 c learn fast
82/92
struct EMP* insert(struct EMP *front, int id, char name[], char desg[]){
struct EMP *newnode;
newnode = (struct EMP*) malloc(sizeof(struct EMP));
if (newnode == NULL){
printf("\nAllocation failed\n");exit(2);
}newnode->empno = id;strcpy(newnode->empName, name);strcpy(newnode->designation, desg);newnode->next = front;front = newnode;
return(front);} /*End of insert() */
/* Function to display a node in a linked list */void printNode(struct EMP *p){
printf("\nEmployee Details...\n");printf("\nEmp No : %d", p->empno);printf("\nName : %s", p->empName);printf("\nDesignation : %s\n", p->designation);printf("-------------------------------------\n");
} /*End of printNode() */
/* ********************************************************//* Function to deleteNode a node based on employee number *//* front: front pointer, id: Key value *//* Returns: the modified list. *//* ********************************************************/
struct EMP* deleteNode(struct EMP *front, int id){
struct EMP *ptr;struct EMP *bptr; /* bptr is pointing to the node behind ptr */
if (front->empno == id){
ptr = front;printf("\nNode deleted:");
printNode(front);
-
8/10/2019 c learn fast
83/92
front = front->next;free(ptr);
return(front);}
for(ptr=front->next, bptr=front; ptr!=NULL; ptr=ptr->next, bptr=bptr->next){if (ptr->empno == id){
printf("\nNode deleted:");printNode(ptr);bptr->next = ptr->next;free(ptr);return(front);
}}
printf("\nEmployee Number %d not found ", id);return(front);} /*End of deleteNode() */
/*****************************************************************//* Function to search the nodes in a linear fashion based emp ID *//* front: front pointer, key: key ID. *//*****************************************************************/void search(struct EMP *front, int key){
struct EMP *ptr;
for (ptr = front; ptr != NULL; ptr = ptr -> next){
if (ptr->empno == key){
printf("\nKey found:");printNode(ptr);return;
}}printf("\nEmployee Number %d not found ", key);
} /*End of search() */
/* Function to display the linked list */void display(struct EMP *front){
struct EMP *ptr;
for (ptr = front; ptr != NULL; ptr = ptr->next)
-
8/10/2019 c learn fast
84/92
{printNode(ptr);
}} /*End of display() */
/* Function to display the menu of operations on a linked list */void menu(){
printf("---------------------------------------------\n");printf("Press 1 to INSERT a node into the list \n");printf("Press 2 to DELETE a node from the list \n");printf("Press 3 to DISPLAY the list \n");printf("Press 4 to SEARCH the list \n");printf("Press 5 to EXIT \n");printf("---------------------------------------------\n");
} /*End of menu() */
/* Function to select the option */char option(){
char choice;
printf("\n\n>> Enter your choice: ");switch(choice=getche()){
case '1':case '2':
case '3':case '4':
case '5': return(choice);default : printf("\nInvalid choice.");
}return choice;
} /*End of option() */
/* The main() program begins */void main(){
struct EMP *linkList;char name[21], desig[51];char choice;int eno;
linkList = NULL;
printf("\nWelcome to demonstration of singly linked list\n");
-
8/10/2019 c learn fast
85/92
menu(); /*Function call */
do {choice = option(); /*to choose oeration to be performed */
switch(choice){
case '1': printf("\nEnter the Employee Number : ");scanf("%d", &eno);
printf("Enter the Employee name: ");
fflush(stdin);gets(name);
printf("Enter the EmployeeDesignation : ");gets(desig);
linkList = insert(linkList, eno, name,desig);
break;
case '2': printf("\n\nEnter the employee number to bedeleted: ");
scanf("%d", &eno);
linkList = deleteNode(linkList, eno);break;
case '3': if (linkList == NULL){
printf("\nList empty.");break;
}display(linkList);break;
case '4': printf("\n\nEnter the employee number to besearched: ");
scanf("%d", &eno);
search(linkList, eno);break;
-
8/10/2019 c learn fast
86/92
case '5': break;}
} while (choice != '5');} /*End fo main()*/
/*------------------------------------------------------------------------------Output
Welcome to demonstration of singly linked list---------------------------------------------Press 1 to INSERT a node into the listPress 2 to DELETE a node from the listPress 3 to DISPLAY the listPress 4 to SEARCH the listPress 5 to EXIT---------------------------------------------
>> Enter your choice: 1Enter the Employee Number : 1234Enter the Employee name : KeerthiEnter the Employee Designation : Engineer
>> Enter your choice: 1Enter the Employee Number : 2345Enter the Employee name : SrinivasanEnter the Employee Designation : Specilist
>> Enter your choice: 1Enter the Employee Number : 4567Enter the Employee name : AnnapoornaEnter the Employee Designation : Project Manager
>> Enter your choice: 3Employee Details...
Emp No : 4567Name : AnnapoornaDesignation : Project Manager-------------------------------------
Employee Details...
-
8/10/2019 c learn fast
87/92
Emp No : 2345Name : SrinivasanDesignation : Specilist-------------------------------------
Employee Details...Emp No : 1234Name : KeerthiDesignation : Engineer-------------------------------------
>> Enter your choice: 2
Enter the employee number to be deleted: 2345
Node deleted:Employee Details...
Emp No : 2345Name : SrinivasanDesignation : Specilist-------------------------------------
>> Enter your choice: 3Employee Details...
Emp No : 4567Name : AnnapoornaDesignation : Project Manager-------------------------------------
Employee Details...
Emp No : 1234Name : KeerthiDesignation : Engineer-------------------------------------
>> Enter your choice: 4
Enter the employee number to be searched: 2345
-
8/10/2019 c learn fast
88/92
Employee Number 2345 not found
>> Enter your choice: 5
------------------------------------------------------------------------------*/
Sample 61
/* Write a C program to implement stack. Stack is a LIFO data strcuture ** LIFO - Last in First Out ** Perform PUSH(insert operation), POP(Delete operation) and Display stack */
#include #include #define MAXSIZE 5
struct stack /* Structure definition for stack */{
int stk[MAXSIZE];int top;
};
typedef struct stack STACK;STACK s;
/* Function declaration/Prototype*/
void push (void);int pop(void);void display (void);
void main (){
int choice;int option = 1;
clrscr ();
s.top = -1;
printf ("STACK OPERATION\n");while (option){
printf ("------------------------------------------\n");printf (" 1 --> PUSH \n");
-
8/10/2019 c learn fast
89/92
printf (" 2 --> POP \n");printf (" 3 --> DISPLAY \n");printf (" 4 --> EXIT \n");printf ("------------------------------------------\n");
printf ("Enter your choice\n");scanf ("%d", &choice);
switch (choice){
case 1: push();break;
case 2: pop();break;
case 3: display();break;
case 4: return;}
fflush (stdin);printf ("Do you want to continue(Type 0 or 1)?\n");scanf ("%d", &option);
}}
/*Function to add an element to the stack*/void push (){
int num;if (s.top == (MAXSIZE - 1)){
printf ("Stack is Full\n");return;
}else{
printf ("Enter the element to be pushed\n");scanf ("%d", &num);s.top = s.top + 1;s.stk[s.top] = num;
}return;
}
/*Function to delete an element from the stack*/
-
8/10/2019 c learn fast
90/92
int pop (){
int num;if (s.top == - 1){
printf ("Stack is Empty\n");return (s.top);}else{
num = s.stk[s.top];printf ("poped element is = %d\n", s.stk[s.top]);s.top = s.top - 1;
}return(num);
}
/*Function to display the status of the stack*/void display (){
int i;if (s.top == -1){
printf ("Stack is empty\n");return;
}else{
printf ("\nThe status of the stack is\n");for (i = s.top; i >= 0; i--){
printf ("%d\n", s.stk[i]);}
}printf ("\n");
}
/*---------------------------------------------------------------------------OutputSTACK OPERATION------------------------------------------
1 --> PUSH2 --> POP
3 --> DISPLAY4 --> EXIT
------------------------------------------
-
8/10/2019 c learn fast
91/92
Enter your choice1Enter the element to be pushed23Do you want to continue(Type 0 or 1)?
1------------------------------------------1 --> PUSH
2 --> POP3 --> DISPLAY
4 --> EXIT------------------------------------------Enter your choice1Enter the element to be pushed45
Do you want to continue(Type 0 or 1)?1------------------------------------------
1 --> PUSH2 --> POP
3 --> DISPLAY4 --> EXIT
------------------------------------------Enter your choice1Enter the element to be pushed78Do you want to continue(Type 0 or 1)?1------------------------------------------
1 --> PUSH2 --> POP
3 --> DISPLAY4 --> EXIT
------------------------------------------Enter your choice3
The status of the stack is784523
Do you want to continue(Type 0 or 1)?1
-
8/10/2019 c learn fast
92/92