[go: up one dir, main page]

0% found this document useful (0 votes)
60 views2 pages

Computer Science Practical Solution

The document contains solutions for two programming tasks in C: calculating the area and perimeter of a rectangle, and checking if a number is even or odd. Each task includes the C code and a step-by-step algorithm. Additionally, it provides answers to common viva-voce questions related to algorithms, flowcharts, and C programming syntax.

Uploaded by

Saadat Zaidi
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)
60 views2 pages

Computer Science Practical Solution

The document contains solutions for two programming tasks in C: calculating the area and perimeter of a rectangle, and checking if a number is even or odd. Each task includes the C code and a step-by-step algorithm. Additionally, it provides answers to common viva-voce questions related to algorithms, flowcharts, and C programming syntax.

Uploaded by

Saadat Zaidi
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
You are on page 1/ 2

Computer Science Practical Paper - Solution

Task 1: Program to Print Area and Perimeter of a Rectangle


Below is a C program to calculate the area and perimeter of a rectangle:

#include <stdio.h>

int main() {
float length, width, area, perimeter;

// Input from user


printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);

// Calculations
area = length * width;
perimeter = 2 * (length + width);

// Output results
printf("Area of Rectangle: %.2f\n", area);
printf("Perimeter of Rectangle: %.2f\n", perimeter);

return 0;
}

Algorithm:
1. Start
2. Input length and width of the rectangle
3. Calculate area (area = length * width)
4. Calculate perimeter (perimeter = 2 * (length + width))
5. Display area and perimeter
6. Stop

Task 2: Program to Check Even or Odd Number


Below is a C program to check if a number is even or odd:

#include <stdio.h>

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

// Check even or odd


if (num % 2 == 0)
printf("%d is Even.\n", num);
else
printf("%d is Odd.\n", num);

return 0;
}

Algorithm:
1. Start
2. Input a number
3. Check if number is divisible by 2
- If true, print 'Even'
- Else, print 'Odd'
4. Stop

Viva-Voce Questions
1. What is an algorithm? - An algorithm is a step-by-step procedure to solve a problem.

2. What is a flowchart? - A flowchart is a graphical representation of an algorithm using


symbols.

3. What is the difference between scanf and printf in C? - scanf is used for input, and printf is
used for output.

4. What is the difference between an even and an odd number? - An even number is
divisible by 2, while an odd number is not.

5. What is the syntax of an if statement in C? - if(condition) { statements } else { statements }

You might also like