Problem solving and programming Assignment 2
Problem solving and programming Assignment 2
Roll No.: 09
PRN : 12412991
Class : CSDS-A (FY)
Assignment 2
2. Describe the structure of 'C' Program: (Header, main (), variable
declaration, body and return) in detail. Explain various header files
along with their significance in the program execution. Also write the
first simple program for: l) "Hello world" ii) sum of two numbers iii)
greater number etc...
Solution :
A C program follows a specific structure that ensures proper
execution. The key components are:
1. Header Files Inclusion
o This section includes the necessary header files using
#include preprocessor directives.
o Header files provide standard functions and macros that
are useful for input/output, string manipulation,
mathematical computations, etc.
2. Main Function (main())
o Every C program must have a main() function.
o Execution of the program starts from this function.
3. Variable Declaration
o Variables must be declared before they are used.
o Data types such as int, float, char, and double define the
type of data the variable will hold.
4. Body of the Program
o This contains the actual logic and statements that perform
the required operations.
o It includes loops, conditional statements, functions, and
expressions.
5. Return Statement (return 0;)
o The main() function returns an integer value.
o return 0; indicates successful execution.
Common Header Files in C and Their Significance:
Header File Description
int main() {
printf("Hello, World!\n");
return 0;
}
Start
Initialize Program
Output:
printf("Hello, world!\n");
End
OUTPUT
2. Sum of Two Numbers
#include <stdio.h>
int main() {
int a, b, sum;
sum = a + b;
printf("Sum = %d\n",
5 Sum = 15
sum);
6 Program ends
OUTPUT :
FlowChart for " Sum of Two Numbers"
Start
Input:
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
Calculate:
sum = num1 + num2;
Output:
printf("Sum of %d and %d is: %d\n",
num1, num2, sum);
End
3. Find the Greater Number
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 > num2)
printf("%d is greater\n", num1);
else if (num2 > num1)
printf("%d is greater\n", num2);
else
printf("Both numbers are equal\n");
return 0;
}
Memory (Variable
Step What Happens? Output
Values)
Enter two
2 Asks user for two numbers
numbers:
12 is
6 Prints the result
greater
7 Program ends
OUTPUT:
FlowChart for " Find the Greater Number "
Start
Input:
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
Comparison:
if (num1 > num2)
Output:
printf("%d is greater than %d\n",
num1, num2);
Output:
if (num2 > num1) printf("Both numbers are equal\n");
Output:
End
printf("%d is greater than %d\n",
num2, num1);