0% found this document useful (0 votes)
5 views

pic practical slip Answers

The document contains a series of programming tasks in C, including calculating areas and perimeters of shapes, checking leap years, and determining if strings are palindromes. Each task includes code snippets, algorithms, flowcharts, and theoretical explanations of programming concepts such as loops and operators. The document is structured into slips, each focusing on different programming challenges and concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

pic practical slip Answers

The document contains a series of programming tasks in C, including calculating areas and perimeters of shapes, checking leap years, and determining if strings are palindromes. Each task includes code snippets, algorithms, flowcharts, and theoretical explanations of programming concepts such as loops and operators. The document is structured into slips, each focusing on different programming challenges and concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Slip 1

Task 1: Program to calculate area and perimeter of rectangle

#include <stdio.h>

Int main() {

Float length, width, area, perimeter;

Printf(“Enter length and width of the rectangle: “);

Scanf(“%f%f”, &length, &width);

Area = length * width;

Perimeter = 2 * (length + width);

Printf(“Area = %.2f\n”, area);

Printf(“Perimeter = %.2f\n”, perimeter);

Return 0;

}
Task 2.1: Algorithm & Flowchart

Algorithm:

1. Start

2. Read length and width

3. Calculate area = length × width

4. Calculate perimeter = 2 × (length + width)

5. Print area and perimeter

6. Stop
Flowchart Symbols:

Terminator (Start/Stop): Oval

Input/Output: Parallelogram

Process: Rectangle

Decision: Diamond (Draw basic flow using these for the above algorithm)

Task 2.2: Definitions

Algorithm: Step-by-step solution to a problem.

Flowchart: Pictorial representation of an algorithm using symbols.

Five symbols:

Start/End – Oval

Input/Output – Parallelogram
Process – Rectangle

Decision – Diamond

Connector – Circle

Slip 2

Task 1: Program to calculate area and circumference of circle

#include <stdio.h>

#define PI 3.1416

