0% found this document useful (0 votes)
7 views1 page

C Programming Assignment Questions

This document outlines a C programming assignment for first-semester CSE students, including basic programming tasks. The assignment consists of ten questions that cover fundamental concepts such as arithmetic operations, control structures, and array manipulation. Students are required to write programs to perform various operations, including finding sums, checking even/odd numbers, and implementing sorting algorithms.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

C Programming Assignment Questions

This document outlines a C programming assignment for first-semester CSE students, including basic programming tasks. The assignment consists of ten questions that cover fundamental concepts such as arithmetic operations, control structures, and array manipulation. Students are required to write programs to perform various operations, including finding sums, checking even/odd numbers, and implementing sorting algorithms.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C Programming Assignment Sample

Name: __________________________

Roll: ___________________________

Department: CSE

Semester: 1st

Course Title: Structured Programming Language

Course Code: CSE 1101

Assignment Questions:
1. Write a C program to find the sum, average, and product of three numbers.

2. Write a C program to find whether a number is even or odd.

3. Write a program to check whether a given year is a leap year or not.

4. Write a program to calculate the factorial of a number using a loop.

5. Write a C program to find the largest and smallest number in an array.

6. Write a program to print the multiplication table of any number.

7. Write a program to implement a simple calculator using switch-case.

8. Write a program to reverse a number entered by the user.

9. Write a program to count the number of vowels and consonants in a string.

10. Write a C program to sort an array using bubble sort.

Common questions

Powered by AI

A switch-case structure in C facilitates implementing a basic calculator by directing control flow based on user input for operations. The user inputs the operation (such as +, -, *, /), which is evaluated in the switch-case. Each case corresponds to an operation; for instance, 'case '+':' handles addition, executing the code block to add numbers and display results. Using default ensures handling invalid operators. Switch-case avoids multiple if-else checks, improving readability and enabling clear separation of logic for each operation .

Sorting an array using bubble sort in C presents challenges such as inefficiency for large datasets due to its O(n^2) time complexity. Each pass only bubbles the largest unsorted element to its correct position, requiring multiple passes. To mitigate this, improvements like tracking swap operations during a pass can minimize unnecessary iterations; if no swaps occur, the array is sorted and the algorithm can terminate early. Although conceptually simple, bubble sort is suboptimal for large arrays and can be replaced with more efficient algorithms like quicksort for better performance .

The algorithmic thought process for reversing a number involves extracting its digits and reconstructing the number in reverse order. In a C program, this is typically achieved using a loop where the last digit is extracted using modulus operation `number % 10`, multiplied by 10 to update reversed number, and removed from the original number by integer division `number / 10`. This process repeats until the original number reduces to zero. This method efficiently handles digits using basic arithmetic without requiring additional data structures, ensuring the program runs with linear complexity .

Implementing arithmetic operations in a simple C calculator is beneficial in educational settings for learning programming fundamentals, including control structures and user input handling. It aids understanding of operation algorithms and error handling. In constrained systems lacking built-in functions or requiring customization beyond typical calculator functions, a custom C calculator can provide tailored solutions. This practice enhances programming skills and provides insight into manual implementation of arithmetic logic .

Determining if a given year is a leap year involves checking a series of conditions. A year is a leap year if it is divisible by 4. However, if the year is divisible by 100, it is not a leap year unless it is also divisible by 400. In a C program, you can implement this logic using if-else statements to evaluate these conditions sequentially .

Programming a C function to find the largest and smallest numbers requires initializing two variables to store the maximum and minimum. One iteration sets both values to the first element's value. Subsequent iterations compare each element, updating the maximum and minimum values accordingly. Two separate iterations are theoretically not required as both values can be found in a single pass by comparing each element to existing max/min values. However, separating logic into independent steps can enhance clarity and focus on error handling, especially in educational contexts .

Using a loop to calculate a factorial in C enhances performance by avoiding the overhead associated with recursive function calls. Each recursive call consumes stack memory and increases the risk of stack overflow for large inputs. In contrast, a loop iteratively multiplies numbers in sequence, which typically consumes less memory and runs with linear time complexity, making it suitable for larger inputs and environments with restricted stack memory .

Bubble sort ensures an array is sorted by repeatedly stepping through the list, comparing adjacent elements and swapping them if they are in the wrong order. This process is known as 'bubbling up' the largest unsorted element to its correct position at the end of the array with each pass. The main limiting factor is its O(n^2) time complexity, which makes it inefficient for large datasets. Each pair of adjacent elements is compared for each pass, and no significant elements advance unless swaps occur, leading to unnecessary iterations .

To handle case sensitivity efficiently when counting vowels and consonants, a C program can convert all characters to lowercase using the `tolower()` function before checking. This approach avoids separate conditions for uppercase vowels. Looping through each character, the program checks if it is within 'a', 'e', 'i', 'o', 'u' to count as vowels, incrementing a vowel count. Characters outside these are checked for being alphabetic, incrementing consonant count. This method uniformly handles uppercase inputs and enhances code simplicity and efficiency .

To determine if a number is even or odd in a C program, the primary approach is to use the modulus operator (%). Conceptually, a number is even if it is divisible by 2 without a remainder, so the expression `number % 2 == 0` evaluates to true for even numbers. Conversely, `number % 2 != 0` evaluates to true for odd numbers. This method efficiently checks the parity of a number by examining its division remainder .

You might also like