Apply for Zend Framework Certification Training

c



< Create an code for palendrome Pointers in c >



Problem Solving and Logic Development
Problem solving is the process of understanding a problem, developing a logical solution, and implementing that solution using a programming language.
For example, if we want to create a program to calculate the sum of two numbers, we first understand the problem, identify the required inputs and output, design the solution, and finally write the program.
1. Problem Definition
Problem definition means clearly stating what problem needs to be solved.
A good problem definition should identify:
What is given as input?
What output is required?
What conditions or restrictions exist?
What is the expected result?
Example: Find the largest of two numbers
Problem:
Write a program to find the largest of two numbers.
Input: Two numbers, A and B
Output: The larger number
Example:
Input:
A = 25
B = 18
Output:
25 is the largest number
A clear problem definition prevents confusion before programming begins.
2. Problem Analysis
After defining the problem, we analyze it to determine how it can be solved.
Problem analysis generally involves:
Identifying the input
Identifying the output
Identifying required processing
Identifying conditions
Breaking the problem into smaller steps
Example: Calculate Simple Interest
Formula:
Simple Interest = (P × R × T) / 100
Where:
P = Principal amount
R = Rate of interest
T = Time
Analysis
Input:
P, R, T
Processing:
SI = (P × R × T) / 100
Output:
Simple Interest
This analysis makes the actual programming task much easier.
3. General Problem-Solving Strategies
Different problems require different approaches. Some common strategies are:
1. Divide and Conquer
Break a large problem into smaller problems.
Example:
To calculate student results:
Student Result
     ↓
Calculate marks
     ↓
Calculate percentage
     ↓
Determine grade
     ↓
Display result
Each smaller task can be solved separately.
2. Brute Force
Try all possible solutions until the correct one is found.
Example:
Finding a particular number in a small list:
10  25  18  40  32
     ↑
Search each element one by one
3. Step-by-Step Approach
Solve the problem in a logical sequence.
Example: Making tea
Boil water
   ↓
Add tea
   ↓
Add milk
   ↓
Add sugar
   ↓
Boil
   ↓
Serve
Programming problems can also be solved using a similar sequence.
4. Pattern Recognition
Identify patterns in a problem and use them to create a solution.
Example:
2, 4, 6, 8, 10
The pattern is:
Next number = Previous number + 2
5. Working Backward
Start with the required result and determine what steps are needed to reach it.
This technique is useful in mathematical and logical problems.
4. Algorithm
An algorithm is a finite sequence of well-defined steps used to solve a problem.
Example: Algorithm to Add Two Numbers
Step 1: Start
Step 2: Read A and B
Step 3: Calculate Sum = A + B
Step 4: Display Sum
Step 5: Stop
Characteristics of an Algorithm
A good algorithm should have the following characteristics:
1. Input
It should accept zero or more inputs.
Example:
A and B
2. Output
It should produce at least one result.
Example:
Sum
3. Definiteness
Every step must be clear and unambiguous.
Bad:
Calculate something
Good:
Sum = A + B
4. Finiteness
The algorithm must eventually terminate.
5. Effectiveness
Each step should be practical and executable.
6. Correctness
The algorithm should produce the correct result.
5. Algorithm Development Techniques
Some common techniques used to develop algorithms are:
A. Sequence
Instructions are executed one after another.
Input A
Input B
Sum = A + B
Display Sum
B. Selection
A condition determines which action should be performed.
Example:
If marks >= 40
    Display "Pass"
Else
    Display "Fail"
C. Iteration
A set of instructions is repeated.
Example:
For i = 1 to 10
    Display i
This prints numbers from 1 to 10.
D. Recursion
A problem is solved by a function calling itself.
Example: Factorial
5! = 5 × 4 × 3 × 2 × 1
Recursive definition:
factorial(n) = n × factorial(n-1)
6. Flowchart
A flowchart is a graphical representation of an algorithm.
It uses standard symbols to show:
Input
Processing
Decisions
Output
Flow of execution
Example: Add Two Numbers
   START
     ↓
Input A, B
     ↓
Sum = A + B
     ↓
Display Sum
     ↓
    STOP
A flowchart makes the logic easier to understand visually.
7. Standard Flowchart Symbols
Symbol Name Purpose
Oval Terminator Start/Stop
Rectangle Process Calculation or processing
Parallelogram Input/Output Read or display data
Diamond Decision Condition/decision
Arrow Flow Line Shows direction
Circle Connector Connects different parts
Example of Decision
Suppose we want to check whether a number is positive or negative:
 
       START
         ↓
      Input N
         ↓
     Is N >= 0?
       /     \
     Yes      No
      ↓        ↓
 "Positive" "Negative"
       \      /
          ↓
         STOP
The diamond represents the decision.
8. Flowchart Designing and Implementation
While designing a flowchart, follow a logical sequence.
Steps
Understand the problem.
Identify input and output.
Identify calculations.
Identify decisions.
Arrange the steps in logical order.
Connect the symbols using arrows.
Check the flowchart for errors.
Convert the flowchart into code.
Example: Check Even or Odd
Logic:
Input N
   ↓
N % 2 == 0?
   ↓
