Docsity

CSC103 Programming Fundamentals Assignment-02 Solutions - Prof. Afsar, Assignments of C programming

Partial preview of the text

Download CSC103 Programming Fundamentals Assignment-02 Solutions - Prof. Afsar and more Assignments C programming in PDF only on Docsity!

CSC103 Programming Fundamentals

Assignment-

Student Name Sara Ajmal

Registration Number FA23-BEE-

Class/Section EE-1E

Instructor’s Name Khan Afsar

Assignment-02:

Submission deadline: 27th Oct, 2023

Chapter-03 exercise questions:

Question # 3: What output would you expect from the following program? #include <stdio.h> int main (void) { printf ("Testing..."); printf ("....1"); printf ("...2"); printf ("..3"); printf ("\n"); return 0; } Answer: The output for this program will be; Testing.......1...2.. Question # 4: Write a program that subtracts the value 15 from 87 and displays the result, together with an appropriate message, at the terminal. Answer: This will be the program; #include <stdio.h>

sum = 25 + 37 - 19; /* DISPLAY RESULTS */ printf("The answer is %i\n", sum); return 0; } Question # 6: What output might you expect from the following program? #include <stdio.h> int main (void){ int answer, result; answer = 100; result = answer - 10; printf ("The result is %i\n", result + 5); return 0; } Answer: Result will be calculated as answer - 10, which will be 100 -10. It will be 90. And then result + 5 will be 90 + 5 = 95. So the exact output will be; The result is 95 Chapter-04 exercise questions:

Question # 4: Write a program that converts 27° from degrees Fahrenheit (F) to degrees Celsius (C) using the following formula: C = (F - 32) / 1.8. Answer: #include <stdio.h> int main() double fahrenheit = 27.0; // Convert Fahrenheit to Celsius double celsius = (fahrenheit - 32) / 1.8; printf("%.2lf degrees Fahrenheit is equal to %.2lf degrees Celsius.\n", fahrenheit, celsius); return 0; } Question # 5: What output would you expect from the following program? #include <stdio.h> int main (void){ char c, d; c = 'd'; d = c; printf ("d = %c\n", d); return 0; } Answer: The output for the program will be;

result = numerator / denominator; printf("Result in exponential format: %e\n", result); return 0; } Question # 8: To round off an integer i to the next largest even multiple of another integer j, the following formula can be used: Next_multiple = i + j - i % j For example, to round off 256 days to the next largest number of days evenly divisible by a week, values of i = 256 and j = 7 can be substituted into the preceding formula as follows: Next_multiple = 256 + 7 - 256 % 7 = 256 + 7 - 4 = 259 Write a program to find the next largest even multiple for the following values of i and j: i j 365 7 12,258 23 996 4 Answer: The program for this will be; #include <stdio.h> int main() { int i, j; // Case 1 i = 365;

j = 7; int next_multiple_1 = i + j - (i % j); // Case 2 i = 12258; j = 23; int next_multiple_2 = i + j - (i % j); // Case 3 i = 996; j = 4; int next_multiple_3 = i + j - (i % j) printf("For i = 365 and j = 7, the next largest even multiple is: %d\n", next_multiple_1); printf("For i = 12258 and j = 23, the next largest even multiple is: %d\n", next_multiple_2); printf("For i = 996 and j = 4, the next largest even multiple is: %d\n", next_multiple_3); return 0; } Chapter-05 exercise questions: Question # 2: Write a program to generate and display a table of n and n^2 , for integer values of n ranging from 1 to 10. Be certain to print appropriate column headings. Answer: #include <stdio.h> int main() { printf("n\t\tn^2\n");

printf("%d! = %llu\n", n, factorial); } return 0; } Question # 11: Write a program that calculates the sum of the digits of an integer. For example, the sum of the digits of the number 2155 is 2 + 1 + 5 + 5 or 13.The program should accept any arbitrary integer typed in by the user. Answer: The program will be; #include <stdio.h> int main() { int number, originalNumber, remainder, sum = 0; printf("Enter an integer: "); scanf("%d", &number); originalNumber = number; // Calculate the sum of the digits while (number > 0) { remainder = number % 10; sum += remainder; number /= 10; } printf("The sum of the digits of %d is: %d\n", originalNumber, sum); return 0; }