c programs (1)
DESCRIPTION
nice to reviewTRANSCRIPT
1
S.NO CONTENTS 1. AREA OF TRIANGLE 2. AREA OF CIRCLE 3. SIMPLE INTEREST 4. ODD OR EVEN 5. POSITIVE OR NEGATIVE 6. BIGGEST NUMBER 7. ROOTS OF QUADRATIC EQN 8. CALCULATOR(+,-,*,/) 9. SUM OF N NATURAL NOS 10. FIBONACCI NUMBERS 11. GCD AND LCM 12. SUM OF N ODD AND EVEN NOS 13. PALINDROME NUMBER 14. SIN(X) 15. COS(X) 16. CHECK IF PRIME NUMBER 17. PRINT PRIME NUMBERS 18. RANGE DIVISIBLE BY 5 19. ARRAY,SUM OF POSITIVE,NEGATIVE NOS AND AVERAGE 20. LINEAR SEARCH 21. ASCENDING ORDER USING BUBBLE SORT 22. SORTING AND BINARY SEARCH 23. MEAN,VARIANCE AND STANDARD DEV 24. AVG OF 2 LARGEST NOS WITHOUT SORTING 25. POLYNOMIAL 26. MATRICES-SUM,DIFFERENCE AND TRACE 27. TRANSPOSE OF MATRIX 28. PALINDROME STRING 29. CONCATENATE STRINGS 30. REVERSE LOWER & UPPER CASE LETTERS 31. NO. OF VOWELS AND CONSONANTS32. SORTING NAMES IN ALPHABETICAL ORDER33. SELECTION SORT34. FUNCTIONS AND PRODUCT OF MATRIX35. SUM OF ALL,ROW & COLUMN ELEMENTS 36. SWAPPING USING FUNCTIONS37. BINARY TO DECIMAL CONVERSION38. ARRAY-COPYING ODD AND EVEN NOS39. FIBONACCI SEQUENCE40. INSERTING AN ELEMENT INTO AN ARRAY41. REVERSE AN INTEGER42. PASSWORD IS HIDDEN BY *43. SUBSTRING IN STRING OR NOT44. SURFACE AREA AND VOLUME OF CUBE45. DECIMAL TO BINARY CONVERSION46. LEAP YEAR OR NOT47. SWAP USING BITWISE XOR OPERATION48. CONVERT A DATE IN YR WEEK & DAYS TO DAYS49. COMPARE TWO STRINGS50. CONCATENATE STRINGS51. LENGTH OF STRING WITHOUT USING STRING FUNCTION52. NO. OF THE PRESENT IN A TEXT53. COMPUTE X ^ N 54. SUM AND DIFFERENCE OF TWO MATRICES55. CHECK IF 2 MATRICES ARE EQUAL56. CHECK IF IDENTITY MATRIX57. INTERCHANGE ANY 2 ROWS AND COLUMNS58. BITWISE SHIFT OPERATION59. CYCLIC PERMUTATION OF ARRAYS60. DESCENDING ORDER
61. OPERATIONS ON ARRAY ELEMENTS62. DELETE A GIVEN ELEMENT FROM ARRAY 63. SPLIT AND ADD ARRAY64. FREQUENCY OF ODD AND EVEN NOS IN THE INPUT OF A MATRIX65. INTERCHANGE DIAGONAL ELEMENTS66. SUM OF 50 NATURAL NOS USING FOR LOOP67. CHECK IF 2 INTEGERS ARE EQUAL68. CATEGORIZE ACC TO HEIGHT69. STORE POINT IN COORDINATE SYSTEM AND DETERMINE ITS QUADRANT70. AREAS OF DIFFERENT GEOMETRIC SHAPES71. ACCEPT GRADE AND CATEGORIZE72. FACTORIAL73. SUM OF ALL DIGITS PRESENT IN A STRING74. SUM OF DIGITS IN AN INTEGER75. SUM OF ROW AND COLUMN ELEMENTS76. SUM OF DIAGONAL ELEMENTS77. TRACE AND NORMAL OF MATRIX78. SPARSE MATRIX79. SORT ROWS IN ASCENDING AND COLUMNS IN DESCENDING ORDER
1./* Write a C program to find the area of a triangle, given three sides*/
#include
#include
#include
int main()
{
int s, a, b, c, area;
clrscr();
printf("Enter the values of a,b and c\n");
scanf ("%d %d %d", &a, &b, &c);
/* compute s*/
s = (a + b + c) / 2;
area = sqrt ( s * (s-a) * (s-b) * (s-c));
printf ("Area of a triangale = %d\n", area);getch();
return 0;
}
/*-----------------------------
Output
Enter the values of a,b and c
3
4
5
Area of a triangale = 6
------------------------------*/
2./* Write a C program to find the area of a circl, given the Radius*/
#include
#include
#include
#define PI 3.142
void main()
{
float radius, area;
clrscr();
printf("Enter the radius of a circle\n");
scanf ("%f", &radius);
area = PI * pow (radius,2);
printf ("Area of a circle = %5.2f\n", area);
}
/*-----------------------------
Output
RUN1
Enter the radius of a circle
3.2
Area of a circle = 32.17
RUN 2
Enter the radius of a circle
6
Area of a circle = 113.11
------------------------------*/
3. /* Write a C program to find the simple interest , given principle, *
* rate of interest and times*/
#include
#include
void main()
{
float p, r, si;
int t;
clrscr();
printf("Enter the values of p,r and t\n");
scanf ("%f %f %d", &p, &r, &t);
si = (p * r * t)/ 100.0;
printf ("Amount = Rs. %5.2f\n", p);
printf ("Rate = Rs. %5.2f%\n", r);
printf ("Time = %d years\n", t);
printf ("Simple interest = %5.2f\n", si);
}
/*-----------------------------
Output
Enter the values of p,r and t
2000
8
3
Amount = Rs. 2000.00
Rate = Rs. 8.00%
Time = 3 years
Simple interest = 480.00
------------------------------*/
4. /* Write a C program to check whether a given integer is odd or even*/
#include
#include
void main()
{
int ival, remainder;
clrscr();
printf("Enter an integer :");
scanf ("%d", &ival);
remainder = ival % 2;
if (remainder == 0)
printf ("%d, is an even integer\n", ival);
else
printf ("%d, is an odd integer\n", ival);
}
/*-----------------------------
Output
RUN1
Enter an integer :13
13, is an odd integer
RUN2
Enter an integer :24
24, is an even integer
---------------------------------*/
5. /* Write a C program to check whether a given integer *
* number is positive or negative*/
#include
#include
void main()
{
int number;
clrscr();
printf("Enter a number\n");
scanf ("%d", &number);
if (number > 0)
printf ("%d, is a positive number\n", number);
else
printf ("%d, is a negative number\n", number);
}
/*-----------------------------
Output
Enter a number
-5
-5, is a negative number
RUN2
Enter a number
89
89, is a positive number
------------------------------*/
6. /* Write a C program to find the biggest of three numbers*/
#include
#include
#include
void main()
{
int a, b, c;
clrscr();
printf("Enter the values of a,b and c\n");
scanf ("%d %d %d", &a, &b, &c);
printf ("a = %d\tb = %d\tc = %d\n", a,b,c);
if ( a > b)
{
if ( a > c)
{
printf ("A is the greatest among three\n");
}
else
{
printf ("C is the greatest among three\n");
}
}
else if (b > c)
{
printf ("B is the greatest among three\n");
}
else
printf ("C is the greatest among three\n");
}
/*-----------------------------
Output
Enter the values of a,b and c
23 32 45
a = 23 b = 32 c = 45
C is the greatest among three
RUN2
Enter the values of a,b and c
234
678
195
a = 234 b = 678 c = 195
B is the greatest among three
RUN3
Enter the values of a,b and c
30 20 10
a = 30 b = 20 c = 10
A is the greatest among three
------------------------------*/
7. /* write a C program to find and output all the roots of a *
* quadratic equation, for non-zero coefficients. In case *
* of errors your program should report suitable error message*/
#include
#include
#include
#include
int main()
{
float A, B, C, root1, root2;
float realp, imagp, disc;
//clrscr();
printf("Enter the values of A, B and C\n");
scanf("%f %f %f", &A,&B,&C);
/* If A = 0, it is not a quadratic equation */
if( A==0 || B==0 || C==0)
{
printf("Error: Roots cannot be determined\n");
exit(1);
}
else
{
disc = B*B - 4.0*A*C;
if(disc < 0)
{
printf("Imaginary Roots\n");
realp = -B/(2.0*A) ;
imagp = sqrt(abs(disc))/(2.0*A);
printf("Root1 = %f +i %f\n",realp, imagp);
printf("Root2 = %f -i %f\n",realp, imagp);
}
else if(disc == 0)
{
printf("Roots are real and equal\n");
root1 = -B/(2.0*A);
root2 = root1;
printf("Root1 = %f \n",root1);
printf("Root2 = %f \n",root2);
}
else if(disc > 0 )
{
printf("Roots are real and distinct\n");
root1 =(-B+sqrt(disc))/(2.0*A);
root2 =(-B-sqrt(disc))/(2.0*A);
printf("Root1 = %f \n",root1);
printf("Root2 = %f \n",root2);
}
}
return 0;
} /* End of main() */
/*---------------------------
Output
RUN 1
Enter the values of A, B and C
3 2 1
Imaginary Roots
Root1 = -0.333333 +i 0.471405
Root2 = -0.333333 -i 0.471405
RUN 2
Enter the values of A, B and C
1 2 1
Roots are real and equal
Root1 = -1.000000
Root2 = -1.000000
RUN 3
Enter the values of A, B and C
3 5 2
Roots are real and distinct
Root1 = -0.666667
Root2 = -1.000000
---------------------------------*/
8. /* Write a C program to simulate a simple calculator to perform *
* arithmetic operations like addition, subtraction,multiplication *
* and division only on integers. Error message should be repoetrd *
* if any attempt is made to divide by zero */
#include
#include
void main()
{
char oper;/* oper is an operator to be selected */
float n1, n2, result;
clrscr();
printf ("Simulation of a Simple Calculator\n\n");
printf("Enter two numbers\n");
scanf ("%f %f", &n1, &n2);
fflush (stdin);
printf("Enter the operator [+,-,*,/]\n");
scanf ("%c", &oper);
switch (oper)
{
case '+': result = n1 + n2;
break;
case '-': result = n1 - n2;
break;
case '*': result = n1 * n2;
break;
case '/': result = n1 / n2;
break;
default : printf ("Error in operation\n");
break;
}
printf ("\n%5.2f %c %5.2f= %5.2f\n", n1,oper, n2, result);
}
/*-----------------------------
Output
Simulation of Simple Calculator
Enter two numbers
3 5
Enter the operator [+,-,*,/]
+
3.00 + 5.00= 8.00
RUN2
Simulation of Simple Calculator
Enter two numbers
12.75
8.45
Enter the operator [+,-,*,/]
-
12.75 - 8.45= 4.30
RUN3
Simulation of Simple Calculator
Enter two numbers
12 12
Enter the operator [+,-,*,/]
*
12.00 * 12.00= 144.00
RUN4
Simulation of Simple Calculator
Enter two numbers
5
9
Enter the operator [+,-,*,/]
/
5.00 / 9.00= 0.56
------------------------------*/
9. /* Write a C program to find the sum of 'N' natural numbers*/
#include
#include
void main()
{
int i, N, sum = 0;
clrscr();
printf("Enter an integer number\n");
scanf ("%d", &N);
for (i=1; i 0 )
{
rem = num % 2;
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 integer
75
Input number is = 75
Its Binary equivalent is = 1001011
No.of 1's in the binary number is = 4
RUN2
Enter a decimal integer
128
Input number is = 128
Its Binary equivalent is = 10000000
No.of 1's in the binary number is = 1
-----------------------------------------------------*/
46. /* 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);
if ( (year % 4) == 0)
printf("%d is a leap year",year);
else
printf("%d is not a leap year\n",year);
}
/*------------------------------------------
Output
Enter a year
2000
2000 is a leap year
RUN2
Enter a year
2007
2007 is not a leap year
------------------------------------------*/
47. /* 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
Enter two integers
23 34
Before swapping i= 23 and k = 34
After swapping i= 34 and k = 23
------------------------------------------*/
48. /* 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);
}
/*-----------------------------------------------
Output
Enter the number of days
375
375 is equivalent to 1 years, 1 weeks and 3 days
Enter the number of dayy
423
423 is equivalent tt 1 years, 8 weeks and 2 days
Enter the number of days
1497
1497 is equivalent to 4 years, 5 weeks and 2 days
---------------------------------------------------*/
49. /* 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 and
continues with subsequent characters until the corresponding characters
differ 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])
{
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:
happy
Enter another string:
HAPPY
String1 is greater than string2
RUN2
Enter a string:
Hello
Enter another string:
Hello
Both strings are equal
RUN3
Enter a string:
gold
Enter another string:
silver
String1 is less than string2
----------------------------------------*/50. /* Program to accept two strings and concatenate them *
* i.e.The second string is appended to the end of the first string */
#include
#include
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 string
travserse 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);
}
/*---------------------------------------
Output
Enter the first string :CD-
Enter the second string:ROM
First string = CD-
Second string = ROM
Concatenated string = CD-ROM
----------------------------------------*/
51. /* 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);
}
/*----------------------------------------------------
Output
Enter a string
hello
The length of a string is the number of characters in it
So, the length of hello = 5
RUN2
Enter a string
E-Commerce is hot now
The length of a string is the number of characters in it
So, the length of E-Commerce is hot now =21
----------------------------------------------------------*/
52. /* 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