Lab 3
Lab 3
#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);
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;
}
int main()
{
float fah, cel;
return 0;
}
int main() {
int distance;
float meter, feet, inches, 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.}
#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;
}
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;
#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;
}