Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Problem solving and programming Assignment 2

The document outlines the structure of a C program, detailing components such as header files, the main function, variable declaration, the program body, and the return statement. It also explains common header files and their significance, followed by examples of simple C programs including 'Hello, World!', the sum of two numbers, and finding the greater number. Each example includes the code, algorithms, and step-by-step execution details.

Uploaded by

ashrafsuhail1414
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Problem solving and programming Assignment 2

The document outlines the structure of a C program, detailing components such as header files, the main function, variable declaration, the program body, and the return statement. It also explains common header files and their significance, followed by examples of simple C programs including 'Hello, World!', the sum of two numbers, and finding the greater number. Each example includes the code, algorithms, and step-by-step execution details.

Uploaded by

ashrafsuhail1414
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Name : Ashraf Ahmed

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

<stdio.h> Standard Input and Output functions (e.g.,


printf(), scanf()).

<stdlib.h> Memory allocation, process control, conversions (e.g.,


malloc(), free()).

<math.h> Mathematical functions (e.g., sqrt(), pow(),


sin()).

<string.h> String manipulation functions (e.g., strlen(),


strcpy()).

<conio.h> Console input/output (e.g., getch(), clrscr()).

<ctype.h> Character handling functions (e.g., toupper(),


tolower()).

<time.h> Time-related functions (e.g., time(), clock()).


1. "Hello, World!" Program
#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}

Algorithm for "Hello, World!"


1. Start
2. Print "Hello, World!"
3. Stop
FlowChart for "Hello, World!"

Start

Initialize Program

Output:
printf("Hello, world!\n");

End

OUTPUT
2. Sum of Two Numbers
#include <stdio.h>

int main() {
int a, b, sum;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

sum = a + b;

printf("Sum = %d\n", sum);


return 0;
}

Algorithm for Sum of Two Numbers


1. Start
2. Take two numbers as input.
3. Add the two numbers.
4. Print the sum.
5. Stop
Step-by-Step Execution:

Step What Happens? Values in Memory Output on Screen

1 Program starts a = ?, b = ?, sum = ?

printf("Enter two Enter two


2
numbers: "); numbers:

3 User inputs 5 and 10 a = 5, b = 10

4 sum = a + b; (5 + 10) sum = 15

printf("Sum = %d\n",
5 Sum = 15
sum);

6 Program ends

Example Input & Output


Enter two numbers: 5 10
Sum = 15

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

Algorithm for Finding the Greater Number


1. Start
2. Take two numbers as input.
3. Compare the two numbers:
o If the first number is greater, print it.
o Else if the second number is greater, print it.
o Else print "Both are equal."
4. Stop
Step-by-Step Execution

Memory (Variable
Step What Happens? Output
Values)

1 Program starts num1 = ?, num2 = ?

Enter two
2 Asks user for two numbers
numbers:

3 User enters 8 and 12 num1 = 8, num2 = 12

Checks if (num1 > num2) →


4
8 > 12 (False)

Checks else if (num2 >


5
num1) → 12 > 8 (True)

12 is
6 Prints the result
greater

7 Program ends

Example Input & Output


Enter two numbers: 5 10
Sum = 15

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

You might also like