c lab prorams with output
c lab prorams with output
#include <stdio.h>
void fibonacci(int n) {
int a = 0, b = 1, c;
printf("Fibonacci Series: ");
printf("%d %d ", a, b);
for(int i = 2; i < n; i++) {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
fibonacci(n);
return 0;
}
Output:
#include <stdio.h>
int gcd(int a, int b) {
if(b == 0)
return a;
return gcd(b, a % b);
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("GCD: %d", gcd(a, b));
return 0;
}
Output:
Output:
#include <stdio.h>
int main() {
int choice;
float num1, num2, result;
printf("Menu:\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
printf("Enter your choice: ");
scanf("%d", &choice);
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch(choice) {
case 1: result = num1 + num2; break;
case 2: result = num1 - num2; break;
case 3: result = num1 * num2; break;
case 4:
if(num2 != 0)
result = num1 / num2;
else
printf("Error! Division by zero.");
return 0;
break;
default:
printf("Invalid choice");
return 0;
}
printf("Result: %.2f", result);
return 0;
}
Output:
markdown
Menu:
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice: 1
Enter two numbers: 5 3
Result: 8.00
Output:
Menu:
1. Area of Circle
2. Area of Square
3. Area of Sphere
Enter your choice: 2
Enter side of square: 4
Area of Square: 16.00
#include <stdio.h>
int main() {
int num, reversed = 0, remainder, original;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
if (original == reversed)
printf("Palindrome");
else
printf("Not Palindrome");
return 0;
}
Output:
Output:
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
for(int i = 1; i < num; i++) {
if(num % i == 0)
sum += i;
}
if(sum == num)
printf("Perfect number");
else
printf("Not a perfect number");
return 0;
}
Output:
Enter a number: 6
Perfect number
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for(i = 1; i <= rows; i++) {
for(j = 1; j <= rows - i; j++) {
printf(" ");
}
for(j = 1; j <= (2 * i - 1); j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Output:
markdown
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Sum of array elements: %d", sumOfArray(arr, size));
return 0;
}
Output:
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
Output:
Enter a number: 5
Factorial of 5 is 120
int main() {
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int result[2][2];
addMatrices(a, b, result);
Output:
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
struct Employee employees[2];
printf("\nEmployee Details:\n");
for(int i = 0; i < 2; i++) {
printf("Employee %d: ID=%d, Name=%s, Salary=%.2f\n", i + 1,
employees[i].id, employees[i].name, employees[i].salary);
}
return 0;
}
Output:
Employee Details:
Employee 1: ID=101, Name=John Doe, Salary=50000.00
Employee 2: ID=102, Name=Jane Smith, Salary=55000.00
9. Create a Structure for Student and Pass it to a Functio
#include <stdio.h>
struct Student {
char name[50];
int roll;
};
int main() {
struct Student s;
printf("Enter student name: ");
scanf(" %[^\n]", s.name);
printf("Enter roll number: ");
scanf("%d", &s.roll);
displayStudent(s);
return 0;
}
Output:
#include <stdio.h>
int main() {
int i = 10;
float f = 3.14;
char c = 'A';
return 0;
}
Output:
Address of integer i: 0x7ffccf91b5a4
Address of float f: 0x7ffccf91b5a8
Address of char c: 0x7ffccf91b5b0
#include <stdio.h>
int main() {
int x = 5, y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
Output:
Before swap: x = 5, y = 10
After swap: x = 10, y = 5
#include <stdio.h>
int main() {
int length = 5, width = 3, area, perimeter;
calculateAreaAndPerimeter(&length, &width, &area, &perimeter);
printf("Area: %d, Perimeter: %d\n", area, perimeter);
return 0;
}
Output:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
Output:
Array elements: 1 2 3 4 5
#include <stdio.h>
int main() {
int marks[3][3], total[3] = {0}, grandTotal = 0;
Output:
12. Display ID, Name, and Percentage of a Student Using Structure and Function
Passing by Value
#include <stdio.h>
struct Student {
int id;
char name[50];
float marks[3];
float percentage;
};
int main() {
struct Student s = {101, "John Doe", {75.5, 82.5, 90.0}};
displayStudent(s);
return 0;
}
Output:
int main() {
char str1[100], str2[100];
return 0;
}
Output: