0% found this document useful (0 votes)
119 views20 pages

C Program Assignment

The document contains 20 C program examples including programs to: 1) Multiply two matrices 2) Check if an input character is a vowel 3) Check if a year is a leap year The programs cover basic concepts like loops, arrays, strings, functions etc. and solve common problems like reversing a number, finding factorial, Fibonacci series etc.

Uploaded by

sachin
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)
119 views20 pages

C Program Assignment

The document contains 20 C program examples including programs to: 1) Multiply two matrices 2) Check if an input character is a vowel 3) Check if a year is a leap year The programs cover basic concepts like loops, arrays, strings, functions etc. and solve common problems like reversing a number, finding factorial, Fibonacci series etc.

Uploaded by

sachin
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
You are on page 1/ 20

C PROGRAM ASSIGNMENT

Contents

1. Write a program to find the multiplication of two matrices.3

2. Write a program to check whether input alphabet is a vowel or not..................................4


3. Write a program to check leap year....................................................................................5
4. Write a program to find factorial using for loop.................................................................5
5. Write a program to find hcf and lcm ..................................................................................6
6. Write a program to convert decimal to binary....................................................................7
7. Write a program to swap two numbers...............................................................................7
8. Write a program to reverse a number..................................................................................8
9. Write a program to print diamond pattern..........................................................................9
10. Write a program to find fibonacci series.......................................................................10
11. Write a program for addition of two numbers using pointers.......................................11
12. Write a program to find maximum or largest element present in an array....................11
13. Write a program to reverse an array..............................................................................12
14. Write a program to add two matrices............................................................................13
15. Write a program to transpose of a matrix......................................................................14
16. Write a program to prints length of string.....................................................................15
17. Write a program to remove spaces or excess blanks from a string...............................16
18. Write a program add two complex numbers.................................................................17
19. Write a program to swap strings....................................................................................18
20. Write a program to delete an element from an array.....................................................19

1
1. Write a program to find the multiplication of two matrices.
Program
#include <stdio.h>

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

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


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter the number of rows and columns of second matrix\n");


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

if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");

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


for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

printf("Product of entered matrices:-\n");

for (c = 0; c < m; c++) {

2
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;
}

Output

2. Write a program to check whether input alphabet is a vowel or not.

Program

#include <stdio.h>

int main()
{
char ch;

printf("Enter a character\n");
scanf("%c", &ch);

if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' ||


ch == 'u' || ch == 'U')
printf("%c is a vowel.\n", ch);
else
printf("%c is not a vowel.\n", ch);

3
return 0;
}

Output

3. Write a program to check leap year.

Program

#include <stdio.h>

int main()
{
int year;

printf("Enter a year to check if it is a leap year\n");


scanf("%d", &year);

if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);

return 0;
}

Output

4
4. Write a program to find factorial using for loop.

Program

#include <stdio.h>

int main()
{
int c, n, fact = 1;

printf("Enter a number to calculate it's factorial\n");


scanf("%d", &n);

for (c = 1; c <= n; c++)


fact = fact * c;

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

return 0;
}

Output

5. Write a program to find hcf and lcm .

Program

#include <stdio.h>

int main() {
int a, b, x, y, t, gcd, lcm;

printf("Enter two integers\n");


scanf("%d%d", &x, &y);

a = x;
b = y;

while (b != 0) {
t = b;
b = a % b;
a = t;
}

5
gcd = a;
lcm = (x*y)/gcd;

printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);


printf("Least common multiple of %d and %d = %d\n", x, y, lcm);

return 0;
}

Output

6. Write a program to convert decimal to binary.

Program

#include <stdio.h>

int main()
{
int n, c, k;

printf("Enter an integer in decimal number system\n");


scanf("%d", &n);

printf("%d in binary number system is:\n", n);

for (c = 31; c >= 0; c--)


{
k = n >> c;

if (k & 1)
printf("1");
else
printf("0");
}

printf("\n");

return 0;
}

Output

6
7. Write a program to swap two numbers.

Program

#include <stdio.h>

int main()
{
int x, y, temp;

printf("Enter the value of x and y\n");


scanf("%d%d", &x, &y);

printf("Before Swapping\nx = %d\ny = %d\n",x,y);

temp = x;
x = y;
y = temp;

printf("After Swapping\nx = %d\ny = %d\n",x,y);

return 0;
}

Output

8. Write a program to reverse a number.

Program

#include <stdio.h>

int main()
{
int n, reverse = 0;

printf("Enter a number to reverse\n");

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

while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}

printf("Reverse of entered number is = %d\n", reverse);

return 0;
}

Output

9. Write a program to print diamond pattern.

Program

#include <stdio.h>

int main()
{
int n, c, k, space = 1;

printf("Enter number of rows\n");


scanf("%d", &n);

space = n - 1;

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


{
for (c = 1; c <= space; c++)
printf(" ");

space--;

for (c = 1; c <= 2*k-1; c++)


printf("*");

printf("\n");
}

8
space = 1;

for (k = 1; k <= n - 1; k++)


{
for (c = 1; c <= space; c++)
printf(" ");

space++;

for (c = 1 ; c <= 2*(n-k)-1; c++)


printf("*");

printf("\n");
}

return 0;
}

