0% found this document useful (0 votes)
10 views7 pages

Programs 1

The document contains multiple C programs demonstrating various algorithms, including checking for prime numbers, linear search in an array, identifying senior citizens based on age and gender, generating Floyd's triangle, and finding the transpose of a matrix. Each program includes user input prompts and outputs corresponding results based on the input. The document serves as a practical guide for implementing basic programming concepts in C.

Uploaded by

Likith K
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)
10 views7 pages

Programs 1

The document contains multiple C programs demonstrating various algorithms, including checking for prime numbers, linear search in an array, identifying senior citizens based on age and gender, generating Floyd's triangle, and finding the transpose of a matrix. Each program includes user input prompts and outputs corresponding results based on the input. The document serves as a practical guide for implementing basic programming concepts in C.

Uploaded by

Likith K
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

3.

Program for Prime number

#include <stdio.h>

int main() {

int num, i, flag = 0;

printf("Enter a number: ");

scanf("%d", &num);

if (num <= 1) {

printf("%d is not a prime number\n", num);

else {

for (i = 2; i <= num / 2; i++) {

if (num % i == 0) {

flag = 1;

break;

if (flag == 0)

printf("%d is a prime number\n", num);

else

printf("%d is not a prime number\n", num);

return 0;

Output

Enter a number: 11
11 is a prime number

[Link] a program to find key elements in an array using linear search.

#include <stdio.h>

int main() {

int arr[100], n, key, i, found = 0;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter the elements of the array:\n");

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

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

printf("Enter the key element to search: ");

scanf("%d", &key);

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

if(arr[i] == key) {

printf("Key element found at position %d\n", i + 1);

found = 1;

break;

if(found == 0) {

printf("Key element not found in the array\n");

}
return 0;

Output

Enter number of elements: 5

Enter elements: 10 25 30 45 50

Enter key element: 30

Key element found at position 3

5.#include <stdio.h>

int main() {

int age;

char gender;

printf("Enter gender (M/F): ");

scanf(" %c", &gender);

printf("Enter age: ");

scanf("%d", &age);

if ((gender == 'M' || gender == 'm') && age >= 60) {

printf("Male Senior Citizen\n");

else if ((gender == 'F' || gender == 'f') && age >= 58) {

printf("Female Senior Citizen\n");

else {

printf("Not a Senior Citizen\n");

}
return 0;

output

Enter gender (M/F): F

Enter age: 60

Female Senior Citizen

[Link] Floyd’s triangle for given rows.

#include <stdio.h>

int main() {

int rows, i, j, num = 1;

printf("Enter number of rows: ");

scanf("%d", &rows);

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

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

printf("%d ", num);

num++;

printf("\n");

return 0;

}
[Link] for Floyds

#include <stdio.h>

int main() {

int rows, i, j, num = 1;

printf("Enter the number of rows: ");

scanf("%d", &rows);

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

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

printf("%d ", num);

num++;

printf("\n");

return 0;

Output

If user enters 5

23

456

7 8 9 10

11 12 13 14 15
[Link] for transpose of matrix

#include <stdio.h>

int main() {

int rows, cols, i, j;

printf("Enter number of rows and columns: ");

scanf("%d %d", &rows, &cols);

int matrix[rows][cols], transpose[cols][rows];

// Input matrix

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

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

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

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

// Finding transpose

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

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

transpose[j][i] = matrix[i][j];

// Display original matrix

printf("\nOriginal Matrix:\n");

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

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

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


}

printf("\n");

// Display transpose matrix

printf("\nTranspose Matrix:\n");

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

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

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

printf("\n");

return 0;

Output

Enter number of rows and columns: 2 3

Enter elements of the matrix:

123

456

Original Matrix:

123

456

Transpose Matrix:

14

25

36

You might also like