You are on page 1of 15

1.

a. Write a program to find the area of a triangle (Given the three sides).

#include <stdio.h> #include <math.h> main() { int a,b,c; float s,area; system("clear"); printf("Enter the 3 sides of the triangle\n"); scanf("%d%d%d",&a,&b,&c); if( (a+b)>c && (b+c)>a && (c+a)>b ) { s=(a+b+c)/2.0; area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("Area of the triangle is = %f\n",area); } else printf("The triangle can't be formed\n");

OUTPUT: Enter the 3 sides of the triangle 2 3 4 Area of the triangle is = 2.904737

1. b. Write a program to find the area of a circle (Given the radius). #include <stdio.h> #define PI 3.142 main() {

float r,area; system("clear"); printf("Enter the radius of the circle\n"); scanf("%f",&r); area=PI*r*r; printf("Area of the circle is = %f\n",area);

OUTPUT: Enter the radius of the circle 5 Area of the circle is = 78.550

2.

Write a program to find the Simple interest, given the principle, time and rate of interest with appropriate validations.

#include <stdio.h> main() { float p,t,r,si; system("clear"); printf("Enter the principle\n"); scanf("%f",&p); printf("Enter the time\n"); scanf("%f",&t); printf("Enter the rate\n"); scanf("%f",&r); si=(p*t*r)/100; printf("Simple Interest = %.2f\n",si); }

OUTPUT: Enter the principle 1000 Enter the time 5 Enter the rate 2 Simple Interest = 100.00

3.

Write a program to find out whether a given year is a leap year or not.

#include <stdio.h> main() {

int year; system("clear"); printf("Enter any year\n"); scanf("%d",&year); if( (year%400==0) || (year%100!=0 && year%4==0)) printf("It is a Leap Year\n"); else printf("It is Not a Leap Year\n");

/* OUTPUT: Enter any year 2000 It is a Leap Year Enter any year 1900 It is Not a Leap Year */

4.

Write a program to find the roots of a quadratic equation with appropriate error messages.

#include <stdio.h> #include <math.h> main() {

float a,b,c,d,x1,x2; system("clear"); printf("Enter the co-efficients of a Quadratic Equation\n"); scanf("%f%f%f",&a,&b,&c); d=b*b-4*a*c; if(a==0) { printf("Roots can't be determined\n"); } else if(d==0) { printf("Roots are Equal\n"); x1=x2=-b/(2*a); printf("Roots = %f\n",x1); } else if(d>0) { printf("Roots are Real and Distinct\n"); x1=(-b+sqrt(d))/(2*a); x2=(-b-sqrt(d))/(2*a); printf("Roots are:\nx1=%f\tx2=%f\n",x1,x2); } else { printf("Roots are Imaginary\n"); x1=-b/(2*a); x2=sqrt(fabs(d))/(2*a); printf("Roots are:\n%f+i%f and %f-i%f\n",x1,x2,x1,x2); }

} /* OUTPUT: Enter the co-efficients of a Quadratic Equation 1 2 3 Roots are Imaginary Roots are: -1.000000+i1.414214 and -1.000000-i1.414214 Enter the co-efficients of a Quadratic Equation 2 4 1 Roots are Real and Distinct Roots are: x1=-0.292893 x2=-1.707107 Enter the co-efficients of a Quadratic Equation 2 4 2 Roots are Equal Roots = -1.000000 */

5.

Write a program to display the following files of current directory. i) .EXE files ii) .BAT files iii) .OBJ files iv) .BAK files. By using system DOS command.

#include <stdio.h> #include <process.h> main() { printf("Displaying all '.EXE' files in the current directory\n"); system("dir *.exe/p/w"); getch(); printf("\nDisplaying all '.BAT' files in the current directory\n"); system("dir *.bat/p/w"); getch(); printf("\nDisplaying all '.OBJ' files in the current directory\n"); system("dir *.obj/p/w"); getch(); printf("\nDisplaying all '.BAK' files in the current directory\n"); system("dir *.bak/p/w");

/* OUTPUT: Displaying all '.EXE' files in the current directory 5_DOS-~1.EXE BGIOBJ.EXE CINSTXFR.EXE CPP.EXE TCC.EXE TCCONFIG.EXE TLINK.EXE WORDCNT.EXE Displaying all '.BAT' files in the current directory AUTOEXEC.BAT temp.bat batch.bat Displaying all '.OBJ' files in the current directory 5_DOS-~1.OBJ ASCII.OBJ BIN.OBJ BOOT.OBJ C0H.OBJ C0L.OBJ C0M.OBJ C0S.OBJ Displaying all '.BAK' files in the current directory ARRAY.BAK ARRAY2.BAK ARRAY3.BAK ARRAY4.BAK MINE.BAK MINE1.BAK PIXEL.BAK SB.BAK */

TC.EXE

C0C.OBJ C0T.OBJ HELLO.BAK SUB.BAK

6.

Write a program to find GCD and LCM of given two numbers.

#include <stdio.h> main() {

int m,n,a,b,gcd,lcm; printf("Enter 2 numbers\n"); scanf("%d%d",&m,&n); a=m,b=n; while(m!=n) { if(m>n) m=m-n; else n=n-m; } gcd=m; lcm=(a*b)/gcd; printf("GCD = %d\nLCM = %d\n",gcd,lcm);

} /* OUTPUT: Enter 2 numbers 16 48 GCD = 16 LCM = 48 */