Int main() {

Float radius, area, circumference;

Printf(“Enter radius of the circle: “);

Scanf(“%f”, &radius);
Area = PI * radius * radius;

Circumference = 2 * PI * radius;

Printf(“Area = %.2f\n”, area);

Printf(“Circumference = %.2f\n”, circumference);

Return 0;

Task 2.1: Algorithm & Flowchart

Algorithm:

1. Start

2. Read radius

3. Calculate area = π × r²
4. Calculate circumference = 2 × π × r

5. Print area and circumference

6. Stop

Flowchart: Same as before, apply symbols.

Task 2.2: Theory

Tokens: Smallest elements of a C program (keywords, identifiers, constants, operators).

Keywords: Reserved words in C (e.g., int, return, if, else) which have fixed meanings.

Slip 3
Task 1: Program to check leap year

#include <stdio.h>

Int main() {

Int year;

Printf(“Enter a year: “);

Scanf(“%d”, &year);

If ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

Printf(“%d is a leap year.\n”, year);

Else

Printf(“%d is not a leap year.\n”, year);

Return 0;

Task 2.1: Algorithm & Flowchart

Algorithm:

1. Start
2. Read year

3. If year divisible by 4 and not 100 or divisible by 400, it’s a leap year

4. Print result

5. Stop

Flowchart: Use decision diamond for the condition.

Task 2.2: Theory

Relational operators: Compare values (e.g., ==, !=, <, >, <=, >=)

Logical operators:
&& (AND)

|| (OR)

! (NOT)

Shall I continue with Slips 4 to 6 next?

Slip 4

Task 1: Program to check if a string is a palindrome

#include <stdio.h>

#include <string.h>

int main() {

char str[100], rev[100];

int i, len, flag = 1;


printf("Enter a string: ");

scanf("%s", str);

len = strlen(str);

for(i = 0; i < len; i++) {

if(str[i] != str[len - i - 1]) {

flag = 0;

break;

if(flag)

printf("The string is a palindrome.\n");

else

printf("The string is not a palindrome.\n");

return 0;

Task 2.1: Algorithm & Flowchart

Algorithm:
1. Start

2. Read a string

3. Reverse the string

4. Compare original and reversed string

5. If equal, it's a palindrome; else, not

6. Stop

Flowchart: Use process blocks for reverse and compare, and a decision block for checking
equality.

Task 2.2: Theory


Types of loops:

for

while

do-while

Example: while loop

int i = 1;

while (i <= 5) {

printf("%d\n", i);

i++;

---

Slip 5
Task 1: Program to find greatest of 3 numbers using conditional operator

#include <stdio.h>

int main() {

int a, b, c, max;

printf("Enter three numbers: ");

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

max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);

printf("Greatest number is %d\n", max);

return 0;

Task 2.1: Algorithm & Flowchart

Algorithm:

1. Start
2. Read 3 numbers

3. Use conditional operator to find greatest

4. Display result

5. Stop

Flowchart: Decision symbols to compare numbers

Task 2.2: Syntax of Loops

while:

while (condition) {
// body

do...while:

do {

// body

} while (condition);

---

Slip 6

Task 1: Program to check vowel or consonant

#include <stdio.h>

int main() {

char ch;

printf("Enter a character: ");


scanf(" %c", &ch);

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||

ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')

printf("%c is a vowel.\n", ch);

else

printf("%c is a consonant.\n", ch);

return 0;

Task 2.1: Algorithm & Flowchart

Algorithm:

1. Start

2. Read character

3. Check if it's vowel using condition


4. Display result

5. Stop

Flowchart: Use a decision box for vowel checking condition

Task 2.2: Difference between while and do...while | Feature | while | do...while
| |-----------------|--------------------------|------------------------------| | Condition Check | Before loop
body | After loop body | | Execution | May never execute | Executes at least
once | | Syntax | while (cond) {} | do { } while (cond); |

---

Shall I continue with Slips 7 to 9?

Slip 7

Task 1: Program to print student grade based on percentage

#include <stdio.h>
Int main() {

Float percentage;

Printf(“Enter student percentage: “);

Scanf(“%f”, &percentage);

If(percentage >= 75)

Printf(“Grade: Distinction\n”);

Else if(percentage >= 60)

Printf(“Grade: First Class\n”);

Else if(percentage >= 50)

Printf(“Grade: Second Class\n”);

Else if(percentage >= 35)

Printf(“Grade: Pass Class\n”);

Else

Printf(“Grade: Fail\n”);

Return 0;

Task 2.1: Algorithm & Flowchart


Algorithm:

1. Start

2. Input percentage

3. Use conditions to determine grade

4. Display grade

5. Stop

Flowchart: Use multiple decision blocks for conditions

Task 2.2: Syntax of for loop


For(initialization; condition; increment/decrement) {

// loop body

Example:

For(int I = 1; I <= 5; i++) {

Printf(“%d\n”, i);

Slip 8

Task 1: Program to find sum of digits of a number

#include <stdio.h>

Int main() {

Int num, sum = 0, digit;

Printf(“Enter a number: “);

Scanf(“%d”, &num);
While(num != 0) {

Digit = num % 10;

Sum += digit;

Num /= 10;

Printf(“Sum of digits = %d\n”, sum);

Return 0;

Task 2.1: Algorithm & Flowchart

Algorithm:

1. Start

2. Input number

3. Initialize sum = 0
4. While number is not 0:

Digit = number % 10

Sum = sum + digit

Number = number / 10

5. Print sum

6. Stop

Flowchart: Use loop and process blocks to extract and add digits

Task 2.2: Definitions


Two-dimensional array: An array with rows and columns (e.g., int a[3][3];)

Multidimensional array: Array with more than 2 dimensions (e.g., int a[2][3][4];)

Slip 9

Task 1: Program to print Fibonacci series

#include <stdio.h>

Int main() {

Int n, t1 = 0, t2 = 1, next;

Printf(“Enter the number of terms: “);

Scanf(“%d”, &n);

Printf(“Fibonacci Series: “);

For(int I = 1; I <= n; i++) {

Printf(“%d “, t1);
Next = t1 + t2;

T1 = t2;

T2 = next;

Return 0;

Task 2.1: Algorithm & Flowchart

Algorithm:

1. Start

2. Read number of terms

3. Initialize t1 = 0, t2 = 1

4. Loop from 1 to n:
Print t1

Next = t1 + t2

T1 = t2

T2 = next

5. Stop

Flowchart: Loop with sequence updates and output

Task 2.2: Syntax of switch-case

Switch(expression) {

Case value1:

// code

Break;
Case value2:

// code

Break;

Default:

// code

Ready for Slips 10 to 12 next?

Slip 10

Task 1: Program to find factorial

#include <stdio.h>

int main() {

int num, fact = 1;

printf("Enter a number: ");

scanf("%d", &num);

for(int i = 1; i <= num; i++) {


fact *= i;

printf("Factorial = %d\n", fact);

return 0;

Task 2.1: Algorithm & Flowchart

Algorithm:

1. Start

2. Input number

3. Initialize fact = 1

4. Loop from 1 to number:


fact = fact × i

5. Print factorial

6. Stop

Flowchart: Loop block to multiply numbers

Task 2.2: Call by value

In call by value, the actual value is passed and changes do not reflect back.

Example:

void display(int x) {

x = x + 10;
printf("Inside function: %d\n", x);

int main() {

int a = 5;

display(a);

printf("Outside function: %d\n", a);

return 0;

---

Slip 11

Task 1: Program to sort array in ascending order

#include <stdio.h>

int main() {

int a[100], n, temp;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter %d elements: ", n);

for(int i = 0; i < n; i++)

scanf("%d", &a[i]);

for(int i = 0; i < n - 1; i++) {

for(int j = i + 1; j < n; j++) {

if(a[i] > a[j]) {

temp = a[i];

a[i] = a[j];

a[j] = temp;

printf("Sorted array in ascending order:\n");

for(int i = 0; i < n; i++)

printf("%d ", a[i]);

return 0;

You might also like