You are on page 1of 10

PIC - Practical - QB - Solutions - By -Th3_

1. Program to print grades based on percentage:

#include <stdio.h>
#include <conio.h>
void main() {
float per;
printf("Enter percentage: ");
scanf("%f", &per);
if (per >= 75)
printf("Grade: Distinction\n");
else if (per >= 60 && per < 75)
printf("Grade: A\n");
else if (per >= 55 && per < 60)
printf("Grade: B\n");
else if (per >= 40 && per < 55)
printf("Grade: Pass\n");
else
printf("Grade: Fail\n");
getch();
}

2. Program to find smallest number among three entered numbers using nested if statement:
#include <stdio.h>
#include <conio.h>
void main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a < b) {
if (a < c)
printf("Smallest number: %d\n", a);
else
printf("Smallest number: %d\n", c);
} else {
if (b < c)
printf("Smallest number: %d\n", b);
else
printf("Smallest number: %d\n", c);
}

getch();
}
PIC - Practical - QB - Solutions - By -Th3_

3. Program to print the name of a month and next month:


#include <stdio.h>
#include <conio.h>
void main() {
int month;
printf("Enter month (1-12): ");
scanf("%d", &month);
switch (month) {
case 1:
printf("Month: January, Next Month: February\n"); break;
case 2: printf("Month: February, Next Month: March\n"); break;
case 3: printf("Month: March, Next Month: April\n"); break;
case 4: printf("Month: April, Next Month: May\n"); break;
case 5: printf("Month: May, Next Month: June\n"); break;
case 6: printf("Month: June, Next Month: July\n"); break;
case 7: printf("Month: July, Next Month: August\n"); break;
case 8: printf("Month: August, Next Month: September\n"); break;
case 9: printf("Month: September, Next Month: October\n"); break;
case 10: printf("Month: October, Next Month: November\n"); break;
case 11: printf("Month: November, Next Month: December\n"); break;
case 12: printf("Month: December, Next Month: January\n"); break;
default: printf("Error: Invalid month\n");
}
getch();
}

4.
1. Program to find the sum of digits of a given number using a while loop:
#include <stdio.h>
#include <conio.h>
void 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);
getch();
}
PIC - Practical - QB - Solutions - By -Th3_

2. Program to find sum of the numbers from 1 to 100 using ‘for loop’:
#include <stdio.h>
#include <conio.h>
void main() {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i; // Add the current number to the sum
}
printf("The sum of numbers from 1 to 100 is: %d\n", sum);
getch();
}

5. Program to check if a given number is a palindrome using a while loop:


#include <stdio.h>
#include <conio.h>
void main() {
int num, reversedNum = 0, originalNum, remainder;
printf("Enter a number: ");
scanf("%d", &num);
originalNum = num;
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
if (originalNum == reversedNum)
printf("%d is a palindrome.\n", originalNum);
else
printf("%d is not a palindrome.\n", originalNum);

getch();
}

6.
1. Program to print odd numbers from 1 to 100:
#include <stdio.h>
#include <conio.h>
void main() {
printf("Odd numbers from 1 to 100:\n");
for (int i = 1; i <= 100; i+=2) {
printf("%d ", i);
PIC - Practical - QB - Solutions - By -Th3_

}
printf("\n");
getch();
}

2.Write a program to find factorial of given number:


#include <stdio.h>
#include <conio.h>
void main() {
int number;
int factorial = 1;
printf("Enter a number: ");
scanf("%d", &number);
for (int i = 1; i <= number; ++i) {
factorial *= i;
}
printf("Factorial of %d = %d\n", number, factorial);
getch();
}

7. Program to print a pattern:


#include <stdio.h>
#include <conio.h>
void main() {
int n = 4;
int count = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", count++);
}
printf("* ");
}
printf("\n");
getch();
}

8. Program to print an array in ascending order:

#include <stdio.h>
#include <conio.h>
void main() {
int arr[] = {5, 2, 8, 1, 3};
PIC - Practical - QB - Solutions - By -Th3_

int n = sizeof(arr) / sizeof(arr[0]);


// Bubble sort algorithm
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Array in ascending order: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

getch();
}

9. Program to calculate the addition of two 3x3 matrices:

#include <stdio.h>
#include <conio.h>
void main() {
int mat1[3][3], mat2[3][3], mat3[3][3], i, j;
printf("Enter elements of first 3x3 matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements of second 3x3 matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &mat2[i][j]);
}
}
printf("The addition of the two matrices is:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
mat3[i][j] = mat1[i][j] + mat2[i][j];
printf("%d ", mat3[i][j]);
PIC - Practical - QB - Solutions - By -Th3_

}
printf("\n");
}
getch();
}

