Sunday, December 2, 2012

Stair case of numbers using FOR loop in C++


#include<iostream.h>
#include<conio.h>
int main()
{
int i,j,c;
clrscr();
cout<<"Enter the lines"<<"\n";
cin>>c;
for(i=0;i<c;i++)
{
for(j=0;j<=i;j++)
{
cout<<i;
}
cout<<"\n";
}
return 0;
}

Swapping using reference variables as arguments in C++


#include<iostream.h>
#include<conio.h>
void swap(int &,int &);
int main()
{
int a,b;
clrscr();
cout<<"enter the values"<<"\n";
cin>>a>>b;
cout<<"Values of a and b before swapping"<<"\n";
cout<<"a="<<a<<"\n"<<"b="<<b;
swap(a,b);
cout<<"\n"<<"The values after exchange are"<<"\n";
cout<<"a="<<a<<"\n"<<"b="<<b;
return 0;
}
void swap(int &c,int &d)
{
int t;
t=c;
c=d;
d=t;
}

Larger value in two numbers using conditional operator in C++


#include<iostream.h>
int main()
{
int a,b,r;
cout<<"Enter two numbers to calculate bigger number \n";
cin>>a>>b;
r=a>b?a:b;
cout<<"The largest number is"<<r;
return 0;
}

Display string for desired number of times using C++


#include<iostream.h>
int main()
{
int a;
cout<<"Enter number of times to print string \n";
cin>>a;
for(int i=1;i<=a;i++)
{
cout<<"Well done \n";
}
return 0;
}

Conversion of Temperature from Fahrenheit to Celsius using C++


#include<iostream.h>
#include<conio.h>
int main()
{
float f,c;
cout<<"Enter the temparature in farhenheit";
cin>>f;
c=(f-32)*5/9.0;
cout<<"\n Temparature in centigrade is:"<<c;
return 0;
}

Conversion of Temperature from Fahrenheit to Celsius using classes in C++


#include<iostream.h>
#include<conio.h>
class temp
{
public:
float gett()
{
float f;
cout<<"Enter the temparature in farhenheit";
cin>>f;
return f;
}
float calc(float)
{
float c,f;
c=(f-32)*5/9.0;
return c;
}
void display(float)
{
int c;
cout<<"\n Temparature in centigrade is:"<<c;
}
};
int main()
{
float c,f;
temp t;
t.gett();
t.calc(f);
t.display(c);
return 0;
}

Wednesday, November 28, 2012

Read & Write MODE R+ MODE usage in FILES using C language


#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
FILE *f;
char c;
clrscr();
f=fopen("balaram.txt","r+");
if(f==NULL)
{
 printf("File can not appending");
 exit(1);
}
printf("\n contents read");
while(!feof(f))
{
printf("%c",getc(f));
}
printf("write data and to stop press'.':");
while(c!='.')
{
c=getche();
 fputc(c,f);
}
}

FINDING OF TRACE OF A MATRIX


#include<stdio.h>
main()
{
int a[10][10],i,j,sum=0;
printf("enter the values of m,n");
scanf("%d%d",&m,&n);
printf("enter the elements of matrix a");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(i==j)
sum=sum+a[i][j];
printf("trace of a matrix=%d",sum);
}

Appending & Reading with A+ MODE using C language


#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
FILE *f;
char c=' ';
clrscr();
f=fopen("balaram.txt","a+");
if(f==NULL)
{
printf("\n can not open file");
exit(1);
}
printf("\n write data and to stop process press'#':");
while(c!='#')
{
c=getche();
fputc(c,f);
}
printf("\n contents read");
rewind(f);
while(!feof(f))
printf("%c",getc(f));
}

Writing & Reading of FILE using W+ MODE in C language

#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
FILE *f;
char c=' ';
clrscr();
f=fopen("kemp.txt","w+");
if(f==NULL)
{
printf("\n can not open file");
exit(1);
}
printf("write data and to stop process press '*':");
while(c!='*')
{
c=getche();
fputc(c,f);
}
rewind(f);
printf("\n contents read:");
while(!feof(f))
printf("%c",getc(f));
}

Tuesday, November 27, 2012

Appending DATA to a Pre-Existing FILE using C languge


#include<stdio.h>

#include<conio.h>

#include<process.h>

void main()

{

FILE *f;

char c;

clrscr();

printf("\n contents of file before appending");

f=fopen("balaram.txt","r");

while(!feof(f))

{
 
c=fgetc(f);

 printf("%c",c);

}

f=fopen("balaram.txt","a");

if(f==NULL)

{

 printf("File can not appending");

  exit(1);

}

printf("\n Enter string to append:");

while(c!='.')

{
 
c=getche();

  fputc(c,f);

}

fclose(f);

printf("\n contents of file after appending:\n");

f=fopen("balaram.txt","r");

while(!feof(f))

{

  c=fgetc(f);

  printf("%c",c);

}

}

DC motor control with Pulse Width Modulation Part 1

DC Motor intro DC motor is a device which converts electrical energy into kinetic energy. It converts the DC power into movement. The typica...