0% found this document useful (0 votes)
7 views29 pages

C Programming

The document contains multiple programming tasks and their corresponding C code implementations, including categorizing senior citizens by age and gender, generating Floyd's triangle, finding the transpose of a matrix, string operations, calculating GCD and LCM, and more. Each task is accompanied by algorithms and flowcharts to illustrate the logic behind the programs. The document serves as a comprehensive guide for various programming exercises in C language.

Uploaded by

yashrajan286
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)
7 views29 pages

C Programming

The document contains multiple programming tasks and their corresponding C code implementations, including categorizing senior citizens by age and gender, generating Floyd's triangle, finding the transpose of a matrix, string operations, calculating GCD and LCM, and more. Each task is accompanied by algorithms and flowcharts to illustrate the logic behind the programs. The document serves as a comprehensive guide for various programming exercises in C language.

Uploaded by

yashrajan286
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

Shreyobhilasha K R

Dept of CS and E

5. Given age and gender of a person, develop a program to categorise senior citizen (male &
female).

#include <stdio.h>
#include <conio.h>
#include <string.h>

void main()
{
int age;
char gender[10];
clrscr();
printf("Enter the age: ");
scanf("%d", &age);

printf("Enter the gender: ");


scanf("%s", gender);

if(age >= 60)


{
if(strcmp(gender, "male") == 0 || strcmp(gender, "Male") == 0)
{
printf("Senior citizen male");
}
else if(strcmp(gender, "female") == 0 || strcmp(gender, "Female") == 0)
{
printf("Senior citizen female");
}
else
{
printf("Invalid gender");
Shreyobhilasha K R
Dept of CS and E

}
}
else
{
printf("Not a senior citizen");
}
getch();
}
Algorithm
[Link]
[Link] variables:
• Integer variable age
• Character array gender
[Link] the value of age
[Link] the value of gender
[Link] if age ≥ 60
• If true:
Compare gender with "male" or "Male"
If matched, display “Senior citizen male”
• Else compare gender with "female" or "Female"
If matched, display “Senior citizen female”
• Else display “Invalid gender”
If false, display “Not a senior citizen”
[Link]

Flow chart
Shreyobhilasha K R
Dept of CS and E
Shreyobhilasha K R
Dept of CS and E

6. Generate Floyd’s triangle for given rows.

#include <stdio.h>

#include <conio.h>

void main()

int rows, i, j, num = 1;

clrscr();

printf("Enter number of rows: ");

scanf("%d", &rows);

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

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

printf("%d ", num);

num = num + 1;

printf("\n");

getch();

Algorithm

1. Start

2. Declare integer variables: rows, i, j

3. Initialize num = 1

4. Read the number of rows

5. Repeat steps for i = 1 to rows:

- Repeat steps for j = 1 to i:

- Print the value of num


Shreyobhilasha K R
Dept of CS and E

- Increment num by 1

- Move to the next line

6. Stop

Flowchart
Shreyobhilasha K R
Dept of CS and E

7. Develop a program to find the transpose of a matrix.

#include <stdio.h>

#include <conio.h>

void main()

int a[10][10], i, j, row, col, transpose[10][10];

clrscr();

printf("Enter the number of rows");

scanf("%d", &row);

printf("Enter the number of columns");

scanf("%d", &col);

printf("Enter the elements of matrix a:");

// elements of matrix a

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

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

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

// transpose of a matrix

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

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

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

}
Shreyobhilasha K R
Dept of CS and E

// display original matrix

printf("The original matrix is \n");

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

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

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

printf("\n");

// display the transpose of matrix

printf("The transpose matrix is\n");

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

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

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

printf("\n");

getch();

Algorithm

1. Start

2. Declare matrix a, transpose, and variables row, col, i, j

3. Read number of rows and columns


Shreyobhilasha K R
Dept of CS and E

4. Read all elements of matrix a

5. Find transpose:

- for i = 1 to row:

- for j = 1 to col:

- transpose[j][i] = a[i][j]

6. Display original matrix a

7. Display transpose matrix

8. Stop

