Saturday, October 19, 2013

Print all values passed by commandline arguments in C++

Program:

//disp.cpp
#include<iostream.h>    
#include<conio.h>
int main(int args,char *argv[])
{
for(int i=0;i<args;i++)
cout<<"\n"<<argv[i];
getch();
return 0;
}

Output:

disp.c Hai hello

Hai
hello

Print All values passed by commandline arguments in C

Program:

//disp.c
#include<stdio.h>         //print all values passed by commandline arguments
#include<conio.h>         //TESTED
int main(int args,char *argv[])
{
for(int i=0;i<args;i++)
printf("\n%s",argv[i]);
getch();
return 0;
}

Output:

disp.c Hai hello

Hai
hello 

Adding 2 numbers in Command line arguments

Program:

//ADD.CPP
#include<iostream.h>
#include<conio.h>                    
#include<stdlib.h>                    
void main(int argc , char * argv[])
{
    clrscr();
    int i,sum=0;
    if(argc!=3)
    {
      cout<<"\nYou have forget to type numbers";
      getch();
      exit(1);
    }
    cout<<"\nThe SUM is:";
    for(i=1;i<argc;i++)
    sum = sum + atoi(argv[i]);
    cout<<sum;
    getch();
}

Output:

[Ctrl+F9 does'nt work here]
Navigate File ---> DOS shell --->Type Program name . extention (ie. ADD.CPP 20 30)

ADD.CPP 20 30
The SUM is:50

Remove Duplicate Entries in an Array

Program:
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a[20],i,j,k,n;
clrscr();
printf("\nEnter Array size : ");
scanf("%d",&n);
printf("\nEnter Numbers :\n ");
for(i=0;i<n;i++)
  scanf("%d",&a[i]);
clrscr();
printf("\nOriginal Array is : \n");
for(i=0;i<n;i++)
  printf("\n %d",a[i]);
printf("\nUpdated Array is  :\n ");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;)
    {
       if(a[j]==a[i])
       {
  for(k=j;k<n;k++)
      a[k]=a[k+1];
   n--;
       }
       else
  j++;
    }
}
for(i=0;i<n;i++)
    printf("\n%d ",a[i]);
getch();
return 0;
}

Output:
Enter Array size : 5
Enter Numbers:
2
4
5
5
6
Original Array is:
2
4
5
5
6
Updated Array is:
2
4
5
6

Program to print it's Own Source code

Program:
#include<stdio.h>    
#include<conio.h>    
int main()
{
clrscr();
FILE *fp;
char c;
fp = fopen(__FILE__,"r");
do
{
c= getc(fp);
putchar(c);
}
while(c!=EOF);
fclose(fp);
getch();
return 0;
}

Output:

#include<stdio.h>  
#include<conio.h>    
int main()
{
clrscr();
FILE *fp;
char c;
fp = fopen(__FILE__,"r");
do
{
c= getc(fp);
putchar(c);
}
while(c!=EOF);
fclose(fp);
getch();
return 0;
}

String Sort

Program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
     clrscr();
    char *str[5],*temp;
    int i,j,n;
    printf("\nHow many names do you want to sort:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("\nEnter the name %d: ",i);
        flushall();
        gets(str[i]);
    }
    for(i=0;i<n;i++)
    {
     for(j=0;j<n-1;j++)
     {
       if(strcmp(str[j],str[j+1])>0)
         {
         strcpy(temp,str[j]);
         strcpy(str[j],str[j+1]);
         strcpy(str[j+1],temp);
         }
     }
    }
    printf("\nSorted List : \n");
    flushall();
    for(i=0;i<n;i++)
        puts(str[i]);
getch();
    return(0);
}

Output:
How many names do you want to sort:5
Enter the name 0:Google
Enter the name 1:Bing
Enter the name 2:Amazon
Enter the name 3:Facebook
Enter the name 4:Twitter
Sorted List :
Amazon
Bing
Facebook
Google
Twitter

Program for converting Roman letter into Decimal number

Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
int digitValue(char);
int main()
{
clrscr();
char roman_Number[1000];
int i=0;
long int number =0;
printf("\nEnter roman letter: ");
scanf("%s",roman_Number);
while(roman_Number[i])
{
if(digitValue(roman_Number[i]) < 0)
{
printf("Invalid roman digit : %c",roman_Number[i]);
return 0;
}
if((strlen(roman_Number) -i) > 2)
{
if(digitValue(roman_Number[i]) < digitValue(roman_Number[i+2])){
printf("Invalid roman number");
return 0;
}
}
if(digitValue(roman_Number[i]) >= digitValue(roman_Number[i+1]))
number = number + digitValue(roman_Number[i]);
else
{
number = number + (digitValue(roman_Number[i+1]) - digitValue(roman_Number[i]));
i++;
}
i++;
    }
    printf("\nEqual decimal value is : %ld",number);
    getch();
    return 0;
}
int digitValue(char c)
{
int value=0;
switch(c)
{
case 'I': value = 1; break;
case 'V': value = 5; break;
case 'X': value = 10; break;
case 'L': value = 50; break;
case 'C': value = 100; break;
case 'D': value = 500; break;
case 'M': value = 1000; break;
case '\0': value = 0; break;
default: value = -1;
}
return value;
}

Output:

Enter roman letter : VII
Equal decimal value is : 7 

Program for Adding two binary numbers

