0% found this document useful (0 votes)
6 views

Assignment 2

The document contains multiple C programs that demonstrate basic programming concepts. It includes examples for calculating the area and perimeter of a circle, converting Celsius to Fahrenheit, and swapping two numbers using both a third variable and without using a third variable. Each program is accompanied by user prompts and sample outputs.

Uploaded by

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

Assignment 2

The document contains multiple C programs that demonstrate basic programming concepts. It includes examples for calculating the area and perimeter of a circle, converting Celsius to Fahrenheit, and swapping two numbers using both a third variable and without using a third variable. Each program is accompanied by user prompts and sample outputs.

Uploaded by

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

Q1. write a program in c to calculate the area of a 7 unit radius circle.

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

// Prompt the user to enter the radius of the circle


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

// Calculate the area of the circle


area = 3.14159 * radius * radius;

// Print the area of the circle


printf("The area of the circle is %.2f\n", area);

return 0;
}

Output:
Enter the radius of the circle: 7
The area of the circle is 153.94

Q2. write a program in c to calculate the perimeter of a circle.

#include <stdio.h>

#define PI 3.14f /* Define the value of pie */

void main() {
/* Variable Declaration. */
float radius, perimeter, area;

/* Taking input of the radious of the circle from the user */


printf("Enter radius of the Circle:\n");
scanf("%f", & radius);

/* Calculating perimeter of the circle */


perimeter = 2 * PI * radius;
printf("Perimeter of the circle: %0.4f\n", perimeter);

/* Calculating area of the circle */


area = PI * radius * radius;
printf("Area of circle: %0.4f\n", area);
}

Q3. write a program in c to convert temperature in Celsius to Fahrenheit by taking value entered by
user.

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

Output:
Enter temperature in Celsius: 34
34.00 Celsius = 93.20 Fahrenheit

Q4. Write a program in C to swap two numbers using a Third variable.

#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);

// value of first is assigned to temp


temp = first;

// value of second is assigned to first


first = second;

// value of temp (initial value of first) is assigned to second


second = temp;

// %.2lf displays number up to 2 decimal points


printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
return 0;
}

Output:
Enter first number: 11
Enter second number: 12

After swapping, first number = 12.00


After swapping, second number = 11.00

Q5. Write a program in C to swap two numbers using without a Third variable.

#include<stdio.h>
int main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}
Output:
Before swap a=10 b=20
After swap a=20 b=10
------------------------------------------------------OR-----------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a*b;//a=200 (10*20)
b=a/b;//b=10 (200/20)
a=a/b;//a=20 (200/10)
system("cls");
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}

Output:
Before swap a=10 b=20
After swap a=20 b=10

You might also like