Top Banner
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
33

c programming

Dec 07, 2015

Download

Documents

Sai Kumar Sai

variousd programmes
Welcome message from author
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
Page 1: c programming

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 */

#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 values102020The area of triangle is 9375.00

2. /* Program to find the largest of three numbers using ternary operator */

Page 2: c programming

#include<stdio.h>main(){

int a,b,c,large;

printf(“Enter a,b,c values\n”);scanf(“%d%d%d”,&a,&b,&c);

large=( a > b ? ( a > c ? a : c ) : ( b > c ? b : c));

printf(“The largest number is %d”,large);

}

OUTPUT

Enter a,b,c values102030The largest number is 30

Page 3: c programming

3. /* Program for swapping of two numbers without using temporary variable*/

#include<stdio.h>main(){

int a,b;printf(“Enter a, b values\n”);scanf(“%d%d”,&a,&b);a=a+b;b=a-b;a=a-b;printf(“The values of A and B after swapping \nA=%d\tB=%d\n”,a,b);

}

OUTPUT

Enter a, b values1020The values of A and B after swappingA=20 B=10

Page 4: c programming

4. /* Program to find the roots of a quadratic equation */

#include<stdio.h>#include<math.h>

main(){

float a,b,c,r1,r2,disc,rp,ip;

printf("Enter a,b,c values\n");scanf("%f%f%f",&a,&b,&c);

if(a= =0){

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);

}}

Page 5: c programming

OUTPUT

Enter a,b,c values1-56The roots are real and unequalRoot--1 = 3.000000Root--2 = 2.000000

Page 6: c programming

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”);

}}

Page 7: c programming

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

Page 8: c programming

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

Page 9: c programming

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

Page 10: c programming

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

Page 11: c programming

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

Page 12: c programming

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(“%d * %d = %d\n”,n,i,n*i);}

}

OUTPUT

Enter N value5Enter the range125 * 1 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 255 * 6 = 305 * 7 = 355 * 8 = 405 * 9 = 455 * 10 = 505 * 11 = 555 * 12 = 60

Page 13: c programming

11. /* Program to check whether the given number is Armstrong or not */

#include<stdio.h>main(){

int num,sum=0,r,n;printf(“Enter a number\n”);scanf(“%d”,&num);n=num;while(num>0){

r=num%10;sum=sum+(r*r*r);num=num/10;

}if(sum==n)

printf(“The number %d is Armstrong number”,n);else

printf(“The number %d is not Armstrong number”,n);}

OUTPUT

Enter a number153The number 153 is Armstrong number

Page 14: c programming

12. /* Program to convert decimal number into its binary equivalent */

#include<stdio.h>main(){

int dec,bin=0;int decimal,digit,base=1;printf("\nInput a decimal number:");scanf("%d",&decimal);dec=decimal;

while(decimal>0){

digit=decimal%2; bin=bin+digit*base;

base=base*10; decimal=decimal/2; }

printf("\nBinary equivalent of Decimal number \t %d = %d \n",dec,bin);}

OUTPUT

Input a decimal number:30

Binary equivalent of Decimal number 30 = 11110

Page 15: c programming

13. /* Program to implement linear search */

#include<stdio.h>main(){

int i,ele,found=0,a[10];printf(“Enter array elements\n”);for(i =0 ; i<10 ; i++){

scanf(“%d”,&a[i]);}printf(“Enter Search element\n”);scanf(“%d”,&ele);for(i = 0 ; i<10 ; i++){

if(ele==a[i])found=found+1;

}if(found>0)

printf(“The search element found in the array\n”);else

printf(“The search element not found in the array\n”);}

OUTPUT

Enter array elements10 20 30 40 50 60 70 80 90 100Enter Search element50The search element found in the array

Page 16: c programming

14. /* Program to find the largest and smallest number in an array */

#include<stdio.h>main(){

int i,large,small,a[10];printf(“Enter array elements\n”);for(i = 0 ; i<10 ; i++){

scanf(“%d”,&a[i]);}large=a[0];small=a[0];for(i = 0 ; i<10 ; i++){

if(a[i]>large)large=a[i];

if(a[i]<small)small=a[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

Page 17: c programming

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

Page 18: c programming

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");

Page 19: c programming

}printf(“\nThe addition of two matrices is\n”);for(i=0;i<r1;i++){

for(j=0;j<c1;j++){ c[i][j]=a[i][j]+b[i][j]; printf(“%d\t”,c[i][j]);}

printf(“\n”);}

}

else

printf("Matrix addition not possible!!!");}

Page 20: c programming

OUTPUTEnter matrix A order2 2 Enter matrix B order2 2 Enter matrix A elements(4)1 2 3 4

Enter matrix B elements (4)

1 1 1 1

The elements of matrix A are

1 2

3 4

The elements of matrix B are

1 1

1 1

The addition of two matrices are

2 3

4 5

Page 21: c programming

17. /* Program to calculate the transpose of a matrix */

#include<stdio.h>int main(){int a[10][10],r1,c1,i,j;printf("Enter matrix order:");scanf("%d%d",&r1,&c1);printf("Enter matrix elements (%d)\n",r1*c1); for(i=0;i<r1;i++) {

for(j=0;j<c1;j++)scanf(“%d”,&a[i][j]);

}printf(“\n”The given matrix is\n”); for(i=0;i<r1;i++) {

for(j=0;j<c1;j++)printf(“%d\t”,a[i][j]);

printf(“\n”);}

printf(“\nThe transpose of the given matrix is\n”); for(i=0;i<r1;i++) {

for(j=0;j<c1;j++)printf(“%d\t”,a[j][i]);

printf(“\n”);}

}

Page 22: c programming

OUTPUT

Enter matrix order:2

2Enter matrix elements (4)2213

The given matrix is2 2

1 3

The transpose of the given matrix is

2 1

2 3

Page 23: c programming

18. /* Program for multiplication of two matrices */

#include<stdio.h>main(){int a[10][10],b[10][10],c[10][10]={0}, i,j,m,n,k,p,q;

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++)

c[i][j]=c[i][j]+a[i][k]*b[k][j];

Page 24: c programming

} }for(i=0;i<m;i++) { for(j=0;j<n;j++) printf(“%d\t”,c[i][j]); printf(“\n”); }}

Page 25: c programming

OUTPUT

Enter Matrix A size

2

2

Enter Matrix B size

2

2

Enter matrix A elements

1234

Enter matrix B elements

1234

The elements of matrix A

1 2

3 4

The elements of matrix B

1 2

3 4

Multipliction of two matrices is:

7 10

15 22

Page 26: c programming

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*/

Page 27: c programming

#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);}

OUTPUT

Enter String:= madam

Entered string "madam" is a palindrome

Enter String:= aditya

Entered string "aditya" is not a palindrome