0% found this document useful (0 votes)
2 views4 pages

Prgrms For Unit-I

The document contains multiple C programs that perform various tasks such as calculating the area and perimeter of a rectangle, checking for leap years, determining the day of the week, generating multiplication tables, summing even numbers, calculating factorials, counting positive, negative, and zero numbers, and generating Fibonacci sequences. Each program includes user input and outputs the results accordingly. The programs demonstrate basic programming concepts such as loops, conditionals, and arithmetic operations.

Uploaded by

priya.a
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)
2 views4 pages

Prgrms For Unit-I

The document contains multiple C programs that perform various tasks such as calculating the area and perimeter of a rectangle, checking for leap years, determining the day of the week, generating multiplication tables, summing even numbers, calculating factorials, counting positive, negative, and zero numbers, and generating Fibonacci sequences. Each program includes user input and outputs the results accordingly. The programs demonstrate basic programming concepts such as loops, conditionals, and arithmetic operations.

Uploaded by

priya.a
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/ 4

Program: Area and Perimeter of a Rectangle

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

// Input length and width of the rectangle


printf("Enter the length of the rectangle: ");
scanf("%f", &length);

printf("Enter the width of the rectangle: ");


scanf("%f", &width);

// Calculate area and perimeter


area = length * width;
perimeter = 2 * (length + width);

// Output the results


printf("Area of the rectangle: %.2f\n", area);
printf("Perimeter of the rectangle: %.2f\n", perimeter);

return 0;
}

Program: Check Leap Year


#include <stdio.h>
int main() {
int year;

// Input year from the user


printf("Enter a year: ");
scanf("%d", &year);

// Check if the year is a leap year


if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
} else {
printf("%d is a leap year.\n", year);
}
} else {
printf("%d is not a leap year.\n", year);
}

return 0;
}
Program: Day of the Week
#include <stdio.h>
int main() {
int day;

// Input the day number from the user


printf("Enter a number (1-7) to represent the day of the week: ");
scanf("%d", &day);

// Use switch case to determine the day


switch(day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid input! Please enter a number between 1 and 7.\n");
}

return 0;
}

Program: Multiplication Table


#include <stdio.h>
int main() {
int num, i;

// Input number from the user


printf("Enter a number to print its multiplication table: ");
scanf("%d", &num);
// Print the multiplication table using a for loop
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}

return 0;
}

Program: Sum of Even Numbers


#include <stdio.h>
int main() {
int sum = 0, i = 1;

// Calculate sum of even numbers between 1 and 100 using a while loop
while (i <= 100) {
if (i % 2 == 0) {
sum += i;
}
i++;
}

// Output the result


printf("Sum of all even numbers between 1 and 100 is: %d\n", sum);

return 0;
}

Program: Factorial Calculation


#include <stdio.h>
int main() {
int num, factorial = 1, i = 1;

// Input number from the user


printf("Enter a number to calculate its factorial: ");
scanf("%d", &num);

// Calculate factorial using a do-while loop


do {
factorial *= i;
i++;
} while (i <= num);

// Output the result


printf("Factorial of %d is: %d\n", num, factorial);

return 0;
}
Program: Count Positive, Negative, and Zero Numbers
#include <stdio.h>
int main() {
int num, positiveCount = 0, negativeCount = 0, zeroCount = 0;

// Loop to take numbers from the user until -1 is entered


while (1) {
printf("Enter a number (-1 to stop): ");
scanf("%d", &num);

if (num == -1)
break;

if (num > 0)
positiveCount++;
else if (num < 0)
negativeCount++;
else
zeroCount++;
}

// Output the counts


printf("Positive numbers: %d\n", positiveCount);
printf("Negative numbers: %d\n", negativeCount);
printf("Zero numbers: %d\n", zeroCount);

return 0;
}

Program: Fibonacci Sequence


#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm;

// Input the number of terms from the user


printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Sequence: ");

// Loop to generate Fibonacci sequence


for (int i = 1; i <= n; i++) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}

You might also like