



















Preview text:
lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING 1) #include> int main(){ int num ; int i = 1;
int poss=0,nega=0; for(;i<=12;i++){
printf("Enter integer %d: ",i);
scanf("%d",&num); if(num>0){ poss++; } else{ nega++; } }
printf("Number of possitive : %d\n",poss);
printf("Number of negaive : %d\n",nega); } 2) #include int main(){ int num,a,b,c,d; printf("Enter integer a: "); lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING scanf("%d",&a); printf("Enter integer b: "); scanf("%d",&b);
if (a>b){ printf("As a is greater than b so that we cannot calculate the sum"); }
else{ for(;anumber is : %d\n",a); c =a+c ; }
printf("the sum is : %d\n",c); } 3)
#include int main() { char c; int i = 0; for
(;1;i++) { printf("Enter a character: "); scanf("
%c", &c); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ) { if (i == 0) {
printf("No character was entered.\n"); } else { lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING
printf("Number of inputs: %d\n", i); } break; } } } 4) #include int main() { char x, y; do { printf("Enter a grade: "); scanf(" %c", &x); if (x == 'A') { printf("Excellent!\n"); } else if (x == 'B' ) { printf("Good!\n"); } else if (x == 'C' ) { printf("Fair!\n"); } else if (x == 'D') { lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING printf("Average!\n"); } else if (x == 'F' ) { printf("Weak!\n"); } else { printf("Invalid.\n"); }
printf("Do you want to continue? (y/n):
"); scanf(" %c", &y); } while (y == 'y' ); printf("Exit program...\n"); } Exercise 1
Write a C program that reads an option (value can be 1, 2 or 3). If the option value is 1, 2
or 3, circulate the program again; otherwise, exit.
If option value is 1 : input the octal number.
If option value is 2 : input the decimal number.
If option value is 3 : input the hexadecimal number.
Then, print this number in Octal, Decimal, Hexadecimal. Output: Option: 1 Enter an octal: 12 Octal Decimal Hexadecimal
---------------------------------- 012 10 0xa Option: 2 Enter a decimal: 5 Octal Decimal Hexadecimal
---------------------------------- 05 5 0x5 Option: 3 Enter a hexadecimal: 14 Octal Decimal Hexadecimal
---------------------------------- 024 20 0x14
Option: 4 Exit program... #include void converter(){ lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING int option ; int number; printf("Option: "); scanf("%d",&optio n ); switch (option){ case 1: printf("Enter an octal : "); scanf("%o",&numb er); printf("Octal Decimal Hexadecimal \n"); printf("----------------- -----------------\n"); printf("%#o %d %#x\n\ n",number,number, number); converter(); break; case 2: printf("Enter a decimal : "); scanf("%d",&numb lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING er); printf("Octal Decimal Hexadecimal\n"); printf("----------------- -----------------\n"); printf("%#o %d %#x\n\ n",number,number, number); converter(); break; case 3: printf("Enter a hexadecimal : "); scanf("%x",&numb er); printf("Octal Decimal Hexadecimal\n"); printf("----------------- -----------------\n"); printf("%#o %d %#x \n\ n",number,number, number); converter(); break; lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING default: printf("Exit.."); break; } } int main(){ printf("Nguyen Duc Huy EEEACIU22205\n"); converter(); return 0; } Exercise 2
Write a C program that reads an option (value can be 1, 2 or 3). If the option value is 1, 2
or 3, circulate the program again; otherwise, exit.
If option value is 1 : enter three float numbers, than find min of them.
If option value is 2 : Enter an integer (n) and a character (c), then print a square pattern
(nxn) with a character c input. lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING
If option value is 3 : Enter an integer, a float and a double, Then, calculate the average of them. Output: Option: 1
Enter three floats: 3.1 3.5 4.2 Min: 3.1 Option: 2
Enter an integer and a character: 3 a aaa aaa aaa Option: 3
Enter an integer, a float and a double: 3 2.1 4.5 Average: 3.2 Option: 4 No function was called.
--------------------------------
Process exited after 32.67 seconds with return value 4 Press any key to continue . . .
#include int main() { int v,n,min,i,z; char c; float a,b,d,avg,Min; printf("Nguyen Duc Huy EEEACIU22205\n"); while(1){ printf("Option:"); scanf("%d",&v); if(v==1){
printf("Enter three floats: "); scanf("%f",&a); scanf("%f",&b); scanf("%f",&d); Min=a; if (Min>b){ Min=b; } else if (Min>d){ Min=d; } printf("Min: %f\n", Min); } else if (v==2){
printf("Enter an integer and a
character:"); scanf("%d",&n); scanf("
%c",&c); for (i=0;ifor (z=0;z } printf("\n"); } lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING else if (v==3){
printf("Enter an integer, a float and a double:
"); scanf("%d",&n); scanf("%f",&a); scanf("%f",&b); avg=(a+b+n)/3; printf("Average: %f\n",avg); } else {break;} } } Exercise 3
Write a C program that performs ATM transaction. The ATM option (value can be 1, 2 or
3). ). If the option value is 1, 2 or 3, circulate the program again; otherwise, exit. 1) Balance checking 2) Cash withdrawal 3) Cash deposition.
The default PIN =1234 and the initial amount =$2000. Output: lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING
ENTER YOUR SECRET PIN NUMBER:1234
********Welcome to ATM Service************** 1. Check Balance lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING 2. Withdraw Cash 3. Deposit Cash
******************?**************************?* Enter your choice: 1 YOUR BALANCE IN Rs : 2000
DO U WISH TO HAVE ANOTHER TRANSCATION?(y/n):y
********Welcome to ATM Service************** 1. Check Balance 2. Withdraw Cash 3. Deposit Cash
******************?**************************?* Enter your choice: 2
ENTER THE AMOUNT TO WITHDRAW: 200 PLEASE COLLECT CASH YOUR CURRENT BALANCE IS 1800 DO U WISH TO HAVE ANOTHER TRANSCATION?(y/n):
********Welcome to ATM Service************** 1. Check Balance 2. Withdraw Cash 3. Deposit Cash
******************?**************************?* Enter your choice: 3
ENTER THE AMOUNT TO DEPOSIT 5000 YOUR BALANCE IS 6800 DO U WISH TO HAVE ANOTHER TRANSCATION?(y/n):
********Welcome to ATM Service************** 1. Check Balance 2. Withdraw Cash 3. Deposit Cash
******************?**************************?* Enter your choice: 1 YOUR BALANCE IN Rs : 6800 DO U WISH TO HAVE ANOTHER TRANSCATION?(y/n):
********Welcome to ATM Service************** 1. Check Balance lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING 2. Withdraw Cash 3. Deposit Cash lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING
******************?**************************?* Enter your choice: 4 Invalid option
DO U WISH TO HAVE ANOTHER TRANSCATION?(y/n):n THANKS FOR USING OUT ATM SERVICE. #include int main() {
printf("Nguyen Duc Huy EEEACIU22205\n");
printf("******Welcome to ATM Service******\n"); int v,p,w; int amount=2000; while(1){ printf("Option:"); scanf("%d",&v); if(v==1){
printf("Balance checking...\n");
printf("Money: $%d\n",amount); } else if (v==2){ printf("Cash withdrawal...\n"); printf("Enter your Pin: ");
scanf("%d",&p); if (p==22205){
printf("Enter your cash you want to withdrawal: "); scanf("%d",&w); if (amount>w){ amount=amount-w;
printf("Your balnace is :$%d\n",amount); } else {
printf("Your balance is not enough to withdraw "); } } else { printf("Wrong Pin\n"); } } else if (v==3){
printf("Cash deposition....\n"); printf("Enter your Pin: "); scanf("%d",&p); if (p==1234){
printf("Enter your cash you want to deposit: "); lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING scanf("%d",&w); lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING amount=amount+w;
printf("Your balance is :%d\n",amount); } else { printf("Wrong Pin\n"); } } else {break; } } }
V. SUBMISSION GUIDELINE Exercise 1
Write a program to add, subtract, multiply and divide two number using user defined type
function with return type. At the end, the program should ask the user to continue or not.
If yes, continues, otherwise, prints “Exit program...”. Output:
Chose the option + (add), - (subtract), * (multiply), / (divide): + Enter number 1: 16 Enter number 2: -7 The result is: 9
Do you want to continue? (y/n):n Exit program... #include lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING
int add(int a, int b) { return a + b; } int
subtract(int a, int b) { return a - b; } int
multiply(int a, int b) { return a * b; } float
divide(int a, int b) { return (float)a / b; } int main() { char opt, next; int n1, n2; do {
printf("Choose the option + (add), - (subtract), * (multiply), / (divide): "); scanf(" %c", &opt); printf("Enter number 1: "); scanf("%d", &n1); printf("Enter number 2: "); scanf("%d", &n2);
if(opt == '+'){ printf("The result is: %d\n", add(n1, n2)); } if(opt == '-'){
printf("The result is: %d\n", subtract(n1, n2));
} if(opt == '*'){ printf("The result is: %d\n", multiply(n1, n2));
} if(opt == '/'){ printf("The result is: %.2f\n", divide(n1, n2)); } else{ printf("Please try again.\n"); }
printf("Do you want to continue? (y/n):
"); scanf(" %c", &next); } while(next ==
'y' ); printf("Exit program...\n"); } lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING Exercise 2
Write a program in C read integer n and print natural numbers from 1 to n using recursion Output: Enter n: 21
The natural numbers are : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include void printNumbers(int n) { if(n > 0) { printNumbers(n - 1); printf("%d ", n); } } int main() { int n;
printf("Enter n: "); scanf("%d",
&n); printf("The natural numbers are: "); printNumbers(n); printf("\n"); } lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING Exercise 3
Write a C program that reads an option (unsigned value) to call the two functions below:
Option 1: Write a C function that accepts two float values x and y in degree. The function
will calculate and print the result (3 decimal-point format, i.e. 1.234) from the following
equation on the screen: f(x,y) = 2*sin(2x)*cos3(y) - 3*cos(2x)*sin2(y);
Option 2: Using a recursive function that accepts an integer n and returns the result of the factorial : n!
Option 3: Using a recursive function that accepts an integer n and returns the result of the
following equation: f(n) = 2! + 3! + ... + (n+1)!
The program should repeat when option value is 1, 2 or 3, exit while option is greater than 3. Output: Enter an option: 1
Enter two angles (in degree): 57.3 136.7
f(57.3,136.7) = -0.114 Enter an option: 2 Enter an integer: 6 The factorial is :720 Enter an option: 3 Enter an integer: 5 The result f(5) = 872 Enter an option: 10 Exit program... #include lOMoAR cPSD| 59078336
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING #include
double radian(double degree) {
return degree * (M_PI / 180.0); }
double calculateFunction(double x, double y) { x = radian(x); y = radian(y);
return (2 * sin(2 * x) * cos(y)* cos(y) * cos(y)) - (3 *
cos(2 * x) * sin(y) * sin(y)); } long int fact(int n) { if (n <= 1) return 1; return n * fact(n - 1); } long int sumfact(int n) { if (n <= 1) return fact(2);
return fact(n + 1) + sumfact(n - 1); } int main() { int option; do { printf("Enter an option: "); scanf("%d", &option); if (option == 1) { double x, y;
printf("Enter two angles (in degrees): ");
scanf("%lf %lf", &x, &y);
double result = calculateFunction(x, y);
printf("f(%.1f, %.1f) = %.3f\n", x, y, result); } else if (option == 2) { int n;
printf("Enter an integer: "); scanf("%d", &n);
printf("The factorial is: %ld\n", fact(n)); } else if (option == 3) { int n;
printf("Enter an integer: "); scanf("%d", &n);
printf("The result f(%d) = %ld\n", n, sumfact(n)); } else { printf("Exit program...\n"); }
} while (option >= 1 && option <= 3); }