Jan 30, 2014

C program for finding the largest number in an array


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

void main()
{
int *arr, i, j, n, LARGE;
clrscr();
printf(“Enter the number of elements in the array”);
scanf(“%d”, &n);
arr=(int*) malloc(sizeof(int)*n);
for(i=0; i<n; i++)
{
printf(“Enter a number”);
scanf(“%d”, &arr[i]);
}
LARGE=arr[0];
for(i=1; i<n;i++)
{
if(arr[i]>LARGE)
LARGE=arr[i];
}
printf(“The largest number in the array is : %d”, LARGE);
getch();
}

C program for sorting the elements of an array in descending order.


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

void main()
{
int *arr, temp, i, j, n;
clrscr();
printf(“enter the number of elements in the array”);
scanf(“%d”, &n);
arr=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
for(j=i+1; j<n; j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
printf(“Elements of array in descending order are”);
for(i=0; i<n; i++);
getch();
}

C program to find number of Occurrences of Vowels, Consonants, Words, Spaces and Special Characters in the Given Statement.


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


void main()
{
char s[100];
int vow=0,cons=0,spc=0,punc=0,l,i;
clrscr();
printf(“enter the statement\n”);
gets(s);
l=strlen(s);
for(i=0;i<l;i++)
{
if(isalpha(s[i]))
{
if(s[i]==’a’||s[i]==’e’||s[i]==’i’||s[i]==’o’||s[i]==’u’)
{
vow++;
}
else
{
cons++;
}
}
if(isspace(s[i])
{
spc++;
}
if(ispunct(s[i]))
{
punc++;
}
}
printf(“\nno.of words=%d”,spc+1);
printf(“\nno.of vowels=%d”,vow);
printf(“\nno.of consonants=%d”,cons);
printf(“\nno.of space=%d”,spc);
printf(“\nno.on special characters=%d”,punc);
getch();
}

C Program to find subtraction of two matrices.


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

void main()
{
int a[5],b[5],c[5],i;
clrscr();
printf(“enter value for array a ”);
for(i=0;i<5;i++)
scanf(“%d”,&a[i]);
printf(“enter value for array b ”);
for(i=0;i<5;i++)
scanf(“%d”,&b[i]);
for(i=0;i<5;i++)
c[i]=a[i]-b[i];
printf(“subtraction”);
for(i=0;i<5;i++)
printf(“ %d ”,c[i]);
getch();
}


Output:


enter value for array a: 7
8
9
4
5
enter value for array b: 4
5
6
1
2
subtraction 3 3 3 3 3

Jan 29, 2014

C Program to find sum of two matrices



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

void main()
{

int a[3][2],b[3][2],c[3][2],i,j;
clrscr();

printf(“Enter value for 1 matrix: ”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&a[i][j]);
}
printf(“Enter value for 2 matrix: ”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
}
printf(“Sum of matrix is\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
getch();

}

Output:

Enter value for 1 matrix: 1
2
3
4
5
6
Enter value for 2 matrix: 4
5
6
1
3
2
Sum of matrix is
5 7
9 5
8 8