0% found this document useful (0 votes)
12 views3 pages

C Programming Problem Solving Techniques

The document contains examples of C programs demonstrating sequential structures and selective controls. It includes programs for converting Fahrenheit to Celsius, summing digits of a number, finding the greatest of three numbers, checking if a number is even or odd, and determining a student's grade based on marks. Each program is accompanied by code snippets illustrating the implementation of the respective functionality.

Uploaded by

manoj322007
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)
12 views3 pages

C Programming Problem Solving Techniques

The document contains examples of C programs demonstrating sequential structures and selective controls. It includes programs for converting Fahrenheit to Celsius, summing digits of a number, finding the greatest of three numbers, checking if a number is even or odd, and determining a student's grade based on marks. Each program is accompanied by code snippets illustrating the implementation of the respective functionality.

Uploaded by

manoj322007
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

PROBLEM SOLVING TECHNIQUES LAB

1. SIMPLE EXAMPLES ON SEQUENTIAL STRUCTURE WITH


OPERATORS
a. To write a C program to convert Fahrenheit to Celsius

#include<stdio.h>
main()
{ float fah, cel;
printf ("Enter temperature in Fahrenheit scale : ");
scanf ("%f", &fah);
cel = 5*(fah-32)/9;
printf("%f Fahrenheit = %f Celsius\n", fah, cel);
}

b. To write a C program to find the sum of individual digits of a


given number.

//Program to find the sum of digits of a 3-digit number


#include<stdio.h>
main()
{ int num, sum;
printf("Enter a 3-digit number : ");
scanf("%d" , &num);
sum = num/100;
num=num%100;
sum=sum+num/10;
num=num%10;
sum=sum+num;
printf("Sum of digits = %d\n" , sum);
}

c. To write a C program that calculates the area of a rectangle


?????
2. PROGRAMS USING SELECTIVE CONTROLS
a. To write a C program to find the greatest of three numbers
#include<stdio.h>
int main()
{
int a, b,c;
printf(“\n Enter the first number: “);
scanf(“%d”, &a);
printf(“\n Enter the second number: “);
scanf(“%d”, &b);
printf(“\n Enter the third number: “);
scanf(“%d”, &c);
if(a > b) {
if(a > c) {
printf("\nGreatest is: %d", a);
} else {
printf("\nGreatest is: %d", c);
}
} else if(b > c) {
printf("\nGreatest is: %d", b);
} else {
printf("\nGreatest is: %d", c);
}
return 0;
}

b. To write a C Program to check whether a number is even or


odd

#include <stdio.h>

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}

return 0;
}

c. To write a C Program to print the grade of a Student

#include <stdio.h>
int main() {
float marks;
printf("Enter your marks (out of 100): ");
scanf("%f", &marks);
if (marks >= 90) {
printf("Grade: A\n");
} else if (marks >= 80 && marks < 90) {
printf("Grade: B\n");
} else if (marks >= 70 && marks < 80) {
printf("Grade: C\n");
} else if (marks >= 60 && marks < 70) {
printf("Grade: D\n");
} else {
printf("Grade: F (Failed)\n");
}

return 0;
}

d. To write a C program to check whether the given number is


positive or not

Common questions

Powered by AI

The programs are designed to accomplish specific tasks efficiently using fundamental operators and control structures, ensuring functionality. However, some aspects, like error handling for input validation and the use of more modular functions to improve code readability and maintainability, could be improved. Incorporating more comments could also help others understand the logic .

The program converts Fahrenheit to Celsius using the formula cel = 5 * (fah - 32) / 9. It reads a temperature in Fahrenheit as input, applies the formula, and prints the result in Celsius .

The program uses the modulus operator '%' to find the remainder of division by 2. If the remainder is 0, the number is even; otherwise, it is odd. This operation is effective because even numbers are divisible by 2 with no remainder, while odd numbers are not .

The input function scanf allows the program to capture user input during execution, enabling dynamic interaction. The output function printf is used to format and display results and messages to the user. Together, they facilitate user interaction and feedback, making programs functional and responsive to user actions .

The program uses nested if-else statements to compare the three numbers. It first checks if 'a' is greater than 'b', then it checks if 'a' is also greater than 'c'. If both conditions are true, 'a' is the greatest. If 'a' is not greater than 'c', 'c' is the greatest. If 'a' is not greater than 'b', it checks if 'b' is greater than 'c'. If true, 'b' is the greatest, otherwise 'c' is the greatest .

The program uses a series of else-if conditions to evaluate the student's marks. It checks ranges starting from the highest (90 and above) to the lowest, assigning grades A through F. Using else-if ensures that only one grade is assigned by stopping further checks once a condition is met .

Users may encounter errors such as incorrect input types leading to unexpected program behavior or division by zero errors if extending the programs. To handle these, programs should include input validation checks before processing and use conditional statements to manage error-prone operations. Implementing error messaging and guiding users can also enhance program robustness .

To complete the program, declare variables for the rectangle's length and width, prompt the user for these values using scanf, calculate the area using the formula area = length * width, and finally print the result. Including input and output functions is essential to complete the program .

These programs introduce learners to basic syntax, operators, and control structures such as if-else statements, essential for understanding programming logic. By practicing with real-world problems like conversions, comparisons, and arithmetic operations, beginners can develop critical thinking and problem-solving skills applicable to more complex software development tasks .

The program first divides the number by 100 to extract the hundreds digit and updates the sum. It then calculates the remainder of the number divided by 100 to isolate the tens and units digits, from which it extracts the tens digit by dividing by 10 and updates the sum again. Finally, the units digit is found by taking the remainder of division by 10, which is added to the sum .

You might also like