[go: up one dir, main page]

0% found this document useful (0 votes)
7 views13 pages

c lab prorams with output

The document contains multiple C programming examples, including printing Fibonacci series, finding GCD and LCM, implementing a calculator, calculating areas, checking for palindromes, finding prime numbers, and more. Each example includes code snippets along with sample outputs for user inputs. The programs cover various concepts such as functions, structures, pointers, and arrays.

Uploaded by

Soumya Das
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)
7 views13 pages

c lab prorams with output

The document contains multiple C programming examples, including printing Fibonacci series, finding GCD and LCM, implementing a calculator, calculating areas, checking for palindromes, finding prime numbers, and more. Each example includes code snippets along with sample outputs for user inputs. The programs cover various concepts such as functions, structures, pointers, and arrays.

Uploaded by

Soumya Das
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/ 13

1.

Print Fibonacci Series

#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:

Enter the number of terms: 6


Fibonacci Series: 0 1 1 2 3 5

2. Find GCD of Two Numbers

#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:

Enter two numbers: 56 98


GCD: 14

3. Find LCM of Two Numbers


#include <stdio.h>
int gcd(int a, int b) {
if(b == 0)
return a;
return gcd(b, a % b);
}
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("LCM: %d", lcm(a, b));
return 0;
}

Output:

Enter two numbers: 12 18


LCM: 36

4. Menu-driven Program for Calculator Using Switch Case

#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

5. Menu-driven Program for Calculating Area


#include <stdio.h>
#define PI 3.14159
int main() {
int choice;
float radius, side, area;
printf("Menu:\n");
printf("1. Area of Circle\n2. Area of Square\n3. Area of Sphere\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter radius of circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("Area of Circle: %.2f\n", area);
break;
case 2:
printf("Enter side of square: ");
scanf("%f", &side);
area = side * side;
printf("Area of Square: %.2f\n", area);
break;
case 3:
printf("Enter radius of sphere: ");
scanf("%f", &radius);
area = 4 * PI * radius * radius;
printf("Surface Area of Sphere: %.2f\n", area);
break;
default:
printf("Invalid choice\n");
}
return 0;
}

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:

Enter a number: 121


Palindrome

7. Find Prime Numbers Between Two Intervals


#include <stdio.h>
int main() {
int low, high, i, j, isPrime;
printf("Enter two numbers: ");
scanf("%d %d", &low, &high);
printf("Prime numbers between %d and %d are: ", low, high);
for(i = low; i <= high; i++) {
if(i < 2) continue;
isPrime = 1;
for(j = 2; j <= i/2; j++) {
if(i % j == 0) {
isPrime = 0;
break;
}
}
if(isPrime)
printf("%d ", i);
}
return 0;
}

Output:

Enter two numbers: 10 30


Prime numbers between 10 and 30 are: 11 13 17 19 23 29

8. Check Whether a Number is Perfect

#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

9. Create Pyramid Using Loops

#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

Enter number of rows: 5


*
***
*****
*******
*********

8. 1. Calculate Sum of Elements of 1D Array Using Function


#include <stdio.h>

int sumOfArray(int arr[], int size) {


int sum = 0;
for(int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}

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:

Sum of array elements: 15

2. Find Factorial of a Number Using Function

#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

3. Add Two 2D Arrays Using Function


#include <stdio.h>

void addMatrices(int a[2][2], int b[2][2], int result[2][2]) {


for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}
}

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);

printf("Sum of two matrices:\n");


for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}

Output:

Sum of two matrices:


6 8
10 12

4. Print and Display Records of Employee Details Using Array of Structure


#include <stdio.h>

struct Employee {
int id;
char name[50];
float salary;
};

int main() {
struct Employee employees[2];

for(int i = 0; i < 2; i++) {


printf("Enter details for employee %d\n", i + 1);
printf("ID: ");
scanf("%d", &employees[i].id);
printf("Name: ");
scanf(" %[^\n]", employees[i].name);
printf("Salary: ");
scanf("%f", &employees[i].salary);
}

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:

Enter details for employee 1


ID: 101
Name: John Doe
Salary: 50000
Enter details for employee 2
ID: 102
Name: Jane Smith
Salary: 55000

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

void displayStudent(struct Student s) {


printf("Student Name: %s\n", s.name);
printf("Roll Number: %d\n", s.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:

Enter student name: John


Enter roll number: 101
Student Name: John
Roll Number: 101

10. Pointers Programs

1. Access Addresses of Different Types of Variables Using Pointer

#include <stdio.h>

int main() {
int i = 10;
float f = 3.14;
char c = 'A';

printf("Address of integer i: %p\n", &i);


printf("Address of float f: %p\n", &f);
printf("Address of char c: %p\n", &c);

return 0;
}

Output:
Address of integer i: 0x7ffccf91b5a4
Address of float f: 0x7ffccf91b5a8
Address of char c: 0x7ffccf91b5b0

2. Swap Two Integers Using Pointers

#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

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

3. Compute Area and Perimeter of Rectangle Using Pointers

#include <stdio.h>

void calculateAreaAndPerimeter(int *length, int *width, int *area, int


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

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:

Area: 15, Perimeter: 16

4. Store Values of Array and Display It Using Pointers

#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;

printf("Array elements: ");


for(int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i));
}
return 0;
}

Output:

Array elements: 1 2 3 4 5

11. WAP to Display Marks of 3 Subjects for 3 Students

#include <stdio.h>

int main() {
int marks[3][3], total[3] = {0}, grandTotal = 0;

for(int i = 0; i < 3; i++) {


printf("Enter marks for student %d:\n", i + 1);
for(int j = 0; j < 3; j++) {
printf("Subject %d: ", j + 1);
scanf("%d", &marks[i][j]);
total[i] += marks[i][j];
}
}

printf("\nTotal marks for each student:\n");


for(int i = 0; i < 3; i++) {
printf("Student %d: %d\n", i + 1, total[i]);
grandTotal += total[i];
}

printf("Grand Total of all students: %d", grandTotal);


return 0;
}

Output:

Enter marks for student 1:


Subject 1: 75
Subject 2: 80
Subject 3: 90
Enter marks for student 2:
Subject 1: 60
Subject 2: 70
Subject 3: 80
Enter marks for student 3:
Subject 1: 85
Subject 2: 90
Subject 3: 95

Total marks for each student:


Student 1: 245
Student 2: 210
Student 3: 270
Grand Total of all students: 725

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

void displayStudent(struct Student s) {


s.percentage = (s.marks[0] + s.marks[1] + s.marks[2]) / 3.0;
printf("Student ID: %d\n", s.id);
printf("Name: %s\n", s.name);
printf("Percentage: %.2f%%\n", s.percentage);
}

int main() {
struct Student s = {101, "John Doe", {75.5, 82.5, 90.0}};
displayStudent(s);
return 0;
}

Output:

Student ID: 101


Name: John Doe
Percentage: 82.67%

13. Read String from Terminal Using scanf() and gets()


#include <stdio.h>

int main() {
char str1[100], str2[100];

printf("Enter a string using scanf: ");


scanf("%s", str1);
printf("You entered: %s\n", str1);
// Using gets() to read a string with spaces
printf("Enter a string using gets: ");
getchar(); // To clear the newline character from buffer
gets(str2);
printf("You entered: %s\n", str2);

return 0;
}

Output:

Enter a string using scanf: Hello


You entered: Hello
Enter a string using gets: This is a test.
You entered: This is a test.

You might also like