Programmer

Friday, 29 June 2018

#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
ifstream fin;
int count=0,num,l=1;
char name[20],line[20],ch;
clrscr();
system("dir/w *.txt");
cout<<"Enter File Name :";
cin.getline(name,20);
clrscr();
fin.open(name,ios::in);
fin.get(ch);
while(!fin.eof())
{
if(ch=='\n')
l++;                //Line Counter
cout<<ch;
fin.get(ch);
}
fin.close();
cout<<"\n\nReading Completed..."<<endl;
cout<<"There are "<<l<<" Lines in File";
cout<<endl<<endl;
fin.open(name,ios::in);
cout<<"Which Line You Want to Print From "<<name<<" File :";
cin>>num;
if(num<=l)
{
while(!fin.eof())
{
fin.getline(line,80,'\n');
count++;
if(count==num)
{
cout<<endl<<line<<endl;
}
}
fin.close();
}
else
cout<<"Line Not Found...";
getch();
}




#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
int i,j,n,temp=1;
clrscr();
cout<<"Enter Number :";
cin>>n;
cout.setf(ios::right);
for(i=1;i<n*2;i++)
{
for(j=1;j<n*2;j++)
{
if(i+j<=n||i-j>=n||j-i>=n||i+j>=n*3)
{
cout<<setw(3)<<temp++;
}
else
{
textcolor(RED);
cprintf(" * ");
textcolor(7);
}
}
cout<<endl;
}
getch();
}


Monday, 25 June 2018

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
long num1,num2;
int first[10],second[10],ans[10],temp,size1=0,size2=0,sum1=0,sum2=0,size3=0;
clrscr();
cout<<"Enter 1st Binary Number :";
cin>>num1;
cout<<"Enter 2nd Binary Number :";
cin>>num2;
int i=0;
while(num1)
{
temp=num1%10;
first[i]=temp;
num1=num1/10;
i++;
size1++;
}
i=0;
while(num2)
{
temp=num2%10;
second[i]=temp;
num2=num2/10;
i++;
size2++;
}
for(i=0;i<size1;i++)
{
sum1+=((pow(2,i))*first[i]);
}
for(i=0;i<size2;i++)
{
sum2+=((pow(2,i))*second[i]);
}
i=0;
int total=sum1+sum2;
while(total)
{
temp=total%2;
ans[i]=temp;
total=total/2;
i++;
size3++;
}
cout<<endl<<endl<<endl;
for(i=size1-1;i>=0;i--)
{
cout<<" "<<first[i];
}
cout<<endl<<"+";
for(i=size2-1;i>=0;i--)
{
cout<<second[i]<<" ";
}
cout<<"\n============"<<endl;
for(i=size3-1;i>=0;i--)
{
cout<<ans[i]<<" ";
}
getch();
}


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>
void main()
{
char str[5][20];
int temp[5];
int i,j;
clrscr();
for(i=0;i<5;i++)
{
cout<<"Enter "<<i+1<<" String :";
cin.getline(str[i],20);
temp[i]=-1;
}
int count;
for(i=0;i<5;i++)
{
count=1;
for(j=i+1;j<5;j++)
{
if(strcmp(str[i],str[j])==0)
{
count++;
temp[j]=0;
}
}
if(temp[i]!=0)
{
temp[i]=count;
}
}
cout<<endl<<endl;
cout<<setw(10)<<"String"<<setw(12)<<"Frequency"<<endl;
cout<<"======================"<<endl;
for(i=0;i<5;i++)
{
if(temp[i]!=0)
cout<<setw(10)<<str[i]<<setw(10)<<temp[i]<<endl;
}
getch();
}


Thursday, 21 June 2018

//Example of stack
#include<iostream.h>
#include<conio.h>

struct Node
{
int data;
Node *next;
}*f,*ptr,*start=NULL;

void AddNode()
{
ptr=new Node;
int d;
cout<<"Enter Data : ";
cin>>d;
ptr->data=d;
if(start==NULL)
{
start=ptr;
}
else
{
ptr->next=start;
start=ptr;
}
}