10.
1. Program to calculate the length of a given string:

#include <stdio.h>
#include <conio.h>
void main() {
char str[100];
int length = 0;
printf("Enter a string: ");
scanf("%[^\n]s", str);
while (str[length] != '\0') {
length++;
}
printf("Length of the string: %d\n", length);
getch();
}

2.Write C program to print reverse of given string (without using string functions):

#include <stdio.h>
#include <conio.h>
void main() {
char str[100];
int length = 0;
printf("Enter a string: ");
scanf("%[^\n]s", str);
while (str[length] != '\0') {
length++;
}
printf("Reverse of the string: ");
for (int i = length - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n");
getch();
}
PIC - Practical - QB - Solutions - By -Th3_

11. Program using structure to hold details of a book and display the same:

#include <stdio.h>
#include <conio.h>
struct Library {
int accessionNumber;
char title[100];
char author[100];
float price;
int isIssued;
};
void main() {
struct Library book;
book.accessionNumber = 12345;
strcpy(book.title, "The Great Gatsby");
strcpy(book.author, "F. Scott Fitzgerald");
book.price = 10.99;
book.isIssued = 0;
printf("Book Details:\n");
printf("Accession Number: %d\n", book.accessionNumber);
printf("Title: %s\n", book.title);
printf("Author: %s\n", book.author);
printf("Price: $%.2f\n", book.price);
printf("Issued: %s\n", book.isIssued ? "Yes" : "No");
getch();
}

12. Program to accept and display information of 5 employees using a structure:

#include <stdio.h>
#include <conio.h>
struct Employee {
char name[100];
int id;
float salary;
};
void main() {
struct Employee emp[5];
printf("Enter information of 5 employees:\n");
for (int i = 0; i < 5; i++) {
printf("Employee %d:\n", i + 1);
printf("Name: ");
scanf("%s", emp[i].name);
PIC - Practical - QB - Solutions - By -Th3_

printf("ID: ");
scanf("%d", &emp[i].id);
printf("Salary: ");
scanf("%f", &emp[i].salary);
}
printf("\nDetails of 5 employees:\n");
for (int i = 0; i < 5; i++) {
printf("Employee %d:\n", i + 1);
printf("Name: %s\n", emp[i].name);
printf("ID: %d\n", emp[i].id);
printf("Salary: %.2f\n", emp[i].salary);
}
getch();
}

13. Program to check whether the entered string is palindrome or not using string function:

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main() {
char str[100];
int i, len, flag = 0;
printf("Enter a string: ");
gets(str);
len = strlen(str);
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
flag = 1;
break;
}
}
if (flag)
printf("%s is not a palindrome.\n", str);
else
printf("%s is a palindrome.\n", str);

getch();
}

14. Program to find factorial of a given number using recursion function:


PIC - Practical - QB - Solutions - By -Th3_

#include <stdio.h>
#include <conio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
void main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d\n", num, factorial(num));
getch();
}

15. Program to access values of variables using pointers and display address of variables:

#include <stdio.h>
#include <conio.h>
void main() {
int x = 10;
float y = 20.5;
char ch = 'A';
int *ptr1 = &x;
float *ptr2 = &y;
char *ptr3 = &ch;
printf("Address of x: %u, value: %d\n", ptr1, *ptr1);
printf("Address of y: %u, value: %f\n", ptr2, *ptr2);
printf("Address of ch: %u, value: %c\n", ptr3, *ptr3);
getch();
}

16.
1. Program to perform arithmetic operations on variables using pointers:

#include <stdio.h>
#include <conio.h>
void main() {
int num1 = 10, num2 = 5, sum, diff, prod, div;
PIC - Practical - QB - Solutions - By -Th3_

int *ptr1 = &num1, *ptr2 = &num2;


sum = *ptr1 + *ptr2;
diff = *ptr1 - *ptr2;
prod = *ptr1 * *ptr2;
div = *ptr1 / *ptr2;
printf("Sum: %d\n", sum);
printf("Difference: %d\n", diff);
printf("Product: %d\n", prod);
printf("Division: %d\n", div);
getch();
}

2. Program to print sum of array elements using pointers:

#include <stdio.h>
#include <conio.h>
void main() {
int arr[5] = {1, 2, 3, 4, 5};
int sum = 0;
int *ptr = arr;

for (int i = 0; i < 5; i++) {


sum += *(ptr + i);
}

printf("Sum of array elements: %d\n", sum);

getch();
}

You might also like