Thursday, August 14, 2014

IP address program

Really short C program to print out your ip address. Run it as an .exe. 


#include<stdlib.h>
main()
{
system("C:\\Windows\\System32\\ipconfig");
}

Wednesday, August 13, 2014

Decimal to Binary Program

Simple program to Convert a decimal number to binary. If you have doubts, let me know
#include <stdio.h>
main()
{
int num,count,temp;
printf("Enter the number");
scanf("%d",&num);
for(count=31;count>= 0;count--)
{
temp=num>>count;
if(temp&1)
printf("1");
else
printf("0");
}
}

Tuesday, August 12, 2014

Program to copy a file

C program to copy file. If you have any doubts, let me know

#include <stdlib.h>
#include <stdio.h>
main()
{
   
FILE *source, *target;
char x, sourcef[100], targetf[100];
printf("File to copy?"); gets(sourcef);   source=fopen(sourcef, "r");  printf("Target File?"); gets(targetf);   target=fopen(targetf, "w");   while((x=fgetc(source))!=EOF) fputc(x, target);   printf("The file has been copied."); }

Program to read from file

Program to read from a file. If you have any doubts, let me know

#include <stdlib.h>
#include <stdio.h>
main()
{
char x, name[100];
FILE *pf;
    printf("Name of file?");
    gets(name);
    pf = fopen(name,"r");
    printf("Contents are :");
         while((x=fgetc(pf))!= EOF)
         printf("%c",x);
    }

Saturday, August 9, 2014

Program to find twin prime numbers.

Program to find twin prime numbers. Please comment if you need help.

#include<stdio.h>
void main( )
{
int  i,n,k,r,a[50],p;
printf("nEnter the range:  ");
scanf("%d", &r);
i=1;
p=0;
while(i<=r)
{
k=0;
n=1;
while(n<=i)
{
if( i%n==0 )
k++;
n++;
}
if(k==2)
{
a[p]=i;
p++;
}
i++;
}
for(n=0;n<p;n++)
{
if(a[n+1] - a[n]==2)
printf("\n%d and %d are twin primes", a[n], a[n+1]);
}
}

Program to implement Fibonacci series

Program to implement Fibonacci series. Comment if you need any help.

#include<stdio.h>
int fibo(int x);
main()
{
int n,i,ar[50];
printf("\n  enter the number of terms:");
scanf("%d",&n);
ar[0]=0;
for(i=1;i<n;i++)
{
ar[i]=fibo(i);
}
printf("\n");
for(i=0;i<n;i++)
printf("%d ",ar[i]);
printf("\n");
}
int fibo(int c)
{
int fib=0;
if((c==1)||(c==2))
return 1;
else
{
fib=fibo(c-1)+fibo(c-2);
return fib;
}
}





Program to write to a file and display the vowel which occurred the most

Program to write to a file and display the vowel which occurred the most often. If you need help comment below

#include<stdio.h>
main()
{
  FILE *f1;
  char c;
  int a,e,i,o,u;
  a=0;
  e=0;
  i=0;
  u=0;
  o=0;
  printf("\nEnter the letters to the file:\n");
  f1=fopen("TEXT.txt","w");
  while((c=getchar())!='\n')
    putc(c,f1);
  fclose(f1);
  f1=fopen("TEXT.txt","r");
  while((c=getc(f1))!=EOF)
   {
     if(c=='a')
       a++;
     else if(c=='e')
       e++;
     else if(c=='i')
       i++;
     else if(c=='o')
       o++;
     else if(c=='u')
       u++;
   }
  fclose(f1);
  if((a>=e)&&(a>=i)&&(a>=o)&&(a>=u))
   printf("\n\n'a'is the vowel which occured the most.\n%d times",a);
  if((e>=a)&&(e>=i)&&(e>=u)&&(e>=o))
   printf("\n\n'e' is the vowel which occured the most.\n%d times",e);
  if((i>=a)&&(i>=e)&&(i>=o)&&(i>=u))
   printf("\n\n'i' is the vowel which occured the most.\n%d times",i);
  if((o>=a)&&(o>=e)&&(o>=i)&&(o>=u))
   printf("\n\n'o'is the vowel which occured the most.\n%d times",o);
  if((u>=a)&&(u>=e)&&(u>=i)&&(u>=o))
   printf("\n\n'u' is the vowel which occured the most.\n%d times",u);
  printf("\n\n");
}