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

lab 7

Uploaded by

quadanddualcore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views13 pages

lab 7

Uploaded by

quadanddualcore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 13

1) Lab Task

#include <stdio.h>

void fibonacci(int x) {
int a = 0, b = 1, next;

if (x <= 0) {
printf("Number of terms must be greater than 0.\n");
return;
}

printf("Fibonacci series (%d terms):\n", x);

for (int i = 1; i <= x; i++) {


printf("%d ", a);
next = a + b;
a = b;
b = next;
}
printf("\n");
}

int main() {
int terms;

printf("Enter the number of terms for the Fibonacci series: ");


scanf("%d", &terms);
fibonacci(terms);

return 0;
}
2) Lab Task
#include <stdio.h>

void calculate_avg(int v, int w, int x, int y, int z, int a) {


float average = (v + w + x + y + z + a) / 6.0;
printf("The average of the numbers is: %.2f\n", average);
}

int main() {
int v, w, x, y, z, a;

printf("Enter six integers:\n");


scanf("%d %d %d %d %d %d", &v, &w, &x, &y, &z, &a);

calculate_avg(v, w, x, y, z, a);

return 0;
}
3) Lab Task
#include <stdio.h>

int prod_of_digits(int x) {
int product = 1, digit;

if (x < 0) {
x = -x;
}

while (x > 0) {
digit = x % 10;
product *= digit;
x /= 10;
}

return product;
}
int main() {
int number, product;

printf("Enter an integer: ");


scanf("%d", &number);

product = prod_of_digits(number);

printf("The product of the digits of %d is: %d\n", number, product);

return 0;
}

4) Lab Task

#include <stdio.h>

int palindrome(int x) {
int original = x;
int reversed = 0, digit;

if (x < 0) {
return 0;
}

while (x > 0) {
digit = x % 10;
reversed = reversed * 10 + digit;
x /= 10;
}

return (original == reversed) ? 1 : 0;


}

int main() {
int number, result;

printf("Enter an integer: ");


scanf("%d", &number);

result = palindrome(number);
if (result == 1) {
printf("Entered integer was Palindrome.\n");
} else {
printf("Entered integer was NOT Palindrome.\n");
}

return 0;
}
5) Lab Task

#include <stdio.h>

void initArray(int arr[], int size) {


printf("Enter %d integers:\n", size);
for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
}

int main() {
int size;

printf("Enter the size of the array: ");


scanf("%d", &size);

int array[size];
initArray(array, size);

printf("Initialized array:\n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");

return 0;
}

6) Lab Task
#include <stdio.h>

void initArray(int arr[], int size) {


printf("Enter %d integers:\n", size);
for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
}
int sumArray(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}

int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);

int array[size];
initArray(array, size);

int total = sumArray(array, size);

printf("The sum of the elements in the array is: %d\n", total);

return 0;
}
7) Lab Task

#include <stdio.h>

void calculateAverage(float *num1, float *num2, float *num3) {


float avg = (*num1 + *num2 + *num3) / 3;
printf("The average of the three numbers is: %.2f\n", avg);
}

int main() {
float num1, num2, num3;

printf("Enter the first number: ");


scanf("%f", &num1);
printf("Enter the second number: ");
scanf("%f", &num2);
printf("Enter the third number: ");
scanf("%f", &num3);

calculateAverage(&num1, &num2, &num3);

return 0;
}
8) Lab Task

#include <stdio.h>

void swap(int *x, int *y) {


*x = *x + *y;
*y = *x - *y;
*x = *x - *y;
}

int main() {
int a, b;

printf("Enter the first integer: ");


scanf("%d", &a);
printf("Enter the second integer: ");
scanf("%d", &b);

printf("\nBefore swapping:\n");
printf("a = %d, b = %d\n", a, b);

swap(&a, &b);
printf("\nAfter swapping:\n");
printf("a = %d, b = %d\n", a, b);

return 0;
}

9) Lab Task
#include <stdio.h>
#include <stdlib.h>

void ATM_menu() {
int choice;

while(1) {

printf("\n--- ATM Menu ---\n");


printf("Press 1 – Balance Inquiry\n");
printf("Press 2 – Cash Withdrawal\n");
printf("Press 3 – Cash Deposit\n");
printf("Press 4 – Transfer Funds\n");
printf("Press 5 – Change Password\n");
printf("Press 6 – Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:

printf("\nYou are on Balance Inquiry Page.\n");


printf("Press any key to return to the main menu...\n");
getchar();
getchar();
break;

case 2:

printf("\nYou are on Cash Withdrawal Page.\n");


printf("Press any key to return to the main menu...\n");
getchar();
getchar();
break;

case 3:

printf("\nYou are on Cash Deposit Page.\n");


printf("Press any key to return to the main menu...\n");
getchar();
getchar();
break;

case 4:

printf("\nYou are on Transfer Funds Page.\n");


printf("Press any key to return to the main menu...\n");
getchar();
getchar();
break;

case 5:

printf("\nYou are on Change Password Page.\n");


printf("Press any key to return to the main menu...\n");
getchar();
getchar();
break;

case 6:

printf("\nExiting the ATM system. Goodbye!\n");


exit(0);

default:
printf("\nInvalid choice! Please enter a valid option (1-6).\n");
}
}
}

int main() {

ATM_menu();
return 0;
}

You might also like