void Display()
{
f=start;
if(f==NULL)
{
cout<<"Stack is Empty...\n";
}
else
{
while(f!=NULL)
{
cout<<f->data<<"-->";
f=f->next;
}
cout<<"<--Done\n";
cout<<"List Displayed....\n";
}
}

void Delete()
{
f=start;
if(f==NULL)
{
cout<<"Stack is Empty...\n";
}
else
{
start=start->next;
cout<<"The Data : --> "<<f->data<<" is deleted...\n";
delete f;
}
}

void main()
{
int ch;
do
{
clrscr();
cout<<"0. Exit\n";
cout<<"1. Add Node to Top\n";
cout<<"2. Display All Nodes from Top\n";
cout<<"3. Delete Node from Top\n";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1: AddNode(); break;
case 2: Display(); break;
case 3: Delete(); break;
}
getch();
}while(ch);
}
#include<iostream.h>
#include<conio.h>
int *arr;
void main()
{
int size,i,pos;
clrscr();
cout<<"How Many Numbers You Want to Add :";
cin>>size;
arr=new int[size];
for(i=0;i<size;i++)
{
cout<<"Enter "<<i+1<<" Number :";
cin>>arr[i];
}
cout<<"Before Deletation :"<<endl;
for(i=0;i<size;i++)
{
cout<<arr[i]<<endl;
}
cout<<"From Which Position You Want to Delete Number :";
cin>>pos;
if(pos>=1 && pos<=size)
{
for(i=pos-1;i<size;i++)
{
arr[i]=arr[i+1];
}
size--;
cout<<"After Delete Number From Array :"<<endl;
for(i=0;i<size;i++)
{
cout<<arr[i]<<endl;
}
}
else
cout<<"Select Position From 1 to "<<size;
delete []arr;
getch();
}
#include<iostream.h>
#include<conio.h>
int *arr;
void main()
{
int size,i,num,index;
clrscr();
cout<<"How Many Numbers You Want to Add :";
cin>>size;
arr=new int[size];
for(i=0;i<size;i++)
{
cout<<"Enter "<<i+1<<" Number :";
cin>>arr[i];
}
for(i=0;i<size;i++)
{
cout<<arr[i]<<endl;
}
cout<<"In Which Index You Want to Add Number :";
cin>>index;
cout<<"Which Number You Want to Add :";
cin>>num;
size++;
for(i=size;i>index;i--)
{
arr[i]=arr[i-1];
}
arr[index]=num;
cout<<"\n\nArray After Add Number :"<<endl;
for(i=0;i<size;i++)
{
cout<<arr[i]<<endl;
}
delete []arr;
getch();
}


Tuesday, 12 June 2018

What is Anagram?
C program to check whether two strings are anagrams or not, a string is assumed to consist of lower case alphabets only. Two words are said to be anagrams of each other if the letters of one word can be rearranged to form the other word. So, in anagram strings, all characters occur the same number of times. For example, "abc" and "cab" are anagram strings, as every character 'a,' 'b,' and 'c' occur the same number of times (one time here) in both the strings. A user will input two strings, and our algorithm counts how many times each character ('a' to 'z') appear in both the strings and then compare their corresponding counts. The frequency of an alphabet in a string is how many times that alphabet appears in the string. For example, the frequency of 'm' in the string "programming" is '2' as it is present two times in "programming."

//Program to check The string is Anagram or not.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char ch1[100],ch2[100],temp;
int i,flag=1,str1[26],str2[26];
clrscr();
cout<<"Enter 1st String :";
cin.getline(ch1,100);
int n=strlen(ch1);
for(i=0;i<26;i++)
{
str1[i]=0;
}
for(i=0;ch1[i]!='\0';i++)
{
if(islower(ch1[i]))
ch1[i]=toupper(ch1[i]);
}
for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(ch1[i]>ch1[j])
{
temp=ch1[i];
ch1[i]=ch1[j];
ch1[j]=temp;
}
}
}
cout<<"Enter 2nd String :";
cin.getline(ch2,100);
int s=strlen(ch2);
for(i=0;i<26;i++)
{
str2[i]=0;
}
for(i=0;ch2[i]!='\0';i++)
{
if(islower(ch2[i]))
ch2[i]=toupper(ch2[i]);
}
for(i=0;i<s;i++)
{
for(int j=i+1;j<s;j++)
{
if(ch2[i]>ch2[j])
{
temp=ch2[i];
ch2[i]=ch2[j];
ch2[j]=temp;
}
}
}
if(strcmp(ch1,ch2)==0)
cout<<"Both Strings Are Anagram.";
else
cout<<"Both Strings Are Different.";
getch();

}


