11.10 Lab4
introduction-to-computer-science-laboratory-4.pdf
Introduction to Computer Science
Laboratory 4
November 10, 2024
-
Write the expression result and how it affects the variables involved.
- (a)
( a++ > 3 && 4 == 4 ) with ( a = 3 )
This condition is false, because first do the determination of
a > 3
which is false. Then Leta = a + 1
, thena = 4
* (b)( ++a > 3 && 4 == 4 ) with ( a = 3 )
This condition is true, because first Let
a = a + 1
, thena = 4
, then do the determination ofa > 3
which is true. Also4 == 4
is true. * (c)( a++ || j >= 0 ) with ( a = 0 ) and ( j = 0 )
Only
a = a + 1
, thena = a + 1
, but the program doesn't determine whetherj >= 0
. Since the previous condition has been already true. * (d)( j >= 0 || a++ ) with ( a = 0 ) and ( j = 0 )
Only
j >= 0
, then this condition will be true, but the program doesn't runa++
. Since the previous condition has been already true. * (e)( a++ && --j >= 0 ) with ( a = 0 ) and ( j = 1 )
First do the determination of
0
which is false sincea = 0
(thena = a + 1
). Thenj
will not change. * (f)( 1 > 0 || 1/0 )
Only determine
1 > 0
is true * (g)( 1/0 || 1 > 0 )
Error, doesn't run. * (h)
( (a = 7) > 0 ) with ( a = -3 )
Always true * (i)
( (a = 7) > 0 && (b = 44) > 44 ) with ( a = -3 ) and ( b = 150 )
Always false * (j)
( (a = 7) > 0 || (b = 44) > 44 ) with ( a = -3 ) and ( b = 150 )
Always true * (k)
( 7 - (a = (b + 1)) == 1 ) with ( a = 3 ) and ( b = 5 )
Always true
- (a)
-
Write the output of the following two C programs.\
#include <stdio.h> int main() { int i = 0; int j = 5; j = j++; while (i < j) { i++; printf("%i \n", i); } return 0; } /* outputs: 1 2 3 4 5*/
#include <stdio.h> int main() { int i = 0; int j = 5; j = ++j; while (i < j) { i++; printf("%i \n", i); } return 0; } /* outputs: 1 2 3 4 5 6*/
Note that the only difference is
j = j++;
andj = ++j;
For the first one, first let
j = j
, sincej = 5
, then5
will be signed toj
, after that,j
will plus one, but this operation has no effect on the assignment ofj = 5
-
Solve all the exercises present in the 4th tutorial.
-
Write a C program that asks the user for an integer ( n ) and tells if ( n ) is a perfect number or not. A number is perfect if it is equal to the sum of its divisors, excluding itself. (e.g., 6 is perfect because \( 6 = 1 + 2 + 3 \)).
#include <stdio.h> int main() { int number, divisor, sum = 0; printf("Please enter a number, then I will tell you whether it is a perfect number:"); scanf("%d", &number); for(divisor = 1; divisor < number; divisor++) { if (number % divisor == 0) { sum = sum + divisor; } } if (sum == number) printf("Yes! %d is a perfect number!", number); else printf("No! %d is not a perfect number!", number); return 0; }
-
Write a C program that asks for two integers ( a ) and ( b ) and then prints the Least Common Multiple (LCM) between ( a ) and ( b ). (e.g., with \( a = 4 \) and \( b = 6 \), the output should be 12).
#include <stdio.h> int main() { int a, b, LCM; printf("Please enter two numbers, then I will tell you the LCM of them:"); scanf("%d %d", &a, &b); for(LCM = 1; LCM <= a*b; LCM++) { if ((LCM % a == 0) && (LCM % b == 0)) { printf("The LCM of %d and %d is %d!", a, b, LCM); break; } } return 0; }
-
Write a C program that asks for two integers ( x ) and ( y ) and then prints the result of ( x^y ).
#include <stdio.h> int main() { int x, y, result = 1; printf("Please enter x and y, then I will tell you x^y:"); scanf("%d %d", &x, &y); for(int times = 1; times <= y; times++) { result = result * x; } printf("%d^%d=%d", x, y, result); return 0; }
-
Write a C program that, given an integer ( n ), prints out all the perfect numbers less than or equal to ( n ).
#include <stdio.h> int main() { int n, number, divisor, sum = 0; printf("Please enter n, then I will tell you all the perfect numbers less than or equal to n:"); scanf("%d", &n); for (number = 1; number <= n; number++) { sum = 0; for (divisor = 1; divisor < number; divisor++) { if (number % divisor == 0) sum = sum + divisor; } if (sum == number) printf("%d ", number); } return 0; }
-
Write a C program that asks the user for an integer ( n ) and tells if the number is automorphic. A number is automorphic if its square ends in the same digit. For example, 25 is automorphic because both 25 and 625 (\( 25^2 \)) end in 5.
#include <stdio.h> int main() { int n, square; printf("Please enter n, then I will tell you if the number is automorphic:"); scanf("%d", &n); square = n * n; if ((square % 10) == (n % 10)) printf("Yes, it is automorphic"); else printf("No, it is not automorphic"); return 0; }