Output

10. Write a program to find fibonacci series.

Program

#include<stdio.h>

int main()
{
int n, first = 0, second = 1, next, c;

printf("Enter the number of terms\n");


scanf("%d",&n);

9
printf("First %d terms of Fibonacci series are :-\n",n);

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


{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}

return 0;
}

Output

11. Write a program for addition of two numbers using pointers.

Program

#include <stdio.h>

int main()
{
int first, second, *p, *q, sum;

printf("Enter two integers to add\n");


scanf("%d%d", &first, &second);

p = &first;
q = &second;

sum = *p + *q;

10
printf("Sum of entered numbers = %d\n",sum);

return 0;
}

Output

12. Write a program to find maximum or largest element present in an


array.

Program

#include <stdio.h>

int main()
{
int array[100], maximum, size, c, location = 1;

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


scanf("%d", &size);

printf("Enter %d integers\n", size);

for (c = 0; c < size; c++)


scanf("%d", &array[c]);

maximum = array[0];

for (c = 1; c < size; c++)


{
if (array[c] > maximum)
{
maximum = array[c];
location = c+1;
}
}

printf("Maximum element is present at location %d and it's value is %d.\n", location,


maximum);
return 0;
}

11
Output

13. Write a program to reverse an array.

Program

#include <stdio.h>

int main()
{
int n, c, d, a[100], b[100];

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


scanf("%d", &n);

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

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


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

for (c = n - 1, d = 0; c >= 0; c--, d++)


b[d] = a[c];

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


a[c] = b[c];

printf("Reverse array is\n");

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


printf("%d\n", a[c]);

return 0;
}

Output

12
14. Write a program to add two matrices.

Program

#include <stdio.h>

int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];

printf("Enter the number of rows and columns of matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

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


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

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

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


for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);

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

for (c = 0; c < m; c++) {


for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}

return 0;
}

13
Output

15. Write a program to transpose of a matrix.

Program

#include <stdio.h>

int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];

printf("Enter the number of rows and columns of matrix\n");


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

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

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


for(d = 0; d < n; d++)
scanf("%d",&matrix[c][d]);

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


for( d = 0 ; d < n ; d++ )
transpose[d][c] = matrix[c][d];

printf("Transpose of entered matrix :-\n");

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


for (d = 0; d < m; d++)
printf("%d\t",transpose[c][d]);
printf("\n");
}

return 0;
}

14
Output

16. Write a program to prints length of string.

Program

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

int main()
{
char a[100];
int length;

printf("Enter a string to calculate it's length\n");


gets(a);

length = strlen(a);

printf("Length of entered string is = %d\n",length);

return 0;
}

Output

17. Write a program to remove spaces or excess blanks from a string.

Program

#include <stdio.h>

int main()

15
{
char text[1000], blank[1000];
int c = 0, d = 0;

printf("Enter some text\n");


gets(text);

while (text[c] != '\0') {


if (text[c] == ' ') {
int temp = c + 1;
if (text[temp] != '\0') {
while (text[temp] == ' ' && text[temp] != '\0') {
if (text[temp] == ' ') {
c++;
}
temp++;
}
}
}
blank[d] = text[c];
c++;
d++;
}

blank[d] = '\0';

printf("Text after removing blanks\n%s\n", blank);

return 0;
}

Output

18. Write a program add two complex numbers.

Program

#include <stdio.h>

struct complex
{

16
int real, img;
};

int main()
{
struct complex a, b, c;

printf("Enter a and b where a + ib is the first complex number.\n");


printf("a = ");
scanf("%d", &a.real);
printf("b = ");
scanf("%d", &a.img);
printf("Enter c and d where c + id is the second complex number.\n");
printf("c = ");
scanf("%d", &b.real);
printf("d = ");
scanf("%d", &b.img);

c.real = a.real + b.real;


c.img = a.img + b.img;

if ( c.img >= 0 )
printf("Sum of two complex numbers = %d + %di\n", c.real, c.img);
else
printf("Sum of two complex numbers = %d %di\n", c.real, c.img);

return 0;
}

Output

19. Write a program to swap strings.

Program

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

17
#include <malloc.h>

int main()
{
char first[100], second[100], *temp;

printf("Enter the first string\n");


gets(first);

printf("Enter the second string\n");


gets(second);

printf("\nBefore Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n\n",second);

temp = (char*)malloc(100);

strcpy(temp,first);
strcpy(first,second);
strcpy(second,temp);

printf("After Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n",second);

return 0;
}

Output

18
20. Write a program to delete an element from an array.

Program

#include <stdio.h>

int main()
{
int array[100], position, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d elements\n", n);

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


scanf("%d", &array[c]);

printf("Enter the location where you wish to delete element\n");


scanf("%d", &position);

if ( position >= n+1 )


printf("Deletion not possible.\n");
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];

printf("Resultant array is\n");

for( c = 0 ; c < n - 1 ; c++ )


printf("%d\n", array[c]);
}

return 0;
}

Output

19
20

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy