Tanti Technology

My photo
Bangalore, karnataka, India
Sandeep Kumar | Founder, Tanti Technologies At Tanti Technologies, I share practical knowledge, hands-on tutorials, and real-world solutions across Python, SQL, Docker, Kubernetes, CI/CD, AWS, Azure, Machine Learning, Generative AI, and Agentic AI. My mission is to simplify complex technologies and provide actionable insights that help developers, engineers, and businesses learn faster, build better, automate smarter, and innovate with confidence. 🚀 Learn • Build • Automate • Innovate Follow Tanti Technologies for practical technology insights and next-generation AI solutions.

Monday, 4 July 2011

C program-3

1. /* Write a C program to find the biggest of three numbers*/

#include
#include

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

***************************************************************

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

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

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


************************************************************

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

main()
{
char operator;
float n1, n2;
float sum, diff, prod, quot, result;

clrscr();

printf ("Simulation of Simple Calculator\n\n");

printf("Enter two numbers\n");
scanf ("%f %f", &n1, &n2);

fflush (stdin);

printf("Enter the operator [+,-,*,/]\n");
scanf ("%c", &operator);


switch (operator)
{
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,operator, 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


------------------------------*/

**************************************************************************


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

main()
{
char operator;
float n1, n2;
float sum, diff, prod, quot, result;

clrscr();

printf ("Simulation of Simple Calculator\n\n");

printf("Enter two numbers\n");
scanf ("%f %f", &n1, &n2);

fflush (stdin);

printf("Enter the operator [+,-,*,/]\n");
scanf ("%c", &operator);


switch (operator)
{
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,operator, 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


------------------------------*/

***********************************************************************

5. /*Write a C program to generate and print first N FIBONACCI numbers*/

#include
main()
{
int fib1=0, fib2=1, fib3, N, count=0;

printf("Enter the value of N\n");
scanf("%d", &N);

printf("First %d FIBONACCI numbers are ...\n", N);
printf("%d\n",fib1);
printf("%d\n",fib2);
count = 2; /* fib1 and fib2 are already used */

while( count < N) { fib3 = fib1 + fib2; count ++; printf("%d\n",fib3); fib1 = fib2; fib2 = fib3; } } /* End of main() */ /*-------------------------- Enter the value of N 10 First 5 FIBONACCI numbers are ... 0 1 1 2 3 5 8 13 21 34 -------------------------------*/ ******************************************************************** 6. /* Write a C program to find the GCD and LCM of two integers * * output the results along with the given integers. Use Euclids' algorithm*/ #include

main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;
clrscr();

printf("Enter two numbers\n");
scanf("%d %d", &num1,&num2);

if (num1 > num2)
{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
}
remainder = num1 % num2;
while(remainder !=0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d \n", num1,num2,gcd);
printf("LCM of %d and %d = %d \n", num1,num2,lcm);
} /* End of main() */
/*------------------------
Output
RUN 1
Enter two numbers
5
15
GCD of 5 and 15 = 5
LCM of 5 and 15 = 15
------------------------------*/

********************************************************************************

No comments:

Post a Comment