0% found this document useful (0 votes)
6 views14 pages

C Programming: Algorithms and Functions

The document discusses the importance of algorithm development in problem solving, emphasizing its role in organizing logic and reducing errors. It includes examples of C programming concepts such as built-in functions, loops, and data types, along with sample code for various tasks like finding the largest of three numbers and calculating factorials. Additionally, it compares different looping constructs and provides explanations of key programming principles.

Uploaded by

sundar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views14 pages

C Programming: Algorithms and Functions

The document discusses the importance of algorithm development in problem solving, emphasizing its role in organizing logic and reducing errors. It includes examples of C programming concepts such as built-in functions, loops, and data types, along with sample code for various tasks like finding the largest of three numbers and calculating factorials. Additionally, it compares different looping constructs and provides explanations of key programming principles.

Uploaded by

sundar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. Why is algorithm development important in problem solving?

Algorithm development is important because it provides a step-by-step


solution to a problem. It helps in understanding the problem clearly, organizing
logic, avoiding errors, and making programming easier and efficient.
Example: Before writing a program to find the largest of three numbers, an
algorithm defines the steps like “compare first two numbers, then compare
the larger with the third.”

[Link] a program to print “Hello World”.


#include <stdio.h>
int main()
{
printf("Hello World\n"); // Prints Hello World on the screen
return 0;
}

3. Difference between interactive mode and script mode

4. Purpose of scanf() and printf() functions


Scanf ()`: Used to read input from the user during program execution.
Example:
int age;
scanf("%d", &age);
Printf()`: Used to display output on the screen.
Example:
printf ("Your age is %d", age);

[Link] is algorithm development necessary before programming?


Algorithm development is necessary because it helps in:
- Understanding the problem clearly
- Planning the solution in logical steps
- Reducing programming errors
- Making code easier to write and debug
6. Difference between while and for loops

7.
Syntax of a nested if statement in C

8. How is the switch-case concept implemented in C?


9. Name two input/output functions in C
- Input: scanf()
- Output: printf()
10. Define assignment operator with example
The assignment operator `=` assigns a value to a variable.

11. Explain Problem Analysis Chart (PAC) in C programming with


example?
[Link] how an Algorithm helps in solving problem with a suitable
example

[Link] is a Flowchart? Mention any two symbols used in it.

[Link] the main components of a program structure in C.

[Link] the Differences between algorithm and flowchart with suitable


examples.
[Link] a C program to calculate Simple Interest.

[Link] a C program to evaluate the expression y = a² + b² + 2ab

[Link] the Differences between between entry-controlled and exit


controlled loops in C.
[Link] the purpose and working of return statement in C?

[Link] does a continue statement differ from break statement.

---------------------------------------------------------------
Part B
Develop a Solution to find the largest of three numbers.
(or)
Explain how to determine the largest of two numbers using a
problem analysis chart, algorithm, flowchart, and pseudocode.
Develop a solution to find the largest among three numbers entered by the user.
1. Algorithm
Step 1: Start
Step 2: Read three numbers — `A`, `B`, and `C`
Step 3: If `A > B` and `A > C`, then `A` is the largest
Step 4: Else if `B > C`, then `B` is the largest
Step 5: Else`C` is the largest
Step 6: Display the largest number
Step 7: Stop
2. Flowchart

3. Pseudocode
BEGIN
INPUT A, B, C
IF (A > B) AND (A > C) THEN
PRINT "A is the largest"
ELSE IF (B > C) THEN
PRINT "B is the largest"
ELSE
PRINT "C is the largest"
ENDIF
END
4. C Program
#include <stdio.h>
int main()
{
int a, b, c; // Input three numbers
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c); // Compare and find the largest
if (a > b && a > c)
{
printf("%d is the largest number.\n", a);
}
else if (b > c)
{
printf("%d is the largest number.\n", b);
}
else
{
printf("%d is the largest number.\n", c);
}
return 0;
}
5. Output Example
Enter three numbers: 45 78 12
78 is the largest number.
-------------------------------------------------------------------------------------------------

Discuss built-in functions in C and their uses.


Built-in Functions in C
[Link]
In programming, we often need to perform repeated or common tasks,
such as mathematical calculations, input/output operations, or string
manipulations. Writing the same code repeatedly can be time-consuming and
error-prone.
C programming solves this problem using functions. Among them, built-in
functions (library functions) are predefined functions provided by the C standard
library.
 These functions are already written, tested, and optimized.
 Programmers can directly use them by including the appropriate header
file.
 Examples of header files: stdio.h, math.h, string.h, ctype.h.