Flowchart
Shreyobhilasha K R
Dept of CS and E
Shreyobhilasha K R
Dept of CS and E

8. Develop a program to concatenate two strings, find length of a string and copy one string
to other using string operations.

#include <stdio.h>

#include <string.h>

void main()

char string1[10], string2[10], string3[10];

int length;

clrscr();

printf("Enter the string1 and string2");

scanf("%s%s", string1, string2);

strcat(string1, string2);

printf("The concatenation of two string is %s\n", string1);

length = strlen(string1);

printf("The length of string1 is %d\n", length);

strcpy(string3, string1);

printf("The copy string is %s\n", string3);

getch();

Algorithm

1. Start

2. Declare character arrays string1, string2, string3

3. Declare integer variable length

4. Read two strings string1 and string2

5. Concatenate string2 to string1 using strcat(string1, string2)

6. Display the concatenated string (string1)


Shreyobhilasha K R
Dept of CS and E

7. Find length of string1 using length = strlen(string1)

8. Display the length

9. Copy string1 into string3 using strcpy(string3, string1)

10. Display the copied string (string3)

11. Stop

Flowchart
Shreyobhilasha K R
Dept of CS and E
Shreyobhilasha K R
Dept of CS and E

9. Develop a modular program to find GCD and LCM of given numbers.

#include <stdio.h>

#include <conio.h>

/* Function prototype */

void findLCM(int a, int b, int gcd);

void findGCD(int a, int b)

int i, gcd = 1;

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

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

gcd = i;

printf("The GCD is: %d\n", gcd);

findLCM(a, b, gcd);

void findLCM(int a, int b, int gcd)

int lcm;

lcm = (a * b) / gcd;

printf("The LCM is: %d\n", lcm);


Shreyobhilasha K R
Dept of CS and E

void main()

int num1, num2;

clrscr();

printf("Enter two numbers:\n");

scanf("%d %d", &num1, &num2);

findGCD(num1, num2);

getch();

Algorithm

1. Start

2. Read two numbers num1 and num2

3. Call findGCD(num1, num2)

4. In findGCD():

- Initialize gcd = 1

- Repeat i from 1 to minimum of num1 and num2:

- If both num1 and num2 are divisible by i, update gcd = i

- Display GCD = gcd

- Call findLCM(num1, num2, gcd)

5. In findLCM():

- Calculate lcm = (num1 * num2) / gcd

- Display LCM = lcm

6. Stop

Flowchart
Shreyobhilasha K R
Dept of CS and E
Shreyobhilasha K R
Dept of CS and E

10. Develop a program to declare the structure of employees and display the employee
records with higher salary among two employees.

#include <stdio.h>

#include <conio.h>

struct employee

int emp_id;

char emp_name[20];

float emp_salary;

};

void main()

struct employee emp1, emp2;

clrscr();

printf("Enter the details of Employee 1:\n");

printf("Emp ID: ");

scanf("%d", &emp1.emp_id);

printf("Emp Name: ");

scanf("%s", emp1.emp_name);

printf("Emp Salary: ");

scanf("%f", &emp1.emp_salary);

printf("\nEnter the details of Employee 2:\n");

printf("Emp ID: ");

scanf("%d", &emp2.emp_id);

printf("Emp Name: ");

scanf("%s", emp2.emp_name);
Shreyobhilasha K R
Dept of CS and E

printf("Emp Salary: ");

scanf("%f", &emp2.emp_salary);

if(emp1.emp_salary > emp2.emp_salary)

printf("\n\nDetails of employee with higher salary are as follows:\n");

printf("Id: %d\n", emp1.emp_id);

printf("Name: %s\n", emp1.emp_name);

printf("Salary: %.4f\n", emp1.emp_salary);

else

printf("\n\nDetails of employee with higher salary are as follows:\n");

printf("Id: %d\n", emp2.emp_id);

printf("Name: %s\n", emp2.emp_name);

printf("Salary: %.4f\n", emp2.emp_salary);

getch();

Algorithm

1. Start

2. Declare structure employee with members:

- emp_id (integer)

