78
Question 11- (d) To count the number of characters in a string #include<stdio.h> #include<conio.h> void main( ) { int a; float b,c,e,f,u; clrscr( ); printf("Select your shape:\n1.Rectangle\n2.Triangle"); scanf("%d",&a); if (a==1) { printf("Enter the length and breadht of the rectangle"); scanf("%f%f",&b,&c); } else { if (a==2) { printf("Enter the base and height of the triangle"); scanf("%f%f",&e,&f); } else printf("Invalid input"); } u=(e*f)/2; switch(a) { case 1: printf("The area of the rectangle is %f",b*c); break;

C Programs File LAB

Embed Size (px)

DESCRIPTION

It contains C Programs for first year students at various universities.

Citation preview

Question 11- (d) To count the number of characters in a string#include #include void main( ) { int a; float b,c,e,f,u; clrscr( ); printf("Select your shape:\n1.Rectangle\n2.Triangle"); scanf("%d",&a); if (a==1) { printf("Enter the length and breadht of the rectangle"); scanf("%f%f",&b,&c); } else { if (a==2) { printf("Enter the base and height of the triangle"); scanf("%f%f",&e,&f); } else printf("Invalid input"); } u=(e*f)/2; switch(a) { case 1: printf("The area of the rectangle is %f",b*c); break; case 2: printf("The area of the triangle is %f",u); break; } getch( ); }

#include #include void main( ) { clrscr( ); int c; float b,a; printf("Enter the first number"); scanf("%f",&a); printf("Enter the second number");scanf("%f",&b); printf("Select the operation to be performed: \n1.Addition \n2.Subtraction x x \n3.Multiplication \n4.Division"); scanf("%d",&c); switch (c) { case 1: printf("Their sum is %f",a+b); break; case 2: printf("Their difference is %f",a-b) ; break; case 3: printf("Their product is %f",a*b); break; case 4: printf("Dividing them we get %f",a/b); break; default: printf("Invalid input"); } getch( ); }

#include #include #include void main( ) { clrscr( ); float a,b,c,d,e,u; printf("Enter the coefficient of x^2\n"); scanf("%f",&a); printf("Enter the coefficient of x\n"); scanf("%f",&b); printf("Enter the constant term\n"); scanf("%f",&c); if(((b*b)-4*a*c)>=0) { d=(-b+pow(((b*b)-4*a*c),0.5))/(2*a); e=(-b-pow(((b*b)-4*a*c),0.5))/(2*a); printf("\nRoots are %f & %f\n",d,e); } if(((b*b)-4*a*c)=c)) printf("\n\nGreatest Number is %d",a); if((b>=a)&&(b>=c)) printf("\n\nGreatest Number is %d",b); if((c>=a)&&(c>=b)) printf("\n\nGreatest Number is %d",c); getch(); }

#include #include void main( ) { int number ,a , b=1; printf("Enter the number to multiply the digit of number\n"); scanf( "%d", &number ); while(number > 0 ) { a = number % 10; b = b * a ; number = number / 10 ; } printf("Multiply of digits = %d", b ); getch( ); }

#include void predigit(char num1, char num2); void postdigit(char c, int n); char romanval[1000]; int i = 0; int main( ) { int j; long number; printf("Enter the number: "); scanf("%d", &number); if (number = 1000) { postdigit('M', number / 1000); number = number - (number / 1000) * 1000; } else if (number >= 500) { if (number < (500 + 4 * 100)) { postdigit('D', number / 500); number = number - (number / 500) * 500; }

else { predigit('C','M'); number = number - (1000-100); } } else if (number >= 100) { if (number < (100 + 3 * 100)) { postdigit('C', number / 100); number = number - (number / 100) * 100; } else { predigit('L', 'D'); number = number - (500 - 100); } } else if (number >= 50 ) { if (number < (50 + 4 * 10)) { postdigit('L', number / 50); number = number - (number / 50) * 50; } else { predigit('X','C'); number = number - (100-10); } }

else if (number >= 10) { if (number < (10 + 3 * 10)) { postdigit('X', number / 10); number = number - (number / 10) * 10; } else { predigit('X','L'); number = number - (50 - 10); } } else if (number >= 5) { if (number < (5 + 4 * 1)) { postdigit('V', number / 5); number = number - (number / 5) * 5; } else { predigit('I', 'X'); number = number - (10 - 1); } } else if (number >= 1) { if (number < 4) { postdigit('I', number / 1); number = number - (number / 1) * 1; }

else { predigit('I', 'V'); number = number - (5 - 1); } } } printf("Roman number is: \n\n\n\n"); for(j = 0; j < i; j++) printf("%c", romanval[j]); return 0; } void predigit(char num1, char num2) { romanval[i++] = num1; romanval[i++] = num2; } void postdigit(char c, int n) { int j; for (j = 0; j < n; j++) romanval[i++] = c; }

#include #include int main( ) { int a, b, x, y, t, gcd, lcm; clrscr( ); printf("Enter two integers\n"); scanf("%d%d", &x, &y); a = x; b = y; while(b!=0) { t = b; b = a%b; a = t; } gcd = a; lcm = (x*y)/gcd; printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd); printf("Least common multiple of %d and %d = %d\n", x, y, lcm); return 0; }

#include #include void main( ) { clrscr( ); int ar[100],j,n,i,tmp; printf(" Enter the size of the array \t"); scanf("%d",&n); printf("Now enter the elements in the array \t"); for(i=0;i