Swapping of two numbers using pointers
#include<stdio.h>
void swap(int*,int*);
int main() {
int num1, num2;
printf("\nEnter the first number : ");
scanf("%d", &num1);
printf("\nEnter the Second number : ");
scanf("%d", &num2);
swap(&num1, &num2);
printf("\nFirst number : %d", num1);
printf("\nSecond number : %d", num2);
return (0);
}
void swap(int *num1, int *num2) {
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}
Enter the first number : 12
Enter the Second number : 22
First number : 22
Second number : 12
Factorial of number using recursive function
#include <stdio.h>
int fact(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, fact(n));
return 0;
}
int fact(int n)
{
if (n >= 1)
return n*fact(n-1);
else
return 1;
}
Enter a positive integer: 6
Factorial of 6 = 720
Finding GCD of two numbers using recursive function
#include <stdio.h>
int gcd(int n1, int n2);
int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, gcd(n1,n2));
return 0;
}
int gcd(int n1, int n2)
{
if (n2 != 0)
return gcd(n2, n1%n2);
else
return n1;
}
Enter two positive integers: 366
60
G.C.D of 366 and 60 is 6.
find LCM of two numbers using recursion
#include <stdio.h>
/* Function declaration */
int lcm(int a, int b);
int main()
{
int num1, num2, LCM;
/* Input two numbers from user */
printf("Enter any two numbers to find lcm: ");
scanf("%d%d", &num1, &num2);
if(num1 > num2)
LCM = lcm(num2, num1);
else
LCM = lcm(num1, num2);
printf("LCM of %d and %d = %d", num1, num2, LCM);
return 0;
}
int lcm(int a, int b)
{
static int multiple = 0;
multiple += b;
if((multiple % a == 0) && (multiple % b == 0))
{
return multiple;
}
else
{
return lcm(a, b);
}
}
Enter any two numbers to find lcm
LCM of 12 and 30 = 60
Length of the string without using string functions
#include <stdio.h>
int main()
{
char s[1000];
int i;
printf("Enter a string: ");
scanf("%s", s);
for(i = 0; s[i] != '\0'; ++i);
printf("Length of string: %d", i);
return 0;
}
Enter a string: Programming
Length of string: 11
Reversing the string without using string functions
#include<stdio.h>
#include<string.h>
int main() {
char str[100], temp;
int i, j = 0;
printf("\nEnter the string :");
gets(str);
i = 0;
j = strlen(str) - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("\nReverse string is :%s", str);
return (0);
}
Enter the string : Pritesh
Reverse string is : hsetirP
Write a Program to count vowels, consonants by entering line of characters
#include <stdio.h>
int main()
{
char line[150];
int i, vowels, consonants, digits, spaces;
vowels = consonants = digits = spaces = 0;
printf("Enter a line of string: ");
scanf("%[^\n]", line);
for(i=0; line[i]!='\0'; ++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
++consonants;
}
else if(line[i]>='0' && line[i]<='9')
{
++digits;
}
else if (line[i]==' ')
{
++spaces;
}
}
printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);
return 0;
}
Output
Enter a line of string: adfslkj34 34lkj343 34lk
Vowels: 1
Consonants: 11
Digits: 9
White spaces: 2
WAP to read matrix and find sum of its diagonals
/* C Program to find Sum of Diagonal Elements of a Matrix */
#include<stdio.h>
int main()
{
int i, j, rows, columns, a[10][10], Sum = 0;
printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);
printf("\n Please Enter the Matrix Elements \n");
for(rows = 0; rows < i; rows++)
{
scanf("%d %d", &i, &j);
for(columns = 0;columns < j;columns++)
{
scanf("%d", &a[rows][columns]);
}
}
for(rows = 0; rows < i; rows++)
{
Sum = Sum + a[rows][rows];
}
printf("\n The Sum of Diagonal Elements of a Matrix = %d", Sum );
return 0;
}
OUTPUT
Please Enter Number of rows and columns : 3 3
Please Enter the Matrix Elements
1 2 3
4 5 6
7 8 9
The Sum of Diagonal Elements of a Matrix=15
Example: Program to Find Transpose of a Matrix
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
// Storing elements of the matrix
printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
// Displaying the matrix a[][] */
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
printf("\n");
}
// Finding the transpose of matrix a
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
// Displaying the transpose of matrix a
printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);
)
printf("\n")
}
return 0;
}
Output
Enter rows and columns of matrix: 2
3
Enter element of matrix:
Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 6
Enter element a23: 4
Entered Matrix:
2 3 4
5 6 4
Transpose of Matrix:
2 5
3 6
4 4
Matrix multiplication in C language
#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 number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter 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 the matrices:\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
Example: Program to Find sum of even and odd elements in given matrix
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j,se=0,so=0;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
// Storing elements of the matrix
printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
// Displaying the matrix a[][] */
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
printf("\n");
}
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
if(a[i][j]%2==0)
se=se+a[I][j];
else
so=so+a[I][j];
}
printf("\nsum of even and odd elements of matrix%d %d\n",se,so);
return 0;
}
Output
Enter rows and columns of matrix: 2
3
Enter element of matrix:
Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 6
Enter element a23: 4
Entered Matrix:
2 3 4
5 6 4
nsum of even and odd elements of matrix 16 07