Unit 4 - Array & String
4.1 - Write a C- program reads in an array of integers and print its elements in reverse order. [S-09]
#include <stdio.h>
void main()
{
int num[100],n,i;
printf("Enter number of array elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element = ");
scanf("%d",&num[i]);
}
printf("The elements in reverse order : ");
for(i=n-1;i>=0;i--)
{
printf("\n%d",num[i]);
}
}
4.2 - Write a program in c for multiplication of two matrices. [S-15, W-16]
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("Enter the number of row = ");
scanf("%d",&r);
printf("Enter the number of column = ");
scanf("%d",&c);
printf("Enter the first matrix element : \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the second matrix element : \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Multiplication of two matrices : \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
4.3 - Write a ‘C’ program to count ODD and EVEN numbers from given 10 numbers. [S-13]
#include<stdio.h>
#include<conio.h>
int main()
{
int a[1000],i,n,even=0,odd=0;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
{
if(a[i]%2==0)
even++;
else
odd++;
}
printf("even numbers in array : %d",even);
printf("\nodd numbers in array : %d",odd);
return 0;
}
4.4 - Write a C function to exchange two numbers and use it to reverse an array of 10 integers accepted from user. [S-11]
4.5 - Write a C program to accept an array of N integers and find the largest odd number as well as largest even number and display them. [S-10]
#include<stdio.h>
int main()
{
int a[100],me,mo,n,i;
printf("\nEnter number of elements = ");
scanf("%d",&n);
printf("\nEnter the elements = ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]%2!=0)
continue;
me=a[i];
break;
}
for(;i<n;i++)
{
if(a[i]%2==0 && a[i]>me)
me=a[i];
}
for(i=0;i<n;i++)
{
if(a[i]%2==0)
continue;
mo=a[i];
break;
}
for(;i<n;i++)
{
if(a[i]%2!=0 && a[i]>mo)
mo=a[i];
}
printf("\nLargest even number = %d\n",me);
printf("\nLargest odd number = %d",mo);
}
4.6 - Write a C program to Add two 3 X 3 Matrix. [S-11]
#include <stdio.h>
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows : ");
scanf("%d", &r);
printf("Enter the number of columns : ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("\nSum of two matrices: \n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d ", sum[i][j]);
}
printf("\n");
}
}
4.7 - Write a program to find maximum element from 3 × 3 metrics. [S-10]
#include <stdio.h> int main() { int a,b,i,j,m[100][100],max; printf("Enter the number of rows : "); scanf("%d",&a); printf("Enter the number of columns : "); scanf("%d",&b); printf("Enter the elements of the matrix : \n"); for(i=0;i<a;i++) { for(j=0;j<b;j++) { scanf("%d",&m[i][j]); } } for(i=0;i<a;i++) { for(j=0;j<b;j++) { if (m[i][j] > max) max = m[i][j]; } } printf("Maximum element in the matrix is %d\n", max); return 0; }
4.8 - Write a ‘C’ program to count the number of words in a given string. [S-13]
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100];
int i, totalwords=1;
//totalwords = 1;
printf("\nEnter any String : ");
gets(str);
for(i = 0; str[i] != '\0'; i++)
{
if(str[i] == ' ' || str[i] == '\n' || str[i] == '\t')
{
totalwords++;
}
}
printf("\nThe Total Number of Words in this String %s = %d", str, totalwords);
return 0;
}
4.9 - Write a C program to accept a string and print every third character from string only if it is lowercase. [S-11]
4.10 - Write a C function to reverse a word. Use it to reverse each word of a string given by user. [S-11]
4.11 - Write a C program to reverse a string given from keyboard. Don’t use library function to reverse it. [S-10]
#include<stdio.h>
#include<conio.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);
}
4.12 - Write a program to that read two string and compare them using the function strcmp( ) and print the message that first string is equal, less, greater than the second one. [S-09]
4.13 - Write a C program to read 10 numbers from user and store them in an Array. Display Sum, Minimum and Average of the numbers. [W-13, W-19, S-18, S-20]
#include <stdio.h>
int main()
{
int a[10],i,s=0,min;
float avg;
printf("Enter 10 Numbers : \n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
s=s+a[i];
avg=s/10.0;
}
printf("Sum of Array Elements = %d\n",s);
printf("Average of Elements = %.2f\n",avg);
min=a[0];
for(i=0;i<10;i++)
if(a[i]<min)
min=a[i];
printf("Lowest Element = %d",min);
return 0;
}
4.14 - Write a program to count total words in text. [W-17]
#include <stdio.h>
#include <string.h>
void main()
{
char s[200];
int count = 0, i;
printf("Enter the string : ");
scanf("%[^\n]s", s);
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ' && s[i+1] != ' ')
count++;
}
printf("\nNumber of words in given string are : %d\n", count + 1);
}
4.15 - Write a program to find out the largest of an array. [S-17]
#include <stdio.h>
int main()
{
int n;
double arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
for (int i = 0; i < n; ++i)
{
printf("Enter number%d: ", i + 1);
scanf("%lf", &arr[i]);
}
for (int i = 1; i < n; ++i)
{
if (arr[0] < arr[i])
{
arr[0] = arr[i];
}
}
printf("Largest element = %.2lf", arr[0]);
return 0;
}
No comments:
Post a Comment
If you have any doubts or suggestions, Please let me know