7.

Write a program to find the value of Sin (x) using the series. Sin (x) = x x3/3! + x5/5! x7/7! + .

#include <stdio.h> #define Pi 3.1412 main() {

int n,i; float sum,degree,term,x; printf("Enter the value: "); scanf("%d",&n); printf("Enter the degree: "); scanf("%f",&degree); x=(degree*Pi)/180; term=x; sum=term; for(i=1; i<=n-1; i++) { term = (-term*x*x) / (2*i*(2*i+1)); sum = sum + term; } printf("The value of sin(%.2f) = %f\n",degree,sum);

/* OUTPUT: Enter the value: 3 Enter the degree: 90 The value of sin(90.00) = 1.004521 Enter the value: 3 Enter the degree: 60 The value of sin(60.00) = 0.866230 */

8.

Write a program to print all prime numbers between m and n.

#include <stdio.h> main() {

int m,n,p,i,j; printf("Enter the range of numbers\n"); scanf("%d%d",&m,&n); if(m>n) m^=n,n^=m,m^=n; printf("The prime numbers between %d to %d are:\n",m,n); if(m==1) m=2; for(i=m; i<=n; i++) { for(p=1,j=2; j<=i/2; j++) if(i%j==0) p=0; if(p) printf("%d\t",i); }

/* OUTPUT: Enter the range of numbers 1 25 The prime numbers between 1 to 25 are: 2 3 5 7 11 13 17 */

19

23

9.

Write a program to reverse a number and check whether it is palindrome or Not.

#include <stdio.h> main() { int n,tmp,rem,rev=0; printf("Enter the value of N\n"); scanf("%d",&n); tmp=n; while(n>0) { rem=n%10; rev=rev*10+rem; n=n/10; } if(tmp==rev) printf("%d is Palindrome\n",tmp); else printf("%d is not a Plaindrome\n",tmp); }

/* OUTPUT: Enter the value of N 121 121 is Palindrome Enter the value of N 123 123 is not a Plaindrome */

10. Write a program to generate and print first n Fibonacci numbers using function. #include <stdio.h> main() { int n; void fibo(int); printf("Enter the value of N\n"); scanf("%d",&n); fibo(n);

void fibo(int n) { int i,f0,f1,f2; if(n==1) printf("The first value in the fibonacci series is: \n0\n"); else if(n>1) { printf("The first %d fibonacci numbers are:\n0\n1\n",n); i=3,f0=0,f1=1; while(i<=n) { f2=f0+f1; printf("%d\n",f2); f0=f1; f1=f2; i++; } } else printf("Invalid Input\n"); } /* OUTPUT: Enter the value of N 1 The first value in the fibonacci series is: 0 Enter the value of N 3 The first 3 fibonacci numbers are: 0 1 1 Enter the value of N -1 Invalid Input */

11. Write a program to find a factorial of a given number using recursive function. #include <stdio.h> main() {

int n,fact(int); printf("Enter the vlue of N\n"); scanf("%d",&n); printf("The factorial of %d is = %d\n",n,fact(n));

int fact(int n) { if(n<0) { printf("Invalid Input\n"); exit(1); } else if(n<2) return 1; else return n*fact(n-1); }

/* OUTPUT: Enter the vlue of N 4 The factorial of 4 is = 24 Enter the vlue of N 0 The factorial of 0 is = 1 Enter the vlue of N -1 Invalid Input */

12. Write a program to convert UPPERCASE alphabets to LOWERCASE alphabets in a given string and vice-versa. #include <stdio.h> #include <string.h> main() {

int i=1,len; char str[30]; printf("Enter a string\n"); scanf("%[^\n]s",str); len=strlen(str); for(i=0; i<len; i++) { if(str[i]>=97 && str[i]<=122) str[i] = str[i] - 32; else if(str[i]>=65 && str[i]<=90) str[i] = str[i] + 32; } printf("Resultant string (after CASE Conversion) = %s\n",str);

/* OUTPUT: Enter a string hEllO WoRLd !! @!#%$? Resultant string (after CASE Conversion) = HeLLo wOrlD !! @!#%$? */

13. Write a program to read two strings and concatenate them (without using library functions). #include <stdio.h> main() { int i,j; char s1[' '],s2[' '],flush; printf("Enter String 1:\n"); scanf("%[^\n]s",s1); scanf("%c",&flush); // Flush the New Line Char printf("Enter String 2:\n"); scanf("%[^\n]s",s2); for(i=0; s1[i]!='\0'; i++); // Length od s1 for(j=0; s2[j]!='\0'; j++) // Append s2 chars at the end of s1 { s1[i] = s2[j]; i++; } s1[i]='\0'; // Insert EOS char to s1 printf("Concatenated String is = %s\n",s1);

/* OUTPUT: Enter String 1: Hello Enter String 2: World!! Concatenated String is = Hello World!! */

14. Write a program to read a sentence and count the number of vowels and constants. #include <stdio.h> main() { int i,v=0,c=0; char str[' ']; printf("Enter a Sentence\n"); scanf("%[^\n]s",str); for(i=0; str[i]!='\0'; i++) { switch(str[i]) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': v++; break; default: if(isalpha(str[i])) c++; } } printf("The sentence '%s' has %d vowels and %d consonants\n",str,v,c); }

/* OUTPUT: Enter a Sentence This is a Simple Sentence The sentence 'This is a Simple Sentence' has 8 vowels and 17 consonants */

You might also like