Program:
#include<stdio.h>   //TESTED
#include<conio.h>
int main()
{
clrscr();
    long int binary1,binary2;
    int i=0,remainder = 0,sum[20];
printf("\nEnter binary number 2: ");
scanf("%ld",&binary1);
    printf("\nEnter binary number 2: ");
    scanf("%ld",&binary2);
while(binary1!=0||binary2!=0)
{
        sum[i++] =  (binary1 %10 + binary2 %10 + remainder ) % 2;
        remainder = (binary1 %10 + binary2 %10 + remainder ) / 2;
        binary1 = binary1/10;
        binary2 = binary2/10;
}
if(remainder!=0)
        sum[i++] = remainder;
        --i;
    printf("\nSum of two binary numbers: ");
    while(i>=0)
        printf("%d",sum[i--]);
    getch();
    return 0;
}

Output:
Enter binary number 1: 10
Enter binary number 2: 11
Sum of two binary numbers:101

Convert Decimal Into Hexadecimal

Program:

#include<stdio.h>
#include<conio.h> //tested
int main()
{
    clrscr();
    long int decimalNumber,remainder,quotient;
    int i=1,j,temp;
    char hexadecimalNumber[100];
    printf("Enter any decimal number: ");
    scanf("%ld",&decimalNumber);
    quotient = decimalNumber;
    while(quotient!=0)
    {
         temp = quotient % 16;
         //To convert integer into character
         if( temp < 10)
           temp =temp + 48;
        else
          temp = temp + 55;
        hexadecimalNumber[i++]= temp;
       quotient = quotient / 16;
  }

    printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumber);
    for(j = i -1 ;j> 0;j--)
    printf("%c",hexadecimalNumber[j]);
    getch();
    return 0;
}

Output:
Enter any decimal number:16
Equivalent hexadecimal value of decimal number 16 : f

Pattern Printing (Alphabetic Triangle)

Program:

#include<stdio.h>
#include<conio.h>
int main()
{
    clrscr();
    char i,k,m,h=71;
    int g=1;
    float j;
    for(i=71;i>=65;i--)
    {
        for(m=65;m<=i;m++)
        {
            printf("%c",m);
        }
        for(j=2;j<=g;j++)
        {
            printf("__"); // Two Spaces
        }
        g++;
        printf("\b");
        for(k=h;k>=65;k--)
        {
            printf("%c",k);
        }
        h--;
        printf("\n");
    }
    getch();
    return 0;
}



output:
ABCDEFGFEDCBA
ABCDEF_FEDCBA
ABCDE___EDCBA
ABCD_____DCBA
ABC_______CBA
AB_________BA
A___________A

Friday, October 18, 2013

Text Masking Using C




#include<stdio.h>
#include<conio.h>
char pw[25],ch;
int i;
void main()
{
     clrscr();
     puts(“Enter password”);
     while(1)
     {
          if(i<0)
          i=0;
         ch=getch();
          if(ch==13)
          break; /*13 is ASCII value of ENTER*/
          if(ch==8) /*ASCII value of BACKSPACE*/
          {
               putch(‘\b’);
               putch(NULL);
               putch(‘\b’);
               –i;
               continue;
          }
          pw[i++]=ch;
          ch=’*';
          putch(ch);
     }
     pw[i]=’\0';
     printf(“\n\n%s”,pw);
     getch();
}

Make folder using C

#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <dir.h>
#define DIRNAME "c:\\testdir"
int main(void)
{
   int stat;
   stat = mkdir(DIRNAME);
   if (!stat)
      printf("Directory created\n");
   else
   {
      printf("Unable to create directory\n");
      exit(1);
   }
   getch();
   system("dir/p");
   getch();
   stat = rmdir(DIRNAME);
    if (!stat)
      printf("\nDirectory deleted\n");
   else
   {
      perror("\nUnable to delete directory\n");
      exit(1);
   }
   return 0;
}

Wednesday, October 16, 2013

"FLAMES" Using C++



#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
     char n1[20],n2[20],a[20],b[20];
     int i,j,tot=0;
     cout<<"\n Enter the boy name  : ";
     cin>>a;
     cout<<"\n Enter the girl name : ";
     cin>>b;
     cout<<endl;
     strcpy(n1,a);
     strcpy(n2,b);
     i=strlen(a);
     j=strlen(b);
     tot=i+j;
     for(i=0;a[i]!=NULL;i++)
    {
          for(j=0;b[j]!=NULL;j++)
          {
               if(a[i]==b[j]) //for checking same characters
              {
                   tot=tot-2;
                   b[j]='*';  //to avoid duplication
                   break;
               }
          }
     }
cout<<"\n count = "<<tot<<endl;
if(tot == 2 ||tot== 4 || tot==7 ||tot==9 )
cout<<   n1 << " and " << n2 << " are Enemy";
else if( tot == 3 ||tot==5 || tot==14 ||tot==16 || tot==18 )
cout<<   n1 << " and " << n2 << " are Friends";
else if( tot == 8 || tot==12 || tot==17)
cout<<   n1 << " and " << n2 << " in Affection";
else if( tot == 10)
cout<<   n1 << " and " << n2 << " Love";
else if( tot == 6 || tot==11 || tot==15)
cout<<   n1 << " and " << n2 << " will arriage";
else
cout<<   n1 << " and " << n2 << " are SweetHearts";
getch();
return 0;
}