C Programming Lab Exercises and Solutions
C Programming Lab Exercises and Solutions
The pre-increment operator (++variable) increases the variable's value before it is used in an expression, while the post-increment operator (variable++) uses the variable's current value in the expression and then increments it. For example, if a variable x is 5, then using ++x in an expression will make it 6 before use, whereas x++ will use 5 and then update x to 6 afterward .
To find the greatest of three numbers a, b, and c using if-else statements, compare each number: First, check if a is greater than or equal to both b and c. If true, a is the greatest. If not, check if b is greater than or equal to both a and c. If true, b is the greatest. Otherwise, c must be the greatest. This approach uses logical comparison to evaluate the largest number incrementally .
To calculate the factorial of a number using a while loop, initialize the result variable to 1 and a counter variable to the number itself. Then, loop while the counter is greater than 0, multiplying the result by the counter and decrementing the counter each iteration. This multiplication continues until the counter reduces to zero, leaving the result variable containing the factorial of the original number .
To calculate the geometric sum of a series programmatically, define the first term, the common ratio, and the number of terms. Utilize the formula: sum = a(1-r^n)/(1-r) when r ≠ 1, where 'a' is the first term and 'r' the common ratio. Consider edge cases like r=1 where the sum simplifies to a*n (n terms of 'a'). Implement checks for invalid input or where r approaches 1 to ensure precision and avoid division errors .
Using a switch case structure in a calculator program allows for clear organization of distinct operations such as addition, subtraction, multiplication, and division. Each operation is a case, making it easy to navigate and expand. The switch case improves readability and maintainability compared to multiple if-else statements, particularly when there are many operations, by separating logic into discrete sections .
To generate a Fibonacci sequence up to n terms iteratively, initialize two variables with the first two numbers of the sequence (e.g., 0 and 1). Loop from 2 to n, maintaining a third variable to hold the sum of the previous two. After printing each term, shift the two main variables forward: the current second number becomes the first, and the sum becomes the second, continuing this pattern until n terms are generated .
Nested if statements in a prime number checking program serve to validate multiple conditions in an orderly manner. The outer if statement usually checks initial criteria, such as whether the number is less than 2, while inner if statements iteratively check divisibility up to the half of the number. If any number divides the candidate number without a remainder, it’s not prime. The nesting allows compartmentalization of checks, ensuring efficiency and clarity in logical flow .
To convert a lowercase character to its uppercase equivalent, use the ASCII value of the character. Subtract 32 from the ASCII value of the lowercase character (since lowercase 'a' is 97 in ASCII and uppercase 'A' is 65, differing by 32) and convert this back to a character type. This approach is straightforward and efficiently leverages the fixed distance in the ASCII table between lowercase and uppercase letters .
To determine if a year is a leap year using if-else statements, check if the year is divisible by 4. If it is, then further check if it is not divisible by 100 unless it is also divisible by 400. These conditions ensure that years like 2000 are leap years, while years like 1900 are not .
Swapping two numbers using functions by reference allows the original variables to be modified directly in the calling function, maintaining changes outside the scope of the called function. This avoids the need for additional memory to return values and allows operations on the original data, making the swap more efficient and applicable in places where direct access to original variables is necessary .