[Link]
Built-in Functions: Functions provided by the C language that perform
specific operations and are included in standard header files. No need to write
the function body.
Syntax: return_type function_name(arguments);
Example:
#include <stdio.h>
int main()
{
printf(“Hello World”); // printf() is a built-in function
return 0;
}
[Link] of Built-in Functions
1. Saves Time – No need to implement common operations manually.
2. Reduces Errors – Pre-tested and optimized by compiler developers.
3. Reusability – Functions can be used in multiple programs.
4. Improves Readability – Makes the code easier to understand.
5. Standardization – Ensures consistent behavior across compilers.
6. Efficiency – Faster execution because they are optimized.
4. Categories of Built-in Functions
A. Input/Output Functions (stdio.h)

Example:
#include <stdio.h>
int main()
{
int num;
printf(“Enter a number:”);
scanf(“%d”, &num);
printf(“You entered: %d”, num);
return 0;
}
B. Mathematical Functions (math.h)

Example:
#include <stdio.h>
#include <math.h>
int main()
{
double num = 16;
printf(“Square root = %.2f”, sqrt(num));
return 0;
}
C. String Handling Functions (string.h)

Example:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = “Hello”;
char str2[] = “World”;
strcat(str1, str2);
printf(“%s”, str1); // Hello World
return 0;
}
D. Character Handling Functions (ctype.h)

Example:
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch = ‘b’;
printf(“Uppercase: %c”, to upper(ch)); // B
return 0;
}
Example Program Using Multiple Built-in Functions
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
int main()
{
char name[20] = “cprogram”;
double num = 16;
printf("Length of string: %lu\n", strlen(name));
printf("Square root of %.1f: %.1f\n", num, sqrt(num));
printf("Uppercase of '%c': %c\n", 'b', to upper('b'));

return 0;
}
Output:
Length of string: 8
Square root of 16.0: 4.0
Uppercase of ‘b’: B
Advantages of Built-in Functions
1. Time-efficient – Avoid writing repetitive code.
2. Error-free – Pre-tested and reliable.
3. Improves Code Readability – Makes the program easier to understand.
4. Reusable – Can be used in multiple programs.
5. Standardized – Same function behavior across compilers.
6. Optimized for Performance – Functions are compiled and optimized.
-----------------------------------------------------------------------------------------------
Compare while, do-while, and for loops with examples.
(or)
Write a C program to demonstrate the use of different looping
statements (for, while, and do-while) and explain how each loop
executes during program execution
1. Introduction
Looping is an important concept in C programming that allows a set of
statements to be executed repeatedly until a specified condition becomes false.
C provides three main looping statements:
1. `while` loop
2. `do-while` loop
3. `for` loop
All three serve the same purpose — repetition, but differ in syntax, control
flow, and usage
4. Comparison of For Loop and While Loop,do while loop

5. Example Demonstrating All Loops Together


#include <stdio.h>
int main()
{
int i;
printf("Using while loop:\n");
i = 1;
while (i <= 3)
{
printf("%d ", i);
i++;
}
printf("\nUsing do-while loop:\n");
i = 1;
do
{
printf("%d ", i);
i++;
} while (i <= 3);
printf("\nUsing for loop:\n");
for (i = 1; i <= 3; i++)
{
printf("%d ", i);
}

return 0;
}
Output
Using while loop:
123
Using do-while loop:
123
Using for loop:
123
-------------------------------------------------------------------------------------------------
`.i) Explain primitive data types in C with examples.
.ii) Write a C Program using nested loops to print the following pattern:
*
**
***
****
*****
#include <stdio.h>
int main()
{
int i, j;
// Outer loop for rows
for (i = 1; i <= 5; i++)
{
// Inner loop for columns
for (j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n"); // Move to next line
}
return 0;
output
*
**
***
****
*****

ii) Write a program to calculate the factorial of a number using both for
loop and while loop. Compare the working of both loops.
Factorial of a Number using For Loop and While Loop,do..While loop
1. Definition:
The factorial of a number `n` is the product of all positive integers from `1`
to `n`.
It is denoted by `n!`.
Example:
5! = 5 × 4 × 3 × 2 × 1 = 120

Output
Enter a number: 5
Factorial of 5 = 120

Program using `for` loop Program using `while` loop Program using do….`while` loop

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


int main() int main() int main()
{ { {
int n, i; int n, i = 1; int n, i = 1;
long fact = 1; long fact = 1; long factorial = 1;
printf("Enter a number: "); printf("Enter a number: "); printf("Enter a positive integer:
scanf("%d", &n); scanf("%d", &n); ");
for (i = 1; i <= n; i++) while (i <= n) scanf("%d", &n);
{ { if (n < 0)
fact = fact * i; fact = fact * i; {
} i++; printf("Factorial is not
printf("Factorial of %d = } defined for negative numbers.\n");
%ld\n", n, fact); printf("Factorial of %d = return 0;
return 0; %ld\n", n, fact); }
} return 0; do
} {
factorial *= i; // factorial =
factorial * i
i++;
} while (i <= n);
printf("Factorial of %d = %ld\n",
n, factorial);
return 0;
}

-------------------------------------------------------------------------------------------------

.Write a C program demonstrating all types of operators.

You might also like