- emp_name (character array)

- emp_salary (float)

3. Declare two structure variables emp1 and emp2

4. Read details of Employee 1 (emp1):


Shreyobhilasha K R
Dept of CS and E

- emp_id

- emp_name

- emp_salary

5. Read details of Employee 2 (emp2):

- emp_id

- emp_name

- emp_salary

6. Compare salaries:

- If emp1.emp_salary > emp2.emp_salary

- Display details of emp1

- Else

- Display details of emp2

7. Stop

Flowchart
Shreyobhilasha K R
Dept of CS and E

11. Develop a program to add two numbers using the pointers to the variables.

/* Program to add two numbers using the pointers to the variables */

#include <stdio.h>

#include <conio.h>

void main()

{
Shreyobhilasha K R
Dept of CS and E

int a, b, sum;

int *p1, *p2;

clrscr();

printf("Enter the values of a and b:");

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

p1 = &a;

p2 = &b;

sum = *p1 + *p2;

printf("The addition of %d and %d is %d", a, b, sum);

getch();

Algorithm

1. Start

2. Declare integer variables a, b, and sum

3. Declare pointer variables p1 and p2

4. Read values of a and b

5. Store address of a in pointer p1 (p1 = &a)

6. Store address of b in pointer p2 (p2 = &b)

7. Add values pointed by p1 and p2: sum = *p1 + *p2

8. Display the result (sum)

9. Stop

Flowchart
Shreyobhilasha K R
Dept of CS and E

12. Develop a program to find the sum of digits of a given number.

#include <stdio.h>

#include <conio.h>

void main()

int num, sum = 0, rem;

clrscr();

printf("Enter the number:");

scanf("%d", &num);
Shreyobhilasha K R
Dept of CS and E

while(num != 0)

rem = num % 10;

sum = sum + rem;

num = num / 10;

printf("%d", sum);

getch();

Algorithm

1. Start

2. Declare variables num, sum, and rem

3. Initialize sum = 0

4. Read the value of num

5. While num is not equal to 0:

- rem = num % 10

- sum = sum + rem

- num = num / 10

6. Loop ends

7. Display the value of sum

8. Stop

Flowchart
Shreyobhilasha K R
Dept of CS and E

13. Develop a program to perform Matrix Multiplication.

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

void main()

int m, n, p, q, i, j, k;

int a[10][10], b[10][10], c[10][10];


Shreyobhilasha K R
Dept of CS and E

clrscr();

printf("Enter the size of matrix A\n");

scanf("%d%d", &m, &n);

printf("Enter the size of matrix B\n");

scanf("%d%d", &p, &q);

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

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

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

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

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

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

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

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

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

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

c[i][j] = 0;
Shreyobhilasha K R
Dept of CS and E

if(n != p)

printf("Matrix multiplication is not possible\n");

exit(0);

else

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

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

for(k = 0; k < n; k++)

c[i][j] = c[i][j] + (a[i][k] * b[k][j]);

printf("The elements of matrix A are:\n");

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

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

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

}
Shreyobhilasha K R
Dept of CS and E

printf("\n");

printf("The elements of matrix B are:\n");

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

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

printf("%d\t", b[i][j]);

printf("\n");

printf("The multiplication of Matrix A and Matrix B is Matrix C\n");

printf("The elements of matrix C are:\n");

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

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

printf("%d\t", c[i][j]);

printf("\n");

getch();

Algorithm

1. Start

2. Declare matrices A, B, C and variables m, n, p, q, i, j, k

3. Read rows and columns of A (m, n) and B (p, q)

4. Read elements of A and B


Shreyobhilasha K R
Dept of CS and E

5. If n ≠ p, display "Not possible" and stop

6. Initialize C elements to 0

7. For i = 0 to m:

- For j = 0 to q:

- For k = 0 to n:

- C[i][j] = C[i][j] + A[i][k] * B[k][j]

8. Display A, B, and C

9. Stop

Flowchart
Shreyobhilasha K R
Dept of CS and E
Shreyobhilasha K R
Dept of CS and E

sss

You might also like