0% found this document useful (0 votes)
62 views9 pages

CP Answers

The document contains multiple C programming code snippets that demonstrate various functionalities such as gross salary calculation, classifying student marks, checking Armstrong numbers, performing matrix operations, searching in arrays, and basic arithmetic operations using switch statements. Each code snippet is self-contained and includes user input prompts and output statements. The programs cover a range of topics suitable for beginners in programming.

Uploaded by

yas.she.rt24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views9 pages

CP Answers

The document contains multiple C programming code snippets that demonstrate various functionalities such as gross salary calculation, classifying student marks, checking Armstrong numbers, performing matrix operations, searching in arrays, and basic arithmetic operations using switch statements. Each code snippet is self-contained and includes user input prompts and output statements. The programs cover a range of topics suitable for beginners in programming.

Uploaded by

yas.she.rt24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Gross Salary Calculation


#include <stdio.h>

int main() {

float basic_salary, da, hra, gross_salary;

printf("Enter Basic Salary: ");

scanf("%f", &basic_salary);

da = 0.5 * basic_salary;

hra = 0.4 * basic_salary;

gross_salary = basic_salary + da + hra;

printf("Gross Salary = %.2f\n", gross_salary);

return 0;

2. Class of Students Using If-Else Ladder


#include <stdio.h>

int main() {

int marks;

printf("Enter marks: ");

scanf("%d", &marks);

if (marks >= 70 && marks <= 100)

printf("Distinction\n");

else if (marks >= 60 && marks <= 69)

printf("First Class\n");

else if (marks >= 40 && marks <= 59)

printf("Second Class\n");

else

printf("FAIL\n");

return 0;

}
3. Armstrong Number (3-digit)
#include <stdio.h>

int main() {

int num, original, remainder, result = 0;

printf("Enter a 3-digit number: ");

scanf("%d", &num);

original = num;

while (original != 0) {

remainder = original % 10;

result += remainder * remainder * remainder;

original /= 10;

if (result == num)

printf("%d is an Armstrong number.\n", num);

else

printf("%d is not an Armstrong number.\n", num);

return 0;

4. Square Root, Square, and Factorial using Switch


#include <stdio.h>

#include <math.h>

int main() {

int choice, num, i, fact = 1;

printf("1. Square Root\n2. Square\n3. Factorial\nEnter choice: ");

scanf("%d", &choice);

printf("Enter number: ");

scanf("%d", &num);

switch (choice) {
case 1:

printf("Square root = %.2f\n", sqrt(num));

break;

case 2:

printf("Square = %d\n", num * num);

break;

case 3:

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

fact *= i;

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

break;

default:

printf("Invalid choice.\n");

return 0;

5. Square Root, Prime Check, Cube using Switch


#include <stdio.h>

#include <math.h>

int main() {

int choice, num, i, isPrime = 1;

printf("1. Square Root\n2. Prime Check\n3. Cube\nEnter choice: ");

scanf("%d", &choice);

printf("Enter number: ");

scanf("%d", &num);

switch (choice) {

case 1:

printf("Square root = %.2f\n", sqrt(num));

break;
case 2:

if (num <= 1)

isPrime = 0;

for (i = 2; i <= num / 2; 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);

break;

case 3:

printf("Cube = %d\n", num * num * num);

break;

default:

printf("Invalid choice.\n");

return 0;

6. Addition of Two 3x3 Matrices


#include <stdio.h>

int main() {

int a[3][3], b[3][3], sum[3][3], i, j;

printf("Enter elements of first matrix:\n");

for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

scanf("%d", &a[i][j]);
printf("Enter elements of second matrix:\n");

for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

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

printf("Sum of matrices:\n");

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

for (j = 0; j < 3; j++) {

sum[i][j] = a[i][j] + b[i][j];

printf("%d ", sum[i][j]);

printf("\n");

return 0;

7. Transpose of a 3x3 Matrix


#include <stdio.h>

int main() {

int a[3][3], transpose[3][3], i, j;

printf("Enter matrix elements:\n");

for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

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

printf("Transpose:\n");

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

for (j = 0; j < 3; j++) {

transpose[j][i] = a[i][j];

printf("%d ", transpose[j][i]);


}

printf("\n");

return 0;

8. Subtraction of Two 3x3 Matrices


#include <stdio.h>

int main() {

int a[3][3], b[3][3], diff[3][3], i, j;

printf("Enter elements of first matrix:\n");

for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

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

printf("Enter elements of second matrix:\n");

for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

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

printf("Difference of matrices:\n");

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

for (j = 0; j < 3; j++) {

diff[i][j] = a[i][j] - b[i][j];

printf("%d ", diff[i][j]);

printf("\n");

return 0;

}
9. Search an Element in Array Using Function
#include <stdio.h>

int search(int arr[], int n, int key) {

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

if (arr[i] == key)

return i;

return -1;

int main() {

int arr[100], n, key, pos;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements:\n");

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

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

printf("Enter element to search: ");

scanf("%d", &key);

pos = search(arr, n, key);

if (pos != -1)

printf("Element found at position %d\n", pos + 1);

else

printf("Element not found\n");

return 0;

10. Addition, Subtraction, Multiplication using Switch

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

int a, b, choice;

printf("Enter two numbers: ");

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

printf("1. Add\n2. Subtract\n3. Multiply\nEnter choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Sum = %d\n", a + b);

break;

case 2:

printf("Difference = %d\n", a - b);

break;

case 3:

printf("Product = %d\n", a * b);

break;

default:

printf("Invalid choice.\n");

return 0;

11.) WAP to display pattern using for loop


#include <stdio.h>

int main()

int n, i, j;

printf("Enter number of rows: ");

scanf("%d", &n);

for(i = n; i >= 1; i--)

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

printf("*");

printf("\n");

return 0;

12. LCM and HCF of Two Numbers


#include <stdio.h>

int main() {

int a, b, hcf, lcm, i;

printf("Enter two numbers: ");

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

for (i = 1; i <= a && i <= b; i++) {

if (a % i == 0 && b % i == 0)

hcf = i;

lcm = (a * b) / hcf;

printf("HCF = %d\n", hcf);

printf("LCM = %d\n", lcm);

return 0;

You might also like