0% found this document useful (0 votes)
20 views4 pages

C Programming Basics and Examples

Uploaded by

sharmarahul0725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

C Programming Basics and Examples

Uploaded by

sharmarahul0725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

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

int main() {
double x = 4.0;
printf("sin(%.2f) = %.2f\n", x, sin(x));
printf("cos(%.2f) = %.2f\n", x, cos(x));
printf("log(%.2f) = %.2f\n", x, log(x));
printf("pow(%.2f, 2) = %.2f\n", x, pow(x,2));
printf("sqrt(%.2f) = %.2f\n", x, sqrt(x));
return 0;
}

2. Roots of Quadratic Equation


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

int main() {
double a, b, c, D, root1, root2;
printf("Enter coefficients a, b, c: ");
scanf("%lf %lf %lf", &a, &b, &c);
D = b*b - 4*a*c;
if (D >= 0) {
root1 = (-b + sqrt(D))/(2*a);
root2 = (-b - sqrt(D))/(2*a);
printf("Roots: %.2lf and %.2lf\n", root1, root2);
} else {
printf("No real roots.\n");
}
return 0;
}

3. Formatting Output
#include <stdio.h>

int main() {
printf("Hello\nWorld\tTabbed\bBackslash\'Quote\\Slash\rCarriage\aBeep");
return 0;
}

4. sizeof Operator
#include <stdio.h>

int main() {
printf("Size of char: %zu\n", sizeof(char));
printf("Size of int: %zu\n", sizeof(int));
printf("Size of long int: %zu\n", sizeof(long int));
printf("Size of float: %zu\n", sizeof(float));
printf("Size of double: %zu\n", sizeof(double));
return 0;
}

5. Type Casting Division


#include <stdio.h>

int main() {
int sum = 7, n = 2;
double mean;
mean = (double)sum / n;
printf("Mean = %.2f\n", mean);
return 0;
}

6. Leap Year
#include <stdio.h>

int main() {
int year;
printf("Enter year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}

7. Character Check
#include <stdio.h>
#include <ctype.h>

int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if (isupper(ch)) printf("Capital Letter\n");
else if (islower(ch)) printf("Small Letter\n");
else if (isdigit(ch)) printf("Digit\n");
else printf("Special Symbol\n");
return 0;
}

8. Library Fine
#include <stdio.h>

int main() {
int days;
printf("Enter number of days late: ");
scanf("%d", &days);
if (days <= 5)
printf("Fine = %.2f\n", days * 0.5);
else if (days <= 10)
printf("Fine = %d\n", days * 1);
else if (days <= 30)
printf("Fine = %d\n", days * 5);
else
printf("Membership Cancelled\n");
return 0;
}

9. Steel Grade
#include <stdio.h>

int main() {
int hardness, tensile;
float carbon;
printf("Enter hardness, carbon content, tensile strength: ");
scanf("%d %f %d", &hardness, &carbon, &tensile);
if (hardness > 50 && carbon < 0.7 && tensile > 5600) printf("Grade = 10\n");
else if (hardness > 50 && carbon < 0.7) printf("Grade = 9\n");
else if (carbon < 0.7 && tensile > 5600) printf("Grade = 8\n");
else if (hardness > 50 && tensile > 5600) printf("Grade = 7\n");
else if (hardness > 50 || carbon < 0.7 || tensile > 5600) printf("Grade = 6\n");
else printf("Grade = 5\n");
return 0;
}

10. Calculator
#include <stdio.h>

int main() {
int a, b;
char op;
printf("Enter expression (a op b): ");
scanf("%d %c %d", &a, &op, &b);
switch(op) {
case '+': printf("Result = %d\n", a+b); break;
case '-': printf("Result = %d\n", a-b); break;
case '*': printf("Result = %d\n", a*b); break;
case '/': printf("Result = %d\n", a/b); break;
case '%': printf("Result = %d\n", a%b); break;
default: printf("Invalid operator\n");
}
return 0;
}

11. Month Name


#include <stdio.h>
int main() {
int month;
printf("Enter month number: ");
scanf("%d", &month);
switch(month) {
case 1: printf("January\n"); break;
case 2: printf("February\n"); break;
case 3: printf("March\n"); break;
case 4: printf("April\n"); break;
case 5: printf("May\n"); break;
case 6: printf("June\n"); break;
case 7: printf("July\n"); break;
case 8: printf("August\n"); break;
case 9: printf("September\n"); break;
case 10: printf("October\n"); break;
case 11: printf("November\n"); break;
case 12: printf("December\n"); break;
default: printf("Invalid month\n");
}
return 0;
}

12. Executive Salary


#include <stdio.h>

int main() {
int job, level;
float basic, perks, hra, gross, tax, net;
printf("Enter job number, level, basic pay: ");
scanf("%d %d %f", &job, &level, &basic);
hra = 0.25 * basic;
if (level == 1) perks = 1000;
else if (level == 2) perks = 750;
else if (level == 3) perks = 500;
else perks = 250;
gross = basic + hra + perks;
if (gross > 20000) tax = 0.2 * gross;
else if (gross > 10000) tax = 0.1 * gross;
else tax = 0.05 * gross;
net = gross - tax;
printf("Job: %d, Gross: %.2f, Net: %.2f\n", job, gross, net);
return 0;
}

You might also like