0% found this document useful (0 votes)
2 views9 pages

c Programming

The document is an internal assignment for a student named Jitendra Suryavanshi at Manipal University Jaipur, focusing on C programming. It covers various topics including operators (bitwise, conditional, relational), loop control statements (for, while, do...while), recursion, string handling functions (strcmp, strlen, strcat, strcpy), and the integration of functions with decision-making statements. Each section includes explanations, syntax, and examples to illustrate the concepts.
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)
2 views9 pages

c Programming

The document is an internal assignment for a student named Jitendra Suryavanshi at Manipal University Jaipur, focusing on C programming. It covers various topics including operators (bitwise, conditional, relational), loop control statements (for, while, do...while), recursion, string handling functions (strcmp, strlen, strcat, strcpy), and the integration of functions with decision-making statements. Each section includes explanations, syntax, and examples to illustrate the concepts.
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
You are on page 1/ 9

MANIPAL UNIVERSITY JAIPUR

INTERNAL ASSIGNMENT
STUDENT NAME JITENDRA SURYAVANSHI
ROLL NUMBER 251410202629
COURSE NAME & CODE C PROGRAMMING , DCA1107
PROGRAM BACHELOR OD COMPUTER APPLICATIONS

ASSIGNMENT -1
SET -1

1. Discuss about the following operators in C language with example.


a. Bitwise Operator
b. Conditional Operator
c. Relational Operator
Ans.
a. Bitwise Operators in C
Bitwise operators let you perform operations directly on the bits of variables. These are
mainly used when you're dealing with low-level programming, such as device drivers or
embedded systems.
Operator Meaning
& AND (both bits must be 1)
| OR
^ XOR (bits are different)
~ NOT (inverts each bit)
<< Shift left
>> Shift right

Example:

#include <stdio.h>

int main() {
int x = 5; // 0101
int y = 3; // 0011

printf("x & y = %d\n", x & y); // 0001 => 1


printf("x | y = %d\n", x | y); // 0111 => 7
printf("x ^ y = %d\n", x ^ y); // 0110 => 6
printf("~x = %d\n", ~x); // Inverts bits
printf("x << 1 = %d\n", x << 1); // 1010 => 10
printf("x >> 1 = %d\n", x >> 1); // 0010 => 2
return 0;
}

b. Conditional (Ternary) Operator


The conditional or ternary operator is a shortcut for simple if-else statements. It’s a
compact way to make decisions in your code.

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;

printf("Larger value is: %d\n", max);


return 0;
}

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;

printf("a == b: %d\n", a == b); // 0 (false)


printf("a != b: %d\n", a != b); // 1 (true)
printf("a > b: %d\n", a > b); // 1
printf("a < b: %d\n", a < b); // 0
printf("a >= b: %d\n", a >= b); // 1
printf("a <= b: %d\n", a <= b); // 0
return 0;
}

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;
}

This prints numbers from 0 to 4.

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;
}

This continues to run until i reaches 5.

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;
}

This prints numbers from 0 to 4.

Quick Comparison

Condition Executes At Least


Loop Type When to Use
Check Once?

At the Known number of


for No
beginning repetitions

At the Condition-controlled
while No
beginning looping

Run first, then check


do...while At the end Yes
condition
3. Explain the structure of a C program and outline the key features of the C
programming language.
Ans.

Basic Layout of a C Program


A typical C program is built in an organized way, with several important components.
Here's a common format:

#include <stdio.h> // Include header files

// Declare global variables


int globalVal = 10;

// Function prototype
void showMessage();

int main() { // Program starts here


int num = 5; // Declare local variable
printf("Hello from C!\n");
showMessage(); // Call a custom function
return 0; // Indicate successful execution
}

// 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

4. What is recursion in C? Explain how recursive functions work and provide an


example.
Ans.

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

A recursive function is designed with two key parts:

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.

Example: Finding Factorial

Here’s a simple way to find the factorial of a number using recursion:

#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

5. Explain the following string handling functions with examples.


a) strcmp() b) strlen( ) c) strcat() d) strcpy()
Ans.
a) strcmp() – String Comparison
The strcmp() function checks two strings and tells you how they differ:

 It gives 0 if the strings are exactly the same.


 If the first string comes before the second alphabetically, it returns a negative number.
 If the first string comes after, it returns a positive number.

Example:

#include <stdio.h>
#include <string.h>

int main() {
char one[] = "cat";
char two[] = "dog";

int check = strcmp(one, two);


printf("Comparison result: %d\n", check); // Likely negative
return 0;
}

b) strlen() – Find the Length of a String

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;
}

c) strcat() – Join Two Strings

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;
}

d) strcpy() – Copy One String into Another

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.

 Functions are used to group specific tasks or operations.


 Conditional statements help the program decide which action to take depending on
certain conditions.
Example 1: if-else Inside a Function

You can write conditions within a function to perform different actions based on the
input.

#include <stdio.h>

void evaluateScore(int score) {


if (score >= 90)
printf("You got an A\n");
else if (score >= 70)
printf("You got a B\n");
else
printf("You got a C or lower\n");
}

int main() {
evaluateScore(75); // Output: You got a B
return 0;
}

Example 2: Using switch for Multiple Options

The switch statement is helpful when checking against a list of possible fixed values.

#include <stdio.h>

void showDay(int dayNum) {


switch(dayNum) {
case 1: printf("Sunday\n"); break;
case 2: printf("Monday\n"); break;
case 3: printf("Tuesday\n"); break;
default: printf("Invalid input\n");
}
}

int main() {
showDay(2); // Output: Monday
return 0;
}

Example 3: Calling a Function Inside a Condition

You can also use the result of a function directly in an if condition.

#include <stdio.h>

int isPositive(int number) {


return number > 0;
}

int main() {
int n = -4;
if (isPositive(n))
printf("The number is positive\n");
else
printf("The number is not positive\n");

return 0;
}

You might also like