Friday, 8 June 2018

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[3][3],b[3][3],c[3][3]={0},i,j,k;
clrscr();
randomize();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=random(10);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=random(10);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\t";

for(j=0;j<3;j++)
{
cout<<b[i][j]<<" ";
}
cout<<"\t";

for(j=0;j<3;j++)
{
cout<<c[i][j]<<" ";
}
cout<<endl;
}
getch();
}

/*
Output:

3 0 6   2 2 8   48 24 48
3 1 3   7 5 4   34 20 40                                                       
5 3 8   7 3 4   87 49 84                                                       
                                                                               
*/

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[3][3],rsum[3]={0},csum[3]={0},i,j;
clrscr();
randomize();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=random(10);
rsum[i]+=a[i][j];
csum[j]+=a[i][j];
}
}
cout<<"Matrix is :"<<endl<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<rsum[i];
cout<<endl;
}
for(i=0;i<3;i++)
cout<<csum[i]<<"\t";
cout<<endl;
getch();
}

/*
Output:
3       6       0       9
3       3       2       8                                                     
1       8       1       10                                                     
7       17      3                                                             

*/

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
void main()
{
int a[10][10],i,j,n,temp;
system("cls");
cout<<"\n\nEnter Number of Rows And Columns :";
cin>>n;
randomize();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=random(20)+5;
}
}
cout<<"Before Interchange Diagonal "<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<setw(4)<<a[i][j];
}
cout<<endl;
}
cout<<"\n\nAfter Interchange Diagonal "<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i+j==n-1 || i==j)
{
temp=a[i][j];
a[i][j]=a[i][n-i-1];
a[i][n-i-1]=temp;
}
}
}
textcolor(YELLOW);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i+j==n-1 || i==j)
cprintf("%4d",a[i][j]);
else
cout<<setw(4)<<a[i][j];
}
cout<<endl;
}
getch();
}

/*
Enter Number of Rows And Columns :5
Before Interchange Diagonal
  22  15  24   6  13
  22  20  10   5   8
  22  11   5  11   9
  19   8  17  21   9
  23   5  12  12  14


After Interchange Diagonal
  13  15  24   6  22
  22   5  10  20   8
  22  11   5  11   9
  19  21  17   8   9
  14   5  12  12  23

*/

Thursday, 7 June 2018

#include<iostream.h>
#include<conio.h>
#include<math.h>

void main()
{
long n, x, arr[10],i=0,count=0, size=0, temp, start, stop;
long tempArr[10],flag[10]={0};
clrscr();

cout<<"Enter N : ";
cin>>n;
x=n;
//Loop for getting the length of digits.
while(x)
{
temp=x%10;
size++;
arr[i++]=temp;
x/=10;
}

start=pow(10,size-1);
stop=pow(10,size);

//Loop for printing the range of length possibilities.
for(i=start;i<stop;i++)
{
long temp=i;
for(int j=0;j<size;j++)
{
tempArr[j]=temp%10;
temp=temp/10;
}
for(j=0;j<size;j++)
{
flag[j]=0;
for(int k=0;k<size;k++)
{
if(arr[j]==tempArr[k])
flag[j]+=1;
}
}
int f=0;
for(j=0;j<size;j++)
{
if(flag[j]!=1)
{
f++;
break;
}
}
if(!f)
{
cout<<i<<"\t";
count++;
}
}
cout<<"\nThere are "<<count<<" possibilities\n";

getch();
}


