Exercise Programs (unit 1INTRODUCTION TO C PROGRAMMING)
Check whether the required amount can be withdrawn based on the available amount
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
// Structure declaration
struct acc_type
{
char bank_name[20];
char bank_branch[20];
char acc_holder_name[30];
int acc_number;
char acc_holder_address[100];
float available_balance;
};
struct acc_type account[20];
/*
printf("The above structure can be declared using
typedef like below");
typedef struct acc_type
{
char bank_name[20];
char bank_branch[20];
char acc_holder_name[30];
int acc_number;
char acc_holder_address[100];
float available_balance;
}Acc_detail;
Acc_detail account[20];
*/
int num_acc;
void Create_new_account();
void Cash_Deposit();
void Cash_withdrawl();
void Account_information();
void Log_out();
void display_options();
/* main program */
int main()
{
char option;
char f2f[50] = "http://fresh2refresh.com/";
num_acc=0;
while(1)
{
printf("\n***** Welcome to Bank Application *****\n");
printf("\nThis demo program is brought you by %s",f2f);
display_options();
printf("Please enter any options (1/2/3/4/5/6) ");
printf("to continue : ");
option = getch();
printf("%c \n", option);
switch(option)
{
case '1': Create_new_account();
break;
case '2': Cash_Deposit();
break;
case '3': Cash_withdrawl();
break;
case '4': Account_information();
break;
case '5': return 0;
case '6': system("cls");
break;
default : system("cls");
printf("Please enter one of the options");
printf("(1/2/3/4/5/6) to continue \n ");
break;}
}
return 0;
}
/*Function to display available options in this application*/
void display_options()
{
printf("\n1. Create new account \n");
printf("2. Cash Deposit \n");
printf("3. Cash withdrawl \n");
printf("4. Account information \n");
printf("5. Log out \n");
printf("6. Clear the screen and display available ");
printf("options \n\n");
}
/* Function to create new account */
void Create_new_account()
{
char bank_name[20];
char bank_branch[20];
char acc_holder_name[30];
int acc_number;
char acc_holder_address[100];
float available_balance = 0;
fflush(stdin);
printf("\nEnter the bank name : ");
scanf("%s", &bank_name);
printf("\nEnter the bank branch : ");
scanf("%s", &bank_branch);
printf("\nEnter the account holder name : ");
scanf("%s", &acc_holder_name);
printf("\nEnter the account number(1 to 10): ");
scanf("%d", &acc_number);
printf("\nEnter the account holder address : ");
scanf("%s", &acc_holder_address);
strcpy(account[acc_number-1].bank_name,bank_name);
strcpy(account[acc_number-1].bank_branch,bank_branch);
strcpy(account[acc_number-1].acc_holder_name,
acc_holder_name);
account[acc_number-1].acc_number=acc_number;
strcpy(account[acc_number-1].acc_holder_address,
acc_holder_address);
account[acc_number-1].available_balance=available_balance;
printf("\nAccount has been created successfully \n\n");
printf("Bank name : %s \n" ,
account[acc_number-1].bank_name);
printf("Bank branch : %s \n" ,
account[acc_number-1].bank_branch);
printf("Account holder name : %s \n" ,
account[acc_number-1].acc_holder_name);
printf("Account number : %d \n" ,
account[acc_number-1].acc_number);
printf("Account holder address : %s \n" ,
account[acc_number-1].acc_holder_address);
printf("Available balance : %f \n" ,
account[acc_number-1].available_balance);
//num_acc++;
}
// Displaying account informations
void Account_information()
{
register int num_acc = 0;
//if (!strcmp(customer,account[count].name))
while(strlen(account[num_acc].bank_name)>0)
{
printf("\nBank name : %s \n" ,
account[num_acc].bank_name);
printf("Bank branch : %s \n" ,
account[num_acc].bank_branch);
printf("Account holder name : %s \n" ,
account[num_acc].acc_holder_name);
printf("Account number : %d \n" ,
account[num_acc].acc_number);
printf("Account holder address : %s \n" ,
account[num_acc].acc_holder_address);
printf("Available balance : %f \n\n" ,
account[num_acc].available_balance);
num_acc++;
}
}
// Function to deposit amount in an account
void Cash_Deposit()
{
auto int acc_no;
float add_money;
printf("Enter account number you want to deposit money:");
scanf("%d",&acc_no);
printf("\nThe current balance for account %d is %f \n",
acc_no, account[acc_no-1].available_balance);
printf("\nEnter money you want to deposit : ");
scanf("%f",&add_money);
while (acc_no=account[acc_no-1].acc_number)
{
account[acc_no-1].available_balance=
account[acc_no-1].available_balance+add_money;
printf("\nThe New balance for account %d is %f \n",
acc_no, account[acc_no-1].available_balance);
break;
}acc_no++;
}
// Function to withdraw amount from an account
void Cash_withdrawl()
{
auto int acc_no;
float withdraw_money;
printf("Enter account number you want to withdraw money:");
scanf("%d",&acc_no);
printf("\nThe current balance for account %d is %f \n",
acc_no, account[acc_no-1].available_balance);
printf("\nEnter money you want to withdraw from account ");
scanf("%f",&withdraw_money);
while (acc_no=account[acc_no-1].acc_number)
{
account[acc_no-1].available_balance=
account[acc_no-1].available_balance-withdraw_money;
printf("\nThe New balance for account %d is %f \n",
acc_no, account[acc_no-1].available_balance);
break;
}acc_no++;
}
Menu-driven program to find the area of different shapes
#include <stdio.h>
void main()
int fig_code;
float side, base, length, breadth, height, area, radius;
printf("-------------------------\n");
printf(" 1 --> Circle\n");
printf(" 2 --> Rectangle\n");
printf(" 3 --> Triangle\n");
printf(" 4 --> Square\n");
printf("-------------------------\n");
printf("Enter the Figure code\n");
scanf("%d", &fig_code);
switch(fig_code)
case 1:
printf("Enter the radius\n");
scanf("%f", &radius);
area = 3.142 * radius * radius;
printf("Area of a circle = %f\n", area);
break;
case 2:
printf("Enter the breadth and length\n");
scanf("%f %f", &breadth, &length);
area = breadth * length;
printf("Area of a Reactangle = %f\n", area);
break;
case 3:
printf("Enter the base and height\n");
scanf("%f %f", &base, &height);
area = 0.5 * base * height;
printf("Area of a Triangle = %f\n", area);
break;
case 4:
printf("Enter the side\n");
scanf("%f", &side);
area = side * side;
printf("Area of a Square=%f\n", area);
break;
default:
printf("Error in figure code\n");
break;
}
}
-------------------------
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
-------------------------
Enter the Figure code
1
Enter the radius
30
Area of a circle = 2827.800049
-------------------------
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
-------------------------
Enter the Figure code
2
Enter the breadth and length
20 30
Area of a Reactangle = 600.000000
-------------------------
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
-------------------------
Enter the Figure code
3
Enter the base and height
45 80
Area of a Triangle = 1800.000000
-------------------------
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
-------------------------
Enter the Figure code
4
Enter the side
100
Area of a Square=1000
Find the sum of even numbers
#include<stdio.h>
int main()
{
int start, end, sum = 0;
printf("Enter start and end value\n");
scanf("%d%d", &start, &end);
printf("\nSum of even no's from %d to %d is ", start, end);
while(start <= end)
{ if(start % 2 == 0)
{
sum = sum + start;
} start++; } printf("%d\n", sum); return 0; }
Output 1:
Enter start and end value
10
20
Sum of even no’s from 10 to 20 is 90
Output 2:
Enter start and end value
25
50
Sum of even no’s from 25 to 50 is 494