1. /* Program to calculate the area of a triangle using the formula Area=s(s-a)(s-b)(s-c) where s=(a+b+c)/2 */ #include<stdio.h> main() { float a,b,c,s,area; printf(“Enter a,b,c values\n”); scanf(“%f%f%f”,&a,&b,&c); s=(a+b+c)/2; area=s*(s-a)*(s-b)*(s-c); printf(“The area of triangle is %f”,area); } OUTPUT Enter a,b,c values 10 20 20 The area of triangle is 9375.00
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1. /* Program to calculate the area of a triangle using the formulaArea=s(s-a)(s-b)(s-c) where s=(a+b+c)/2 */
printf("Value of \'a\' cannot be zero\n");exit(0);
}
disc=(b*b)-4*a*c;
if(disc>0){
printf("The roots are real and unequal\n");r1=(-b+sqrt(disc))/(2*a);r2=(-b-sqrt(disc))/(2*a);printf("Root--1 = %f\nRoot--2 = %f\n",r1,r2);
}
else if(disc= =0){
printf("The roots are real and equal\n");r1=-b/(2*a);printf("Root--1 = %f\nRoot--2 = %f\n",r1,r1);
}
else{
printf("The roots are complex\n");rp=-b/(2*a);ip=sqrt(-disc)/(2*a);printf("Root--1 = %f + i %f\n",rp,ip);printf("Root--2 = %f - i %f\n",rp,ip);
}}
OUTPUT
Enter a,b,c values1-56The roots are real and unequalRoot--1 = 3.000000Root--2 = 2.000000
5. /* Program to take two integer operands and one operator from the user and perform the operation */
#include<stdio.h>main(){
int a,b,c;char ch;
printf("Enter the operands a,b\n");scanf("%d%d",&a,&b);
printf("\nPress + for addition\nPress - for subtraction\n");printf("Press * for multiplication\nPress / for division\n");printf("\nEnter the operator of your choice\n");scanf(" %c",&ch); switch(ch)
{ case '+':
{c=a+b;printf("The sum is\t%d",c);}break;
case '-':{c=a-b;printf("The difference is\t%d",c);}break;
case '*':{c=a*b;printf("The product is\t%d",c);}break;
case '/':{c=a/b;printf("The quotient is \t%d",c);}break;
default:printf("You have entered wrong choice\n”);
}}
OUTPUT
Enter the operands a,b1526
Press + for additionPress - for subtractionPress * for multiplicationPress / for division
Enter the operator of your choice*The product is 390
6. /* Program to find the sum of the individual digits of a number */
#include<stdio.h>main(){
int n,s=0,r;printf("Enter n value");scanf("%d",&n);while(n>0){
r=n%10;s=s+r;n=n/10;
}printf("the sum of the digits of given no. is %d",s);
}
OUTPUT
Enter n value123
the sum of the digits of given no. is 6
7. /* Program to find the reverse of a number */
#include<stdio.h>main(){int n,rev=0,r;printf("Enter n value");scanf("%d",&n);while(n>0){
r=n%10;rev=rev+10*r;n=n/10;
}printf("The reverse of a given no. is %d",rev);
}
OUTPUT
Enter n value123
The reverse of a given no. is 321
8. /* Program to generate n Fibonacci series */
#include<stdio.h>main(){int f1,f2,f3,n,i;printf("Enter the length of the series:");scanf("%d",&n);printf("The fibonacci series\n");
f1=0;f2=1;
printf("%d \n%d",f1,f2);for(i=0;i<=n;i++){
f3=f1+f2;printf("\n%d",f3);f1=f2;f2=f3;
}
}
OUTPUT
Enter the length of the series:10
The fibonacci series
01123581321345589144
9. /* Program to generate all the prime numbers between 2 and n */
#include<stdio.h>main(){int flag,i,j,n;printf("Enter n value : ");scanf("%d",&n);for(i=2;i<=n;i++){ flag=0; for(j=1;j<=i;j++)
{ if(i%j==0) flag=flag+1;}
if(flag==2)printf(“%d\n”,i);
}}
OUTPUT
Enter n value : 25
23571113171923
10. /* Program to print the multiplication table of a given number n up to a given value where n is entered by the user */
#include<stdio.h>main(){
int n,i,range;printf(“Enter N value\n”);scanf(“%d”,&n);printf(“Enter the range\n”);scanf(“%d”,&range);for(i = 1 ; i<=range ; i++){
}printf(“The largest value is%d\n”,large);printf(“The smallest value is%d\n”,small);
}
OUTPUT
Enter array elements5 2 10 45 19 20 27 26 30 15The largest value is 45The smallest value is 2
15. /* Program to implement sorting of elements in an array */
#include<stdio.h>main(){
int i,j,temp,a[10];printf(“Enter array elements\n”);for(i = 0 ; i<10 ; i++){
scanf(“%d”,&a[i]);}
for(i = 0 ; i<10 ; i++){
for(j = i+1 ; j<10 ; j++){
if(a[i]>a[j]){
temp=a[i];a[i]=a[j];a[j]=temp;
}}
}printf(“The elements after sorting\n”);for(i = 0 ; i<10 ; i++)
printf(“%d\t”,a[i]);}
OUTPUT
Enter array elements4 8 6 23 11 34 1 2 0 5The elements after sorting are0 1 2 4 5 6 8 11 23 34
16. /* Program for addition of two matrices */
#include<stdio.h> main(){
int a[5][5],b[5][5],c[5][5],r1,c1,r2,c2,i,j;printf("Enter matrix A order\n");scanf("%d%d",&r1,&c1);printf("Enter matrix B order\n");scanf("%d%d",&r2,&c2);
if(r1==r2 && c1==c2){
printf("Enter matrix A elements (%d)\n",r1*c1);for(i=0;i<r1;i++){ for(j=0;j<c1;j++) {
scanf(“%d”,&a[i][j]); } }printf("Enter matrix B elements (%d)\n",r2*c2);for(i=0;i<r2;i++){ for(j=0;j<c2;j++) {
scanf(“%d”,&b[i][j]); }
}printf(“\nThe elements of matrix A\n”);for(i=0;i<r1;i++){
for(j=0;j<c1;j++)printf("%d\t",a[i][j]);
printf("\n");}printf(“\nThe elements of matrix B\n”);for(i=0;i<r2;i++){
for(j=0;j<c2;j++)printf("%d\t",b[i][j]);
printf("\n");
}printf(“\nThe addition of two matrices is\n”);for(i=0;i<r1;i++){
printf("Enter Matrix A size\n");scanf("%d %d",&m,&n);printf("\nEnter Matrix B size\n”);scanf("%d %d",&p,&q);if(n==p){printf("\nEnter matrix A elements\n");for(i=0;i<m;i++) {
for(j=0;j<n;j++)scanf(“%d”,&a[i][j]);
}printf("\n Enter matrix B elements\n");for(i=0;i<p;i++) {
for(j=0;j<q;j++)scanf(“%d”,&b[i][j]);
}printf(“\nThe elements of matrix A\n”);for(i=0;i<m;i++) {
for(j=0;j<n;j++)printf(“%d\t”,a[i][j]);
printf(“\n”); }printf(“\nThe elements of matrix B\n”);for(i=0;i<p;i++) {
for(j=0;j<q;j++)printf(“%d\t”,b[i][j]);
printf(“\n”); }printf("\n Multipliction of two matrices is:\n");for(i=0;i<m;i++){ for(j=0;j<q;j++) { for(k=0;k<n;k++)
19. /* Program to find the length of the string without using library function */
#include<stdio.h>main(){
char name[30];int i=0;printf(“Enter a string\n”);scanf(“%s”,name);while(name[i]!=’\0’){
i++;}printf(“The length of the given string is %d”,i);
}
OUTPUT
Enter a stringhelloThe length of the given string is 5
20. /*Program to check the string for its palindrome property*/
#include<stdio.h>#include<string.h>main(){ char strsrc[30]; char strtmp[30]; printf("\n Enter String:= "); gets(strsrc); strcpy(strtmp,strsrc); strrev(strtmp); if(strcmp(strsrc,strtmp)==0) printf("\n Entered string \"%s\" is a palindrome",strsrc); else printf("\n Entered string \"%s\" is not a palindrome",strsrc);}