Day6_Assignment
Day6_Assignment
/*
program to print the below pattern
*
* *
* * *
* * * *
* * * * *
*/
#include <stdio.h>
int main() {
int i = 0, j;
return 0;
}
2.
/*
program to print the below pattern
*
* *
* * *
* * * *
* * * * *
*/
#include <stdio.h>
int main() {
int i = 1, j;
return 0;
}
3.
// program to create multiplication table from 1 to 10
#include <stdio.h>
int main() {
int i = 1, j;
return 0;
}
4.
//WAP to reverse a number using FOR loop
#include<stdio.h>
int main()
{
int num, rev=0, rem = 0;
printf("Enter the number to be reversed: ");
scanf("%d", &num);
return 0;
}
5.
//WAP to print Fibonacci series up to a user input number
#include<stdio.h>
int main()
{
int n, term1 = 0, term2 = 1, term3;
printf("Enter the number upto which series must be print: ");
scanf("%d", &n);
return 0;
}
6.
//Program to print Pascal's triangle up to 8th row
#include <stdio.h>
int binomial_coefficient(int n, int k) {
int res = 1;
if (k > n - k) {
k = n - k;
}
for (int i = 0; i < k; i++) {
res = res * (n - i) / (i + 1);
}
return res;
}
int main() {
int rows, i, j, space;
printf("\n");
}
return 0;
}
7.
/*Create a "Guess the number" program
Requirements:
1. Generate a random number between 0 and 20
2. Prompt the user to enter their guess (number must be between 0 and 20)
3. Indicate the user's guess is too high or too low
4. Player wins if they guess within 5 tries
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int guess, attempts = 5;
srand(time(0));
int random_number = rand() % 21; // Generates a random number between 1 and
20
attempts--;
if (attempts == 0) {
printf("\nSorry, you've run out of attempts. My number
was %d.\nBetter luck next time.\n\n", random_number);
}
}
return 0;
}
8.
/*Filter even numbers with continue
Description: Write a c program that prompts user to enter a series of integers up
to 20. The program should calculate and display the sum of all even numbers
entered while skipping any negative numbers. Use the continue statement for this.
*/
#include <stdio.h>
int main() {
int n, sum = 0, array[20];
if (n > 20) {
printf("Error: Number of integers should be less than or equal to
20.\n");
return 1;
}
if (array[i] % 2 == 0) {
printf("%d ", array[i]);
sum += array[i];
}
}
printf("\nThe sum of even numbers from the entered integers is: %d\n", sum);
return 0;
}
9.
/*Problem Statement: Banking System Simulation
Description: Create a simple banking system simulation that allows user to create
an
account, deposit money, withdraw money, and check their balance. The program
should provide a menu-driven interface.
Requirements:
1. Use appropriate data types for account balance (e.g., float for monetary
values)
and user input (e.g., int for account numbers).
3. Use control statements to navigate through the menu options:
i. Create Account
ii. Deposit Money
iii. Withdraw Money
iv. Check Balance
4. Ensure that the withdrawal does not exceed the available balance and handle
invalid inputs gracefully.
Example Input/Output:
Welcome to the Banking System
1. Create Account
2. Deposit Money
3. Withdraw Money
4. Check Balance
5. Exit
Choose an option: 1
Enter account holder name: John Doe
Account created successfully! Account Number: 1001
Choose an option: 2
Enter account number: 1001
Enter amount to deposit: 500
Deposit successful! New Balance: 500.0
Choose an option: 3
Enter account number: 1001
Enter amount to withdraw: 200
Withdrawal successful! New Balance: 300.0
Choose an option: 4
Enter account number: 1001
Current Balance: 300.0
Choose an option: 5
Exiting the system.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int generate_account_number() {
return (rand() % 9000) + 1000;
}
int main() {
char account_holder[50];
int choice, account_number = 0, entered_account_number;
float balance = 0.0, withdraw, deposit;
srand(time(0));
while (1) {
printf("\nChoose an option: ");
scanf("%d", &choice);
switch (choice) {
case 1:
getchar();
printf("Enter account holder name: ");
fgets(account_holder, sizeof(account_holder), stdin);
account_number = generate_account_number();
balance = 0.0;
printf("Account created successfully! Account Number: %d\n",
account_number);
break;
case 2:
printf("Enter account number: ");
scanf("%d", &entered_account_number);
if (entered_account_number != account_number) {
printf("Invalid account number!\n");
break;
}
printf("Enter amount to deposit: ");
scanf("%f", &deposit);
if (deposit <= 0) {
printf("Invalid deposit amount. Please enter a positive
number.\n");
} else {
balance += deposit;
printf("Deposit successful! New Balance: %.2f\n", balance);
}
break;
case 3:
printf("Enter account number: ");
scanf("%d", &entered_account_number);
if (entered_account_number != account_number) {
printf("Invalid account number!\n");
break;
}
printf("Enter amount to withdraw: ");
scanf("%f", &withdraw);
if (withdraw > balance) {
printf("Insufficient balance!\n");
} else if (withdraw <= 0) {
printf("Invalid withdrawal amount. Please enter a positive
number.\n");
} else {
balance -= withdraw;
printf("Withdrawal successful! New Balance: %.2f\n",
balance);
}
break;
case 4:
printf("Enter account number: ");
scanf("%d", &entered_account_number);
if (entered_account_number != account_number) {
printf("Invalid account number!\n");
} else {
printf("Current Balance: %.2f\n", balance);
}
break;
case 5:
printf("Exiting the system.\n");
exit(0);
default:
printf("Invalid option! Please choose a number between 1 and
5.\n");
break;
}
}
return 0;
}
10.
/*Problem Statement: Weather Data Analysis
Description: Write a program that collects daily temperature data for a month and
analyzes it to find the average temperature, the highest temperature, the lowest
temperature, and how many days were above average.
Requirements:
1. Use appropriate data types (float for temperatures and int for days).
2. Store temperature data in an array.
3. Use control statements to calculate:
i. Average Temperature of the month.
ii. Highest Temperature recorded.
iii. Lowest Temperature recorded.
iv. Count of days with temperatures above average.
4. Handle cases where no data is entered.
Example Input/Output:
Enter temperatures for each day of the month (30 days):
Day 1 temperature: 72.5
Day 2 temperature: 68.0
...
Day 30 temperature: 75.0
#include <stdio.h>
int main()
{
float temperatures[30], total_temperature = 0.0, highest_temperature,
lowest_temperature, average_temperature;
int days_above_avg = 0;
total_temperature += temperatures[i];
}
return 0;
}
PS C:\Users\betti\Desktop\Training\Day6> ./task10
Day 1 temperature: 15
Day 2 temperature: 17
Day 3 temperature: 10
Day 4 temperature: 0
Day 5 temperature: -4
Day 7 temperature: -6
Day 8 temperature: 1
Day 9 temperature: 8
Day 10 temperature: 12
Day 11 temperature: 16
Day 12 temperature: 18
Day 13 temperature: 20
Day 14 temperature: -=
Day 15 temperature: 22
Day 16 temperature: 14
Day 17 temperature: 27
Day 18 temperature: 24
Day 19 temperature: 22
Day 20 temperature: 21
Day 21 temperature: 27
Day 22 temperature: 28
Day 23 temperature: 22
Day 24 temperature: 21
Day 25 temperature: 27
Day 26 temperature: 22
Day 27 temperature: 24
Day 28 temperature: 28
Day 29 temperature: 22
Day 30 temperature: 26
11.
Requirements:
1. Use appropriate data types for product details (e.g., char arrays for product
names, int for quantities, float for prices).
2. Use control statements for menu-driven operations:
i. Add Product
ii. Update Product Quantity
iii. Delete Product
iv. View All Products in Inventory
3. Ensure that the program handles invalid inputs and displays appropriate error
messages.
Example Input/Output:
Choose an option: 1
Enter product name: Widget A
Enter product quantity: 50
Enter product price: 19.99
Choose an option: 4
Product Name: Widget A, Quantity: 50, Price: $19.99
Choose an option: 5
Exiting the system.*/
#include <stdio.h>
#include <string.h>
int main(){
int choice, quantity;
float price;
char productName[20], userEnteredProductName[20];
while(1){
printf("\nChoose an option: ");
scanf("%d", &choice);
switch(choice){
case 1:
getchar();
printf("Enter product name: ");
fgets(productName, sizeof(productName), stdin);
productName[strcspn(productName, "\n")] = '\0';
case 2:
getchar();
printf("Enter product name to update quantity: ");
fgets(userEnteredProductName, sizeof(userEnteredProductName),
stdin);
userEnteredProductName[strcspn(userEnteredProductName, "\n")] =
'\0';
if (strcmp(productName, userEnteredProductName) != 0) {
printf("Product not available. Please enter a valid product
name.\n");
break;
}
case 3:
getchar();
printf("Enter product name to delete: ");
fgets(userEnteredProductName, sizeof(userEnteredProductName),
stdin);
userEnteredProductName[strcspn(userEnteredProductName, "\n")] =
'\0';
if (strcmp(productName, userEnteredProductName) != 0) {
printf("Product not available. Please enter a valid product
name.\n");
break;
}
productName[0] = '\0';
quantity = 0;
price = 0.0;
printf("Product deleted successfully.\n");
break;
case 4:
if (strlen(productName) == 0) {
printf("No product in inventory.\n");
} else {
printf("Product Name: %s, Quantity: %d, Price: $%.2f\n",
productName, quantity, price);
}
break;
case 5:
printf("Exiting the system.\n");
return 0;
default:
printf("Invalid option. Please choose a number between 1 and
5.\n");
break;
}
}
return 0;
}