Tuesday, 5 June 2018

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[20],octal[20],i,j,temp,flag=0,sum=0,o=0;
long num;
clrscr();
cout<<"Enter Number :";
cin>>num;
i=0;
while(num)
{
temp=num%10;
num=num/10;
flag++;
a[i]=temp;
i++;
}
cout<<"Binary Number is :";
for(i=0;i<flag;i++)
{
cout<<a[i];
}
for(i=0;i<flag;i++)
{
sum+=(pow(2,i)*a[i]);
}
cout<<"\n\nDecimal Number is :"<<sum;
i=0;
while(sum)
{
temp=sum%8;
sum=sum/8;
o++;
octal[i]=temp;
i++;
}
cout<<endl<<"Octal Number is :";
for(i=o-1;i>=0;i--)
{
cout<<octal[i];
}
getch();

}



Friday, 1 June 2018

#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<process.h>
#include<conio.h>

class data
{
int id;
char fname[20],lname[20],pno[14];

public:
void add();
void display();
int getId(){ return id;}
char* getfname()
{
return fname;
}
}d;

void storedata();
void readdata();
void searchdata();
void deldata();

void main()
{
char n;
clrscr();
do
{
cout<<endl<<endl<<endl<<"\t\t\t         Main Menu";
cout<<endl<<"\t\t\t============================";
cout<<endl<<"\t\t\t 0. EXIT";
cout<<endl<<"\t\t\t 1. Add Data";
cout<<endl<<"\t\t\t 2. Display Data";
cout<<endl<<"\t\t\t 3. Search Data";
cout<<endl<<"\t\t\t 4. Delete Data";
cout<<endl<<"\t\t\t CHOOSE : ";
n=getche();
switch(n)
{
case '0': exit(0); break;
case '1':
clrscr();
storedata();
clrscr();
textcolor(RED+BLINK);
cprintf("!Data Added");
textcolor(7);
break;
case '2':
clrscr();
readdata();
getch();
clrscr();
break;
case '3':
clrscr();
searchdata();
break;
case '4':
clrscr();
deldata();
break;
default :
clrscr();
textcolor(RED+BLINK);
cprintf("Invalid Input");
textcolor(7);
}
}
while('n'!='0');
}
void data::add()
{
cout<<"Enter Your Id : ";
cin>>id;
cout<<"Enter Your First Name : ";
cin.get();
cin.getline(fname,20);
cout<<"Enter Your Last Name : ";
cin.getline(lname,20);
cout<<"Enter Your Phone No : ";
cin.getline(pno,14);
}
void data::display()
{
cout<<"Id : "<<id;
cout<<endl<<"Name : "<<fname<<" "<<lname;
cout<<endl<<"Phone No : "<<pno;
}
void storedata()
{
ofstream fout;
fout.open("data.dat",ios::binary|ios::app);
d.add();
fout.write((char*)&d,sizeof(d));
fout.close();
}
void readdata()
{
ifstream fin;
fin.open("data.dat");
while(fin.read((char*)&d,sizeof(d)))
{
d.display();
cout<<endl<<endl;
}
fin.close();
}
void searchdata()
{
int fid;
cout<<"Enter Id : ";
cin>>fid;
ifstream fin;
fin.open("data.dat");
while(fin.read((char*)&d,sizeof(d)))
{
if(fid==d.getId())
{
d.display();
cout<<endl<<endl;
}
}
fin.close();
}
void deldata()
{
char ffname[20];
ofstream fout;
cout<<"Enter First Name : ";
cin.getline(ffname,20);
ifstream fin;
fin.open("data.dat");
fout.open("data1.dat");
while(fin.read((char*)&d,sizeof(d)))
{
if(stricmp(ffname,d.getfname())!=0)
{
fout.write((char *)&d,sizeof(d));
}
}
fout.close();
fin.close();
remove("data.dat");
rename("data1.dat","data.dat");
fin.close();
}


Program To Print Particular Line From File.

#include<fstream.h> #include<conio.h> #include<stdlib.h> void main() { ifstream fin; int count=0,num,l=1; char ...