Week 2 & Week 3 Lab Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Week 2

1.Write a C program to display "Welcome to IFHE" on the


screen.
#include <stdio.h>
int main() {
// Print the welcome message to the screen
printf("Welcome to IFHE\n");
return 0;
}

2.Write a C program to display Your Name, Address and City in


different lines.
#include <stdio.h>
int main() {
// Print the name, address, and city on separate lines
printf("Your Name\n");
printf("Your Address\n");
printf("Your City\n");
return 0;
}
3. Write a C program to find the area of a circle. (Hints : Area=
PI * r2)
#include <stdio.h>
//#define M_PI 3.14
#include <math.h> // For the M_PI constant
int main() {
double radius, area;
// Ask the user to enter the radius of the circle
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
// Calculate the area of the circle
area = M_PI * radius * radius;
// Display the result
printf("The area of the circle with radius %.2f is %.2f\n", radius, area);
return 0;
}
4. Write a C program to enter marks of five subjects and
calculate total, average and percentage of marks.
#include <stdio.h>
int main() {
// Declare variables
float marks[5];
float total = 0.0;
float average, percentage;
int i;
// Input marks for 5 subjects
printf("Enter marks for 5 subjects:\n");
for (i = 0; i < 5; i++) {
printf("Subject %d: ", i + 1);
scanf("%f", &marks[i]);
total += marks[i];
}
// Calculate average and percentage
average = total / 5.0;
percentage = (total / 500.0) * 100.0; // Assuming each subject is out of 100
// Display the results
printf("\nTotal marks: %.2f\n", total);
printf("Average marks: %.2f\n", average);
printf("Percentage: %.2f%%\n", percentage);
return 0;
}

5. Write a C program to convert days into years, weeks and days.


#include <stdio.h>
int main() {
// Declare variables
int days, years, weeks, remainingDays;

// Input number of days


printf("Enter the number of days: ");
scanf("%d", &days);
// Calculate years, weeks, and days
years = days / 365; // Assuming a year has 365 days
remainingDays = days % 365;
weeks = remainingDays / 7;
remainingDays = remainingDays % 7;
// Display the results
printf("%d days is equivalent to:\n", days);
printf("%d years\n", years);
printf("%d weeks\n", weeks);
printf("%d days\n", remainingDays);
return 0;
}
Week -3
1. Write a C program to convert centigrade into Fahrenheit
and vice versa. Formula: C= (F-32)/1.8.
We can use the following formulas to do so :
C = (f-32)/1.8.
f = 1.8 * C + 32.

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

write a c program to convert fahrenheit into celcies


#include<stdio.h>

int main()
{
float fahrenheit, celsius;

//get the limit of fibonacci series


printf("Enter Fahrenheit: \n");
scanf("%f",&fahrenheit);

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

printf("Celsius: %f \n", 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;

// Read two integers from standard input


printf("Enter two integers (a and b): ");
scanf("%d %d", &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

// Increment and Decrement Operators


printf("\nIncrement/Decrement Operators:\n");
printf("++a = %d\n", ++a); // Pre-increment
printf("a++ = %d\n", a++); // Post-increment
printf("--a = %d\n", --a); // Pre-decrement
printf("a-- = %d\n", a--); // Post-decrement

// Ensure variables are reset to their original values


a += 1; // Reset a to its original value after post-increment/decrement

return 0;
}

4.Write a C program to find out whether the character pressed


through the Keyboard is a digit or not (Use conditional operator
only).
#include <stdio.h>

int main() {
char ch;

// Read a character from the user


printf("Enter a character: ");
scanf(" %c", &ch);

// Check if the character is a digit


(ch >= '0' && ch <= '9') ? printf("The character '%c' is a digit.\n", ch)
: printf("The character '%c' is not a digit.\n", ch);

return 0;
}

5. Write a C program to swap variable values of i and j using


following Techniques.
a) using temporary variable
b) without using temporary variable

a) using temporary variable


#include <stdio.h>
int main()
{
int var1, var2, temp;
printf("Enter two integersn");
scanf("%d%d", &var1, &var2);
printf("Before SwappingnFirst variable = %dnSecond variable = %dn", var1,
var2);
temp = var1;
var1 = var2;
var2 = temp;
printf("After SwappingnFirst variable = %dnSecond variable = %dn", var1,
var2);
return 0;
}
b) without using temporary variable
#include <stdio.h>
int main()
{
int var1, var2, temp;
printf("Enter two integersn");
scanf("%d%d", &var1, &var2);
printf("Before SwappingnFirst variable = %dnSecond variable = %dn", var1,
var2);
var1 = var1 + var2;
var2 = var1 - var2;
var1 = var1 - var2;
printf("After SwappingnFirst variable = %dnSecond variable = %dn", var1,
var2);
return 0;
}

You might also like