Week 2 & Week 3 Lab Programs
Week 2 & Week 3 Lab Programs
Week 2 & Week 3 Lab Programs
#include <stdio.h>
int main()
{
float celsius, fahrenheit;
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;
}
int main()
{
float fahrenheit, celsius;
return 0;
}
2. Write a C program to convert a given integer (in
seconds) to hours, minutes and seconds.
#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;
}
Input seconds: 25300
H:M:S - 7:1:40
3.Write a simple program that prints the results of all the operators
available in C (including pre/post increment, bitwise and/or/not, etc.). Read
required operand values from standard input.
#include <stdio.h>
int main() {
int a, b;
// Arithmetic Operators
printf("Arithmetic Operators:\n");
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b); // Use %% to print the % character
// Relational Operators
printf("\nRelational Operators:\n");
printf("a == b = %d\n", a == b);
printf("a != b = %d\n", a != b);
printf("a > b = %d\n", a > b);
printf("a < b = %d\n", a < b);
printf("a >= b = %d\n", a >= b);
printf("a <= b = %d\n", a <= b);
// Logical Operators
printf("\nLogical Operators:\n");
printf("a && b = %d\n", a && b);
printf("a || b = %d\n", a || b);
printf("!a = %d\n", !a);
printf("!b = %d\n", !b);
// Bitwise Operators
printf("\nBitwise Operators:\n");
printf("a & b = %d\n", a & b);
printf("a | b = %d\n", a | b);
printf("a ^ b = %d\n", a ^ b);
printf("~a = %d\n", ~a);
printf("~b = %d\n", ~b);
// Shift Operators
printf("\nShift Operators:\n");
printf("a << 1 = %d\n", a << 1); // Left shift by 1
printf("a >> 1 = %d\n", a >> 1); // Right shift by 1
return 0;
}
int main() {
char ch;
return 0;
}