SDF ASSIGNMENT
WEEK-7 NJG250659
Q1)Write a C Program to remove all Characters in a
String except Alphabets
#include <stdio.h>
int main()
{
char str[100];
char str1[100];
printf("Enter a string to remove all characters except
alphabets: ");
gets(str);
int i=0,j=0;
while(str[i]!='\0')
{
if((str[i]>=65&&str[i]<=90)||(str[i]>=97&&str[i]<=122))
{
str1[j]=str[i];
j++;
}
i++;
}
printf("Final String: ");
puts(str1);
return 0;
}
Q2) C program to find the length of a string without
using the built-in function.
#include <stdio.h>
int main()
{
char str[100];
printf("Enter a string to count its length: ");
gets(str);
int i=0,c=0;
while(str[i]!='\0')
{
c++;
i++;
}
printf("Length: %d",c);
return 0;
}
Q3) Write a C program to multiply two square
matrices.
#include <Stdio.h>
int main()
{
int o,i,j,k;
printf("Enter the order of the matrices: ");
scanf("%d",&o);
int arr1[o][o];
int arr2[o][o];
printf("Enter the elements in the first array\n");
for(i=0;i<o;i++)
{
for(j=0;j<o;j++)
{
scanf("%d",&arr1[i][j]);
}
}
printf("Enter the elements in the second array\n");
for(i=0;i<o;i++)
{
for(j=0;j<o;j++)
{
scanf("%d",&arr2[i][j]);
}
}
int arr[o][o];
for(i=0;i<o;i++)
{
for(j=0;j<o;j++)
{
arr[i][j]=0;
for(k=0;k<o;k++)
{
arr[i][j]+=arr1[i][k]*arr2[k][j];
}
}
}
printf("First Matrix - \n");
for(i=0;i<o;i++)
{
for(j=0;j<o;j++)
{
printf("%d ",arr1[i][j]);
}
printf("\n");
}
printf("Second Matrix - \n");
for(i=0;i<o;i++)
{
for(j=0;j<o;j++)
{
printf("%d ",arr2[i][j]);
}
printf("\n");
}
printf("Matrix after multiplication - \n");
for(i=0;i<o;i++)
{
for(j=0;j<o;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
Q4) Write a program in C to read a user entered
square matrix and then display its diagonal.
#include <Stdio.h>
int main()
{
int o,i,j;
printf("Enter the order of the matrix: ");
scanf("%d",&o);
int arr[o][o];
printf("Enter the elements in the matrix\n");
for(i=0;i<o;i++)
{
for(j=0;j<o;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("\nMatrix-\n\n");
for(i=0;i<o;i++)
{
for(j=0;j<o;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
printf("\n");
printf("The diagonal elements are: ");
for(i=0;i<o;i++)
{
for(j=0;j<o;j++)
{
if(i==j)
{
printf("%d ",arr[i][j]);
}
}
}
return 0;
}
Q5) Write a C program to read elements in a matrix
and find the sum of the upper triangular matrix.
#include <Stdio.h>
int main()
{
int o,i,j,sum=0;
printf("Enter the order of the matrix: ");
scanf("%d",&o);
int arr[o][o];
printf("Enter the elements in the matrix\n");
for(i=0;i<o;i++)
{
for(j=0;j<o;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("\nMatrix-\n\n");
for(i=0;i<o;i++)
{
for(j=0;j<o;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
printf("\n");
for(i=0;i<o;i++)
{
for(j=0;j<o;j++)
{
if(j>=i)
{
sum+=arr[i][j];
}
}
}
printf("The sum of upper triangular matrix elements is
%d",sum);
return 0;
}
Q6) Write a program in C to store and display a
3-dimensional array.
#include <stdio.h>
int main()
{
int i,j,k,row,col,hei;
int arr[10][10][10];
printf("Enter the size of array in order: ");
scanf("%d %d %d",&row,&col,&hei);
printf("Enter the elements\n");
for(k=0;k<hei;k++)
{
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&arr[i][j][k]);
}
}
}
printf("Array -->\n\n");
for(k=0;k<hei;k++)
{
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%3d ",arr[i][j][k]);
}
printf("\n");
}
printf("\n\n");
}
return 0;
}
Q7)Write a C program to find the sum and average of n
numbers using arrays.
#include <stdio.h>
int main()
{
int i,n,sum=0;
printf("Enter the number of elements: ");
scanf("%d",&n);
int arr[n];
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
sum+=arr[i];
}
printf("Sum of %d numbers is %d and their average is
%0.5f",n,sum,((float)sum/n));
return 0;
}
Q9) Given an array, write a program to the left-rotate
the elements of the array by one.
#include <stdio.h>
int main()
{
int i,temp;
int arr[5];
printf("Enter the elements: \n");
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
printf("Initial Array: ");
for(i=0;i<5;i++)
{
printf("%d ",arr[i]);
}
printf("\nFinal Array: ");
temp=arr[0];
for(i=1;i<5;i++)
{
arr[i-1]=arr[i];
}
arr[5-1]=temp;
for(i=0;i<5;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
Q10)Write a C program to find the second smallest
element in a one-dimensional array.
#include <stdio.h>
int main()
{
int i,j,n,temp;
printf("Input array size: ");
scanf("%d",&n);
int arr[n];
printf("Enter the elements: \n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[i])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("Second smallest element is %d",arr[1]);
return 0;
}
Q11)Write a C Program to Find the Frequency of
Characters in a String
#include <stdio.h>
int main()
{
int i,j=0,c=0;
char ch;
char str[100];
printf("Enter a string to find the frequency of characters:
");
gets(str);
for(i=97;i<=122;i++)
{
ch=(char)i;
while(str[j]!='\0')
{
if(str[j]==ch||str[j]==ch-32)
c++;
j++;
}
if(c!=0)
{
printf("Frequency of %c is %d\n",ch,c);
c=0;
}
j=0;
}
return 0;
}
Q12) Write a C program to count the total number of
vowels and consonants in a string.
#include <stdio.h>
int main()
{
int i=0,v=0,c=0;
char ch;
char str[100];
printf("Enter a string to find the frequency of vowels and
consonants: ");
gets(str);
while(str[i]!='\0')
{
ch=str[i];
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
v++;
else
c++;
}
i++;
}
printf("Vowels = %d\nConsonants = %d",v,c);
return 0;
}
Submitted by MAANAS TIWARI