PIC Pyq Code
PIC Pyq Code
#include <stdio.h>
void reverseNumber(int num) {
printf("Reversed number: ");
while (num > 0) {
printf("%d", num % 10);
num /= 10;
}
}
void main() {
int num = 1234;
reverseNumber(num);
}
#include <stdio.h>
void main() {
int sum = 0;
for (int i = 1; i <= 20; i += 2) {
sum += i;
}
printf("Sum of odd numbers between 1 to 20: %d\n", sum);
}
#include <stdio.h>
void main() {
int num, reversedNum = 0;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
reversedNum = reversedNum * 10 + num % 10;
num /= 10;
}
printf("Reversed number: %d\n", reversedNum);
}
#include <stdio.h>
void main() {
int arr[10], i, j, temp;
printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++)
scanf("%d", &arr[i]);
#include <stdio.h>
void main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
#include <stdio.h>
void main() {
int numbers[10], sum = 0, i;
printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &numbers[i]);
sum += numbers[i];
}
#include <stdio.h>
struct Student {
int rollNo;
char name[50];
float marks;
};
void main() {
struct Student students[3];
printf("Enter data for 3 students:\n");
for (int i = 0; i < 3; i++) {
printf("Enter roll number for student %d: ", i + 1);
scanf("%d", &students[i].rollNo);
printf("Enter name for student %d: ", i + 1);
scanf("%s", students[i].name); // Assuming no spaces in the name
printf("Enter marks for student %d: ", i + 1);
scanf("%f", &students[i].marks);
}
printf("\nStudent data:\n");
for (int i = 0; i < 3; i++) {
printf("Student %d:\n", i + 1);
printf("Roll number: %d\n", students[i].rollNo);
printf("Name: %s\n", students[i].name);
printf("Marks: %.2f\n", students[i].marks);
}
}
8) Write a program to add, subtract, multiply and divide two numbers, accepted
from the user using switch case.
#include <stdio.h>
void main() {
float num1, num2, result;
char operator;
switch (operator) {
case '+':
result = num1 + num2;
printf("Result: %.2f\n", result);
break;
case '-':
result = num1 - num2;
printf("Result: %.2f\n", result);
break;
case '*':
result = num1 * num2;
printf("Result: %.2f\n", result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2f\n", result);
} else {
printf("Error! Division by zero.\n");
}
break;
default:
printf("Invalid operator.\n");
}
}
9) Write a program to calculate the sum of all the odd numbers between 1 to 20.
#include <stdio.h>
void main() {
int sum = 0;
10)Write a program to compute the sum of all elements stored in an array using
pointers.
#include <stdio.h>
void main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
int sum = 0;
#include <stdio.h>
void fibonacci(int n) {
int a = 0, b = 1;
int i, next;
void main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
fibonacci(n);
}
#include <stdio.h>
void main() {
int num, fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
13)Write a program to display table of given number (Accept number from user).
#include <stdio.h>
void main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
#include <stdio.h>
void main() {
int sum = 0;
for (int i = 2; i <= 100; i += 2) {
sum += i;
}
printf("Sum of even numbers between 1 to 100: %d\n", sum);
}
15)Write a program to accept ten numbers in an array. Sort array elements and
display it
#include <stdio.h>
void main() {
int numbers[10];
#include <stdio.h>
void main() {
int matrix1[3][3], matrix2[3][3], sum[3][3];
// Addition of matrices
printf("Resultant matrix after addition:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
printf("%d ", sum[i][j]);
}
printf("\n");
}
}
17)Write a program to compute the sum of all elements stored in an array using
pointers.
#include <stdio.h>
void main() {
int arr[] = {5, 10, 15, 20, 25};
int *ptr = arr;
int sum = 0;
#include <stdio.h>
struct Employee {
char name[50];
int age;
char city[50];
};
void main() {
struct Employee employees[3];
printf("\nEmployee details:\n");
for (int i = 0; i < 3; i++) {
printf("Employee %d:\n", i + 1);
printf("Name: %s\n", employees[i].name);
printf("Age: %d\n", employees[i].age);
printf("City: %s\n", employees[i].city);
}
}
#include <stdio.h>
#include <string.h>
void main() {
char str1[50] = "Hello";
char str2[50] = "World";
char str3[50];
// strcmp()
int result = strcmp(str1, str2);
printf("strcmp(\"%s\", \"%s\") = %d\n", str1, str2, result);
// strcpy()
strcpy(str3, str1);
printf("strcpy(str3, str1): %s\n", str3);
// strlen()
printf("strlen(\"%s\") = %d\n", str1, strlen(str1));
// strcat()
strcat(str1, str2);
printf("strcat(str1, str2): %s\n", str1);
}
#include <stdio.h>
void main() {
int num1 = 10, num2 = 5;
int *ptr1 = &num1, *ptr2 = &num2;
int sum, diff, product, quotient;
21) Write ‘C’ program to add two distances given in kilometer using structure.
#include <stdio.h>
struct Distance {
float kilometers;
};
void main() {
struct Distance distance1, distance2, sum;
// Adding distances
sum.kilometers = distance1.kilometers + distance2.kilometers;
#include <stdio.h>
void main() {
int arr[] = {5, 10, 15, 20, 25};
int *ptr = arr;
int sum = 0;
23)Write a ‘C’ program to determine whether a given number is prime or not and
also draw flowchart for this program
#include <stdio.h>
void main() {
int num, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) {
isPrime = 0;
} else {
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
}
24)Write a ‘C’ program to find the largest and smallest number in a given array.
#include <stdio.h>
void main() {
int arr[] = {5, 10, 15, 3, 20, 25};
int size = sizeof(arr) / sizeof(arr[0]);
int smallest = arr[0], largest = arr[0];
// Finding the smallest and largest numbers in the array
for (int i = 1; i < size; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
}
if (arr[i] > largest) {
largest = arr[i];
}
}
#include <stdio.h>
struct Student {
int roll_no;
char name[50];
float marks;
};
void main() {
struct Student s;
struct Student *ptr;
ptr = &s;
#include <stdio.h>
void main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("%c is a VOWEL.\n", ch);
break;
default:
printf("%c is a CONSONANT.\n", ch);
}
}
#include <stdio.h>
#define PI 3.14159
void main() {
float radius;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
28)Write a program to declare structure employee having data member name, age,
city. Accept data for three employees and display it.
#include <stdio.h>
struct employee {
char name[50];
int age;
char city[50];
};
void main() {
struct employee emp[3];
29) Suresh’s basic salary is input through the keyboard. His dearness allowance is
40% of basic salary, and house rent allowance is 20% of basic salary. Write a
program to calculate his gross salary.
#include <stdio.h>
void main() {
float basic_salary, da, hra, gross_salary;
#include <stdio.h>
void main() {
int a = 10, b = 20;
printf("Before swapping: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swapping: a = %d, b = %d\n", a, b);
}
#include <stdio.h>
void main() {
int a = 5, b = 10;
if (a > 0 && b > 0) {
printf("Both a and b are greater than 0\n");
} else {
printf("At least one of a and b is not greater than 0\n");
}
}
32) Develop a program to swap two numbers using pointer and add swapped numbers
also print their addition.
#include <stdio.h>
void main() {
int a = 10, b = 20;
printf("Before swapping: a = %d, b = %d\n", a, b);
swapAndAdd(&a, &b);
printf("After swapping: a = %d, b = %d\n", a, b);
printf("Sum of swapped numbers: %d\n", a + b);
}
33)Design a program in C to read the n numbers of values in an array and display
it in reverse order.
#include <stdio.h>
void main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Elements in reverse order:\n");
for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
}
34) Write a program to take input as a number and reverse it by using a while
loop.
#include <stdio.h>
void main() {
int num, reversedNum = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
printf("Reversed number: %d\n", reversedNum);
}
#include <stdio.h>
void main() {
int num, sum = 0;
do {
printf("Enter a number (enter 0 to exit): ");
scanf("%d", &num);
sum += num;
} while (num != 0);
printf("Sum: %d\n", sum);
}
36) Write a program using structure to display information of employee which
consist of employee id, name, age and salary.
#include <stdio.h>
void main() {
struct Employee {
int id;
char name[50];
int age;
float salary;
} emp;
printf("Enter employee details:\n");
printf("ID: ");
scanf("%d", &emp.id);
printf("Name: ");
scanf("%s", emp.name);
printf("Age: ");
scanf("%d", &emp.age);
printf("Salary: ");
scanf("%f", &emp.salary);
printf("Employee Details:\n");
printf("ID: %d\n", emp.id);
printf("Name: %s\n", emp.name);
printf("Age: %d\n", emp.age);
printf("Salary: %.2f\n", emp.salary);
}
#include <stdio.h>
void main() {
int mat1[3][3], mat2[3][3], result[3][3];
printf("Enter elements of first matrix:\n");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
scanf("%d", &mat2[i][j]);
}
}
// Matrix multiplication
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
result[i][j] = 0;
for (int k = 0; k < 3; ++k) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
printf("Resultant matrix after multiplication:\n");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
printf("%d ", result[i][j]);
}
printf("\n");
}
}
38) Write a program to declare an array of 5 elements and display sum of all
array elements.
#include <stdio.h>
void main() {
int arr[5], sum = 0;
printf("Enter 5 elements:\n");
for (int i = 0; i < 5; ++i) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum of array elements: %d\n", sum);
}
39) Write a C program to declare structure employee having data member name,
age, designation and salary. Accept and display information of 1 employee
#include <stdio.h>
void main() {
struct Employee {
char name[50];
int age;
char designation[50];
float salary;
} emp;
printf("Enter employee details:\n");
printf("Name: ");
scanf("%s", emp.name);
printf("Age: ");
scanf("%d", &emp.age);
printf("Designation: ");
scanf("%s", emp.designation);
printf("Salary: ");
scanf("%f", &emp.salary);
printf("Employee Details:\n");
printf("Name: %s\n", emp.name);
printf("Age: %d\n", emp.age);
printf("Designation: %s\n", emp.designation);
printf("Salary: %.2f\n", emp.salary);
}
40) Write a program to declare structure student having roll no, name & marks.
Accept & display data for 3 students
#include <stdio.h>
void main() {
struct Student {
int rollNo;
char name[50];
float marks;
} students[3];
printf("Enter details of 3 students:\n");
for (int i = 0; i < 3; ++i) {
printf("Student %d\n", i + 1);
printf("Roll No: ");
scanf("%d", &students[i].rollNo);
printf("Name: ");
scanf("%s", students[i].name);
printf("Marks: ");
scanf("%f", &students[i].marks);
}
printf("Details of students:\n");
for (int i = 0; i < 3; ++i) {
printf("Student %d\n", i + 1);
printf("Roll No: %d\n", students[i].rollNo);
printf("Name: %s\n", students[i].name);
printf("Marks: %.2f\n", students[i].marks);
}
}
41) Write a program to read two strings and find whether they are equal or not
#include <stdio.h>
#include <string.h>
void main() {
char str1[100], str2[100];
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
if (strcmp(str1, str2) == 0) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}
}
42) Write a program to compute the sum of all elements stored in an array
using pointers.
#include <stdio.h>
void main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
int sum = 0;
for (int i = 0; i < 5; ++i) {
sum += *ptr;
ptr++;
}
printf("Sum of array elements: %d\n", sum);
}
43) Write a program to accept a string as input from user and determine its
length. [Don’t use built in library function strlen()]
#include <stdio.h>
void main() {
char str[100];
int length = 0;
printf("Enter a string: ");
scanf("%s", str);
while (str[length] != '\0') {
length++;
}
printf("Length of the string: %d\n", length);
}
void main() {
int num, reversedNum = 0, originalNum;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
while (num != 0) {
reversedNum = reversedNum * 10 + num % 10;
num /= 10;
}
if (originalNum == reversedNum) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
}
}
#include <stdio.h>
void main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Addition: %d\n", add(num1, num2));
printf("Subtraction: %d\n", subtract(num1, num2));
printf("Multiplication: %d\n", multiply(num1, num2));
printf("Division: %.2f\n", divide(num1, num2));
}
46) Write a program to accept marks of four subjects as input from user.
Calculate and display total and percentage marks of student.
#include <stdio.h>
void main() {
int marks[4], totalMarks = 0;
float percentage;
printf("Enter marks of four subjects:\n");
for (int i = 0; i < 4; ++i) {
printf("Subject %d: ", i + 1);
scanf("%d", &marks[i]);
totalMarks += marks[i];
}
percentage = (float)totalMarks / 4;
printf("Total marks: %d\n", totalMarks);
printf("Percentage: %.2f%%\n", percentage);
}
47) Write a program to swap two numbers using call by value
#include <stdio.h>
void main() {
int num1 = 5, num2 = 10;
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
swap(num1, num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
}
#include <stdio.h>
void main() {
int num = 10;
float pi = 3.14;
char letter = 'A';
49)If the value of a number (N) is entered through the keyboard. Write a program
using recursion to calculate and display factorial of number (N).
#include <stdio.h>
void main() {
int num;