0% found this document useful (0 votes)
9 views5 pages

Lab 3

The document contains a series of C programming examples demonstrating basic programming concepts. It includes programs for printing messages, performing arithmetic operations, converting time and temperature, calculating distances, evaluating areas, and swapping variable values. Each example is accompanied by code snippets and expected outputs.

Uploaded by

khushisahu.0613
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)
9 views5 pages

Lab 3

The document contains a series of C programming examples demonstrating basic programming concepts. It includes programs for printing messages, performing arithmetic operations, converting time and temperature, calculating distances, evaluating areas, and swapping variable values. Each example is accompanied by code snippets and expected outputs.

Uploaded by

khushisahu.0613
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/ 5

1.

Program to Print the Message “Hello” on the Screen

#include <stdio.h>
int main()
{
/* printf function displays the content that is
* passed between the double quotes.
*/
printf("Hello World");
return 0;
}
Output:

Hello World

2. Write a program to take an input of two integer numbers and print the sum of
that numbers.

#include <stdio.h>
int main()
{
int num1, num2, sum;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);

sum = num1 + num2;


printf("Sum of the entered numbers: %d", sum);
return 0;
}
Output:

Enter first number: 20


Enter second number: 19
Sum of the entered numbers: 39

3. Convert the time in seconds to hours, minutes and seconds. (1 hr =3600 sec).

#include <stdio.h>
int main() {
int sec, h, m, s;
printf("Input seconds: ");
scanf("%d", &sec);

h = (sec/3600);

m = (sec -(3600*h))/60;

s = (sec -(3600*h)-(m*60));

printf("H:M:S - %d:%d:%d\n",h,m,s);

return 0;
}

4. Find the sum of the digits of a four-digit number (ex 1234 sum=10) (without using a
loop).

#include <stdio.h>
int main()
{
int n,f,x,s,y,t,l,sum;
printf("Enter 4-Digit Number: ");
scanf("%d",&n);
f=n/1000;
x=n%1000;
s=x/100;
y=x%100;
t=y/10;
l=y%10;
printf("\nFirst Digit = %d \nSecond Digit = %d \nThird Digit = %d\nLast
Digit = %d\n",f,s,t,l);
sum=f+s+t+l;
printf("\nSum of All 4-Digits : %d",sum);
return 0;
}

5. Convert temperature given in Fahrenheit to Centigrade and Centigrade to Fahrenheit.


Hint: C=5/9(F-32)).

#include<stdio.h> // include stdio.h

int main()
{
float fah, cel;

printf("Enter a temp in fah: ");


scanf("%f", &fah);

cel = (5.0/9) * (fah - 32);

printf("%.2f°F is same as %.2f°C", fah, cel);

return 0;
}

6. Converting distance in mm to cm, inch, feet (1 cm =10mm, 1inch=2.5cm, 1 feet =12


inches).

/* C Program to convert input distance in meter, feet,


inches, centimeter */
#include <stdio.h>
#include <conio.h>

int main() {
int distance;
float meter, feet, inches, centimeter;

printf("Enter the distance [in Kilometers]: ");


scanf("%d", & distance);

meter = distance * 1000;


feet = distance * 3280.84;
inches = distance * 39370.1;
centimeter = distance * 100000;

printf("Meter = %f\n", meter);


printf("Feet = %f\n", feet);
printf("Inches = %f\n", inches);
printf("Centimeters = %f\n", centimeter);

getch();
}

7. Find out the distance between two points e.g. (x1, y1) and (x2, y2).
Hint: Distance=√(x2-x1)2+ (y2-y1)2

1. #include<stdio.h>
2. #include<math.h>
3.
4. int main()
5. {
6. float x1, y1, x2, y2, distance;
7.
8. printf("Enter point 1 (x1, y1)\n");
9. scanf("%f%f", &x1, &y1);
10.
11. printf("Enter point 2 (x2, y2)\n");
12. scanf("%f%f", &x2, &y2);
13.
14. distance = sqrt( (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) )
;
15.
16. printf("Distance between (%0.2f, %0.2f) and (%0.2f, %0.2f) i
s %0.2f\n", x1, y1, x2, y2, distance);
17.
18. return 0;
19.}

8. Evaluate the area of the circle Area = Pi * R 2

#include<stdio.h>
int main() {
float r, Area, Perimeter;
printf(“Enter the radius of circle”);
scanf(“%f”,&r);
Area = 3.14*r*r;
Perimeter=2*3.14*r;
printf(“the area of the circle is %f\n”,Area);
printf(“the perimeter of the circle is %f\n”,Perimeter);
return 0;
}

9. Interchange values of two variables using a third variable.

// C program to swap two variables


#include <stdio.h>

int main()
{
int x, y;
printf("Enter Value of x ");
scanf("%d", &x);
printf("\nEnter Value of y ");
scanf("%d", &y);

int temp = x;
x = y;
y = temp;

printf("\nAfter Swapping: x = %d, y = %d", x, y);


return 0;
}

10. Interchange values of two variables without using 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;
}

You might also like