Yes → Even
No  → Odd
The % operator calculates the remainder.
For example:
10 % 2 = 0
Therefore, 10 is even.
9. Pseudocode
Pseudocode is a simple, informal way of writing program logic using English-like statements.
It is not a programming language.
Example: Add Two Numbers
BEGIN
    READ A
    READ B
    SUM ← A + B
    PRINT SUM
END
Pseudocode allows programmers to focus on logic rather than programming-language syntax.
10. Algorithm vs Flowchart vs Pseudocode
Feature Algorithm Flowchart Pseudocode
Representation Written steps Graphical English-like
Main purpose Describe solution Visualize solution Describe programming logic
Easy to understand Yes Very easy visually Yes
Uses symbols No Yes No
Close to programming Moderate Less More
Easy to convert to code Yes Yes Very easy
Same Problem in Three Forms
Problem: Find whether a number is even or odd.
Algorithm
1. Start
2. Read N
3. If N % 2 == 0, display Even
4. Otherwise display Odd
5. Stop
Pseudocode
BEGIN
READ N
IF N % 2 == 0 THEN
    PRINT "Even"
ELSE
    PRINT "Odd"
END IF
END
Flowchart
       START
         ↓
      Input N
         ↓
     N % 2 = 0?
       /     \
     Yes      No
      ↓        ↓
   Display   Display
    "Even"    "Odd"
       \      /
          ↓
         STOP
11. Structured Programming
Structured programming is a programming approach in which a program is divided into logical blocks and uses structures such as:
Sequence
Selection
Iteration
Functions/modules
It avoids unnecessary jumps such as excessive use of goto.
Example
#include <stdio.h>
int main() {
    int marks;
    printf("Enter marks: ");
    scanf("%d", &marks);
    if (marks >= 40) {
        printf("Pass");
    } else {
        printf("Fail");
    }
    return 0;
}
This program follows a clear logical structure.
Advantages
Easy to understand
Easy to test
Easy to debug
Easy to maintain
Suitable for large programs
12. Unstructured Programming
Unstructured programming does not follow a clear logical structure. It may use excessive jumps such as goto.
Example:
#include <stdio.h>
int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    if (n < 0)
        goto negative;
    printf("Positive number");
    goto end;
negative:
    printf("Negative number");
end:
    return 0;
}
Although goto is available in C, excessive use can make programs difficult to understand and maintain.
Structured vs Unstructured Programming
Structured Unstructured
Logical program structure Irregular flow
Uses functions, loops and conditions Often uses jumps
Easier to understand Difficult to understand
Easier to debug Difficult to debug
Easier to maintain Difficult to maintain
Preferred in modern programming Generally avoided
13. Computational Problem Solving
Computational problem solving means using computational thinking and programming techniques to solve problems efficiently.
It generally follows this process:
Problem
   ↓
Problem Definition
   ↓
Problem Analysis
   ↓
Design Algorithm
   ↓
Create Flowchart/Pseudocode
   ↓
Write Program
   ↓
Test Program
   ↓
Debug Errors
   ↓
Final Solution
Example: Find the Largest of Three Numbers
Suppose:
A = 25
B = 42
C = 18
Step 1: Define the Problem
Find the largest among three numbers.
Step 2: Identify Input
A, B, C
Step 3: Identify Output
Largest number
Step 4: Develop Logic
If A > B and A > C
    A is largest
Else if B > A and B > C
    B is largest
Else
    C is largest
Step 5: Pseudocode
BEGIN
 
READ A, B, C
 
IF A > B AND A > C THEN
    PRINT A
ELSE IF B > A AND B > C THEN
    PRINT B
ELSE
    PRINT C
END IF
 
END
Step 6: C Implementation
#include <stdio.h>
int main() {
    int a, b, c, largest;
    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);
    if (a > b && a > c)
        largest = a;
    else if (b > a && b > c)
        largest = b;
    else
        largest = c;
    printf("Largest = %d", largest);
    return 0;
}
14. Complete Problem-Solving Example
Problem
Write a program to calculate the average of three numbers.
Problem Definition
Find the average of three numbers.
Input
A, B, C
Processing
Average = (A + B + C) / 3
Output
Average
Algorithm
1. Start
2. Read A, B and C
3. Calculate Sum = A + B + C
4. Calculate Average = Sum / 3
5. Display Average
6. Stop
Pseudocode
BEGIN
READ A, B, C
SUM ← A + B + C
AVERAGE ← SUM / 3
PRINT AVERAGE
END
C Program
#include <stdio.h>
int main() {
    float a, b, c, average;
    printf("Enter three numbers: ");
    scanf("%f %f %f", &a, &b, &c);
    average = (a + b + c) / 3;
    printf("Average = %.2f", average);
    return 0;
}
Quick Revision
Problem Definition
       ↓
Understand the Problem
       ↓
Problem Analysis
       ↓
Identify Input & Output
       ↓
Develop Algorithm
       ↓
Create Flowchart
       ↓
Write Pseudocode
       ↓
Write Program
       ↓
Test & Debug
       ↓
Final Solution
Key Difference
 
Algorithm → Step-by-step solution
Flowchart → Graphical representation of solution
Pseudocode → English-like representation of solution
Structured Programming → Organized programming using logical structures
Computational Problem Solving → Systematic use of computational methods to solve problems

< Create an code for palendrome Pointers in c >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top