KEMBAR78
C Basic Excercise | PDF | Fahrenheit | Area
0% found this document useful (0 votes)
36 views13 pages

C Basic Excercise

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

C Basic Excercise

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

/* C program to demonstrate input output using scanf() and print() */

#include <stdio.h>

int main()
{
int num1, num2, sum;

/* Simple message to user */


printf("Enter two numbers: ");

/* Input value in two variables */


scanf("%d%d", &num1, &num2);

/* Find sum */
sum = num1 + num2;

/* Print sum */
printf("Sum = %d", sum);

return 0;
}
• Statement int num1, num2, sum; declares three integer variables.
• Next, printf("Enter two numbers: "); display simple message to user to input two
numbers. It is considered as a good programming practice to display a message
before every input to user. Doing this will notify the user to provide some input.
• In statement scanf("%d%d", &num1, &num2);, %d tells scanf() function to read
an integer variable and store in num1. Similarly, second %d also tells scanf() to
read integer value and store in num2.
• The statement sum = num1 + num2 calculates sum of num1 and num2 and
stores in sum.
• Finally, in the statement printf("Sum = %d", sum); Sum = is printed as it is as a
simple message.
• Next, after printf() encounters format specifier %d. It replaces %d with the value
of sum, which finally prints sum of both numbers.
/* C program to find area of rectangle */

#include <stdio.h>

int main()
{
float length, width, area;

/*
* Input length and width of rectangle
*/
printf("Enter length of rectangle: ");
scanf("%f", &length);
printf("Enter width of rectangle: ");
scanf("%d", &width);

/* Calculate area of rectangle */


area = length * width;

/* Print area of rectangle */


printf("Area of rectangle = %f sq. units ", area);

return 0;
}
/**
* C program to convert temperature from degree celsius to
fahrenheit
*/

#include <stdio.h>

int main()
{
float celsius, fahrenheit;

/* Input temperature in celsius */


printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);

/* celsius to fahrenheit conversion formula */


fahrenheit = (celsius * 9 / 5) + 32;

printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);

return 0;
}
**
* C program to convert days in to years, weeks and days
*/

#include <stdio.h>

int main()
{
int days, years, weeks,days1;

/* Input total number of days from user */


printf("Enter days: ");
scanf("%d", &days);

/* Conversion */
years = (days / 365); // Ignoring leap year
weeks = (days % 365) / 7;
days = days - ((years * 365) + (weeks * 7));

/* Print all resultant values */


printf("YEARS: %d\n", years);
printf("WEEKS: %d\n", weeks);
printf("DAYS: %d", days);

return 0;
/* C program to find power of any number */

#include <stdio.h>
#include <math.h> // Used for pow() function

int main()
{
double base, expo, power;

/* Input two numbers from user */


printf("Enter base: ");
scanf("%lf", &base);
printf("Enter exponent: ");
scanf("%lf", &expo);

/* Calculates base^expo */
power = pow(base, expo);

printf("%.2lf ^ %.2lf = %.2lf", base, expo, power);

return 0;
}
%.2lf is used to print fractional value up to 2 decimal places.
/*C program to calculate simple interest */

#include <stdio.h>
int main()
{
float principle, time, rate, SI;
/* Input principle, rate and time */
printf("Enter principle (amount): ");
scanf("%f", &principle);
printf("Enter time: ");
scanf("%f", &time);
printf("Enter rate: ");
scanf("%f", &rate);

/* Calculate simple interest */


SI = (principle * time * rate) / 100;

/* Print the resultant value of SI */


printf("Simple Interest = %f", SI);
return 0;
}
// Program to calculate compound Interest
#include <stdio.h>
#include <math.h>

int main()
{
float principle, rate, time, CI;
/* Input principle, time and rate */
printf("Enter principle (amount): ");
scanf("%f", &principle);
printf("Enter time: ");
scanf("%f", &time);
printf("Enter rate: ");
scanf("%f", &rate);
/* Calculate compound interest */
CI = principle* (pow((1 + rate / 100), time));

/* Print the resultant CI */


printf("Compound Interest = %f", CI);

return 0;
}
/**
* C program to find area of an equilateral triangle
*/

#include <stdio.h>
#include <math.h> // Used for sqrt() function

int main()
{
float side, area;

/* Input side of equilateral triangle */


printf("Enter side of an equilateral triangle: ");
scanf("%f", &side);

/* Calculate area of equilateral triangle */


area = (sqrt(3) / 4) * (side * side);

/* Print resultant area */


printf("Area of equilateral triangle = %.2f sq. units", area);

return 0;
}
/**
* C program to convert centimeter into meter and kilometer
*/

#include <stdio.h>

int main()
{
float cm, meter, km;

/* Input length in centimeter from user */


printf("Enter length in centimeter: ");
scanf("%f", &cm);

/* Convert centimeter into meter and kilometer */


meter = cm / 100.0;
km = cm / 100000.0;

printf("Length in Meter = %.2f m \n", meter);


printf("Length in Kilometer = %.2f km", km);

return 0;
}
//C program to calculate diameter, circumference and area of circle

#include <stdio.h>
int main()
{
float radius, diameter, circumference, area;

// Input radius of circle from user


printf("Enter radius of circle: ");
scanf("%f", &radius);

// Calculate diameter, circumference and area


diameter = 2 * radius;
circumference = 2 * 3.14 * radius;
area = 3.14 * (radius * radius);

// Print all results


printf("Diameter of circle = %.2f units \n", diameter);
printf("Circumference of circle = %.2f units \n", circumference);
printf("Area of circle = %.2f sq. units ", area);

return 0;
}
/**
* C program to find square root of a number
*/

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

int main()
{
double num, root;

/* Input a number from user */


printf("Enter any number to find square root: ");
scanf("%lf", &num);

/* Calculate square root of num */


root = sqrt(num);

/* Print the resultant value */


printf("Square root of %.2lf = %.2lf", num, root);

return 0;
}
/**
* C program to perform all arithmetic operations
*/

#include <stdio.h>

int main()
{
int num1, num2;
int sum, sub, mult, mod;
float div;

/*
* Input two numbers from user
*/
printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);

/*
* Perform all arithmetic operations
*/
sum = num1 + num2;
sub = num1 - num2;
mult = num1 * num2;
div = (float)num1 / num2;
mod = num1 % num2;

/*
* Print result of all arithmetic operations
*/
printf("SUM = %d\n", sum);
printf("DIFFERENCE = %d\n", sub);
printf("PRODUCT = %d\n", mult);
printf("QUOTIENT = %f\n", div);
printf("MODULUS = %d", mod);

return 0;

You might also like