OCS752 INTRODUCTION TO C PROGRAMMING
UNIT 1 EXERCISE PROGRAMS
1. Check whether the required amount can be withdrawn based on the available amount
#include <stdio.h>
unsigned long amount=1000, deposit, withdraw;
int choice, pin, k;
char transaction ='y';
void main()
{
while (pin != 1520)
{
printf("ENTER YOUR SECRET PIN NUMBER:");
scanf("%d", &pin);
if (pin != 1520)
printf("PLEASE ENTER VALID PASSWORDn");
}
do
{
printf("********Welcome to ATM Service**************n");
printf("1. Check Balancen");
printf("2. Withdraw Cashn");
printf("3. Deposit Cashn");
printf("4. Quitn");
printf("********************************************nn");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("n YOUR BALANCE IN Rs : %lu ", amount);
break;
case 2:
printf("n ENTER THE AMOUNT TO WITHDRAW: ");
scanf("%lu", &withdraw);
if (withdraw % 100 != 0)
{
printf("n PLEASE ENTER THE AMOUNT IN MULTIPLES OF 100");
}
else if (withdraw >(amount - 500))
{
printf("n INSUFFICENT BALANCE");
}
else
{
amount = amount - withdraw;
printf("nn PLEASE COLLECT CASH");
printf("n YOUR CURRENT BALANCE IS%lu", amount);
}
break;
case 3:
printf("n ENTER THE AMOUNT TO DEPOSIT");
scanf("%lu", &deposit);
amount = amount + deposit;
printf("YOUR BALANCE IS %lu", amount);
break;
case 4:
printf("n THANK U USING ATM");
break;
default:
printf("n INVALID CHOICE");
}
printf("nnn DO U WISH TO HAVE ANOTHER TRANSCATION?(y/n): n");
fflush(stdin);./*fflush() is typically used for output stream only. Its purpose is to clear
(or flush) the output buffer and move the buffered data to console*/
scanf("%c", &transaction);
if (transaction == 'n'|| transaction == 'N')
k = 1;
} while (!k);
printf("nn THANKS FOR USING OUT ATM SERVICE");
}
2. Menu-driven program to find the area of different shapes
#include <stdio.h>
void main ()
{
int choice,r,l,w,b,h;
float area;
printf("Input 1 for area of circlen");
printf("Input 2 for area of rectanglen");
printf("Input 3 for area of trianglen");
printf("Input your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Input radious of the circle : ");
scanf("%d",&r);
area=3.14*r*r;
break;
case 2:
printf("Input length and width of the rectangle : ");
scanf("%d%d",&l,&w);
area=l*w;
break;
case 3:
printf("Input the base and hight of the triangle :");
scanf("%d%d",&b,&h);
area=.5*b*h;
break;
}
printf("The area is : %fn",area);
}
Copy
Sample Output:
Input 1 for area of circle
Input 2 for area of rectangle
Input 3 for area of triangle
Input your choice : 1
Input radious of the circle : 5
The area is : 78.500000
3. Find the sum of even numbers
#include <stdio.h>
int main()
{
int i, n, sum=0;
/* Input upper limit from user */
printf("Enter upper limit: ");
scanf("%d", &n);
for(i=2; i<=n; i+=2)
{
/* Add current even number to sum */
sum += i;
}
printf("Sum of all even number between 1 to %d = %d", n, sum);
return 0;
}
Unit 1 ocs752 introduction to c programming

Unit 1 ocs752 introduction to c programming

  • 1.
    OCS752 INTRODUCTION TOC PROGRAMMING UNIT 1 EXERCISE PROGRAMS 1. Check whether the required amount can be withdrawn based on the available amount #include <stdio.h> unsigned long amount=1000, deposit, withdraw; int choice, pin, k; char transaction ='y'; void main() { while (pin != 1520) { printf("ENTER YOUR SECRET PIN NUMBER:"); scanf("%d", &pin); if (pin != 1520) printf("PLEASE ENTER VALID PASSWORDn"); } do { printf("********Welcome to ATM Service**************n"); printf("1. Check Balancen"); printf("2. Withdraw Cashn"); printf("3. Deposit Cashn"); printf("4. Quitn"); printf("********************************************nn"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("n YOUR BALANCE IN Rs : %lu ", amount); break; case 2: printf("n ENTER THE AMOUNT TO WITHDRAW: "); scanf("%lu", &withdraw); if (withdraw % 100 != 0) { printf("n PLEASE ENTER THE AMOUNT IN MULTIPLES OF 100"); } else if (withdraw >(amount - 500)) { printf("n INSUFFICENT BALANCE"); } else { amount = amount - withdraw; printf("nn PLEASE COLLECT CASH"); printf("n YOUR CURRENT BALANCE IS%lu", amount); } break;
  • 2.
    case 3: printf("n ENTERTHE AMOUNT TO DEPOSIT"); scanf("%lu", &deposit); amount = amount + deposit; printf("YOUR BALANCE IS %lu", amount); break; case 4: printf("n THANK U USING ATM"); break; default: printf("n INVALID CHOICE"); } printf("nnn DO U WISH TO HAVE ANOTHER TRANSCATION?(y/n): n"); fflush(stdin);./*fflush() is typically used for output stream only. Its purpose is to clear (or flush) the output buffer and move the buffered data to console*/ scanf("%c", &transaction); if (transaction == 'n'|| transaction == 'N') k = 1; } while (!k); printf("nn THANKS FOR USING OUT ATM SERVICE"); } 2. Menu-driven program to find the area of different shapes #include <stdio.h> void main () { int choice,r,l,w,b,h; float area; printf("Input 1 for area of circlen"); printf("Input 2 for area of rectanglen"); printf("Input 3 for area of trianglen"); printf("Input your choice : "); scanf("%d",&choice);
  • 3.
    switch(choice) { case 1: printf("Input radiousof the circle : "); scanf("%d",&r); area=3.14*r*r; break; case 2: printf("Input length and width of the rectangle : "); scanf("%d%d",&l,&w); area=l*w; break; case 3: printf("Input the base and hight of the triangle :"); scanf("%d%d",&b,&h); area=.5*b*h; break; } printf("The area is : %fn",area); } Copy Sample Output: Input 1 for area of circle Input 2 for area of rectangle Input 3 for area of triangle Input your choice : 1 Input radious of the circle : 5 The area is : 78.500000 3. Find the sum of even numbers #include <stdio.h> int main() { int i, n, sum=0; /* Input upper limit from user */ printf("Enter upper limit: "); scanf("%d", &n); for(i=2; i<=n; i+=2) { /* Add current even number to sum */ sum += i; } printf("Sum of all even number between 1 to %d = %d", n, sum); return 0; }