c Programming
c Programming
INTERNAL ASSIGNMENT
STUDENT NAME JITENDRA SURYAVANSHI
ROLL NUMBER 251410202629
COURSE NAME & CODE C PROGRAMMING , DCA1107
PROGRAM BACHELOR OD COMPUTER APPLICATIONS
ASSIGNMENT -1
SET -1
Example:
#include <stdio.h>
int main() {
int x = 5; // 0101
int y = 3; // 0011
Syntax:
condition ? value_if_true : value_if_false;
Example:
#include <stdio.h>
int main() {
int a = 10, b = 20;
int max = (a > b) ? a : b;
c. Relational Operators in C
These operators compare two values and return true (1) or false (0), depending on the
result.
Operator Function
== Is equal to
!= Is not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Example:
#include <stdio.h>
int main() {
int a = 15, b = 10;
2. Explain the different types of loop control statements in C. Provide syntax and
example for each.
Ans.
1. For Loop
The for loop is ideal when you know ahead of time how many times a block of code
should run. It uses a counter that gets updated every cycle.
Structure:
for (start; condition; update) {
// code
}
Example:
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("i = %d\n", i);
}
return 0;
}
2. While Loop
The while loop keeps running as long as the condition remains true. It's often used when
the number of repetitions isn't known at the start.
Structure:
while (condition) {
// code
}
Example:
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
printf("i = %d\n", i);
i++;
}
return 0;
}
3. do...while Loop
This loop runs the code first and checks the condition afterward. It's useful when you
want the code to execute at least once, no matter what.
Structure:
do {
// code
} while (condition);
Example:
#include <stdio.h>
int main() {
int i = 0;
do {
printf("i = %d\n", i);
i++;
} while (i < 5);
return 0;
}
Quick Comparison
At the Condition-controlled
while No
beginning looping
// Function prototype
void showMessage();
// Function definition
void showMessage() {
printf("This is a custom-defined function.\n");
}
1. Header Files
#include tells the compiler to use standard libraries like stdio.h for input/output.
2. Global Declarations
Variables placed outside functions that can be used anywhere in the program.
3. Function Prototypes
These give the compiler early information about functions used later in the file.
4. main() Function
The execution of every C program starts here.
5. Local Variables
Defined inside functions, used temporarily while the function runs.
6. Function Calls
Used to execute code blocks written as functions.
7. Return Statement
Signals the end of the main() and typically returns 0 to show success.
ASSIGNMENT -1
SET -2
What is Recursion in C?
In C programming, recursion refers to the method where a function keeps calling itself
to work through a problem. This technique is useful when a task can be divided into
smaller versions of the same task.
How It Works
1. Base case – This stops the function from calling itself forever.
2. Recursive step – This is where the function calls itself, but with a changed input that
moves closer to the base case.
Each time the function calls itself, a new copy is created on the call stack. Once it
reaches the base case, the functions start returning one by one, going back through the
earlier calls.
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
Output:
Factorial of 5 is 120
Example:
#include <stdio.h>
#include <string.h>
int main() {
char one[] = "cat";
char two[] = "dog";
This function counts how many characters are in a string—not counting the \0 at the end.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char word[] = "computer";
int len = strlen(word);
printf("Number of characters: %d\n", len); // Output: 8
return 0;
}
With strcat(), you can attach one string to the end of another. Be careful—your first
string must have enough extra space to hold the result.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char base[30] = "Good ";
char addon[] = "Morning";
strcat(base, addon);
printf("After joining: %s\n", base); // Output: Good Morning
return 0;
}
This function duplicates the content of one string into another. The destination string
must be large enough to hold the copy.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char original[] = "Success";
char duplicate[20];
strcpy(duplicate, original);
printf("Copied string: %s\n", duplicate); // Output: Success
return 0;
}
6. Explain how functions can be combined with decision-making statements (like if,
else, and switch) to solve problems.
Ans.
In C programming, using functions along with decision-making tools like if, else, and
switch allows you to build more structured and adaptable programs. It helps break the
logic into smaller parts and makes the code easier to understand and maintain.
You can write conditions within a function to perform different actions based on the
input.
#include <stdio.h>
int main() {
evaluateScore(75); // Output: You got a B
return 0;
}
The switch statement is helpful when checking against a list of possible fixed values.
#include <stdio.h>
int main() {
showDay(2); // Output: Monday
return 0;
}
#include <stdio.h>
int main() {
int n = -4;
if (isPositive(n))
printf("The number is positive\n");
else
printf("The number is not positive\n");
return 0;
}