Wednesday, April 1, 2015

C program to calculate sum of the elements of lower triangle of a m*n matrix by using dynamic memory allocation.

Write a ‘C’ program to calculate sum of the elements of lower triangle of a m*n matrix by using dynamic memory allocation.

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void main()
{
int n,m,i,j,sum=0;
int **p;
clrscr();
printf("\nENTER THE ORDER OF MATRIX");
scanf("%d%d",&m,&n);
p=(int**)malloc(sizeof(int*)*m);
for(i=0;i<m;i++)
{
p[i]=(int*)malloc(sizeof(int)*n);
}
printf("\nENTER THE MATRIX ELEMENT\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&p[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
{
sum=sum+p[i][j];
}
}
}
printf("\nSUM OF LOWER TRIANGLE OF MATRIX IS :%d",sum);
getch();
}

C program to accept n numbers from user, store these numbers into an array and calculate average of n numbers.

Write a ‘C’ program to accept ‘n’ numbers from user, store these numbers into an array. And calculate average of ‘n’ numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,sum=0;
float avr;
clrscr();
printf("\nENTER THE RANGE VALUE");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
avr=sum/n;
printf("%f",avr);
getch();
}

C program to display the alternate characters from an existing file

Write a ‘C’ program to display the alternate characters from an existing file.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
int i=0;
clrscr();
fp=fopen("a.txt","r");
if(fp==NULL)
{
printf("\nFILE DOES NOT EXIST");
exit(0);
}
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
 break;
if(i%2==0)
{
printf("%c",ch);
}
i++;
}
fclose(fp);
getch();
}

C program to calculate sum of non_diagonal elements of an n*n matrix.

Write a ‘C’ program to calculate sum of non_diagonal elements of an n*n matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j,sum=0;
clrscr();
printf("\nENTER THE 3 X 3 MATRIX\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(i!=j)
{
sum=sum+a[i][j];
}
}
}
printf("\n SUM OF NON-DIAGONAL ELEMENTS OF MATRIX IS :%d",sum);
getch();
}

C program to count the number of characters, number of words and number of lines of a text file and display the result.

Write a ‘C’ program to count the number of characters, number of words and number of lines of a text file and display the result.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
int noc=0,now=0,nol=0;
clrscr();
fp=fopen("a.txt","r");
if(fp==NULL)
{
printf("file does not exist");
exit(0);
}
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
 break;
noc++;
if(ch==' ')
now++;
if(ch=='\n')
nol++;
}
printf("%d\n%d\n%d",noc,now,nol);
fclose(fp);
getch();
}

C program to accept the data from user and store that data into the text file.

Write a ‘C’ program to accept the data from user and store that data into the text file.


#include<stdio.h>
#include<conio.h>
struct emp
{
int eno;
char ename[20];
int sal;
};
void main()
{
struct emp e;
FILE *fp;
clrscr();
fp=fopen("a.txt","w");
if(fp==NULL)
{
printf("file does not exist");
exit(0);
}
else
{
while(1)
{
printf("enter the eno,ename&sal");
scanf("%d%s%d",&e.eno,&e.ename,&e.sal);
fprintf(fp,"%d%s%d",e.eno,e.ename,e.sal);
}
}
getch();
}

C program to append the contents of one file at the end of another file.

Write a ‘C’ program to append the contents of one file at the end of another file.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp,*fq;
char ch;
clrscr();
fp=fopen("a.txt","r");
fq=fopen("b.txt","a");
if(fp==NULL)
{
printf("file does not exist");
exit(0);
}
if(fq==NULL)
{
printf("file does not exist");
exit(0);
}
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
 break;
fputc(ch,fq);
}
getch();
}

Program to develop for cost saving in hotel industry

 To develop a program for cost-saving in a hotel, you can consider the following features: Key Features 1. *Room Management*: Optimize room ...