Monday, June 8, 2015

Power using recursion

Power
#include<stdio.h>
int main(){
int pow,num;
long int res;
long int power(int,int);
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter power: ");
scanf("%d",&pow);
res=power(num,pow);
printf("\n%d to the power %d is: %ld",num,pow,res);
return 0;
}

int i=1;
long int sum=1;

long int power(int num, int pow)
{
if(i<=pow){
sum=sum*num;
power(num,pow-1);
}
else
return sum;

return sum;


}

Factorial Using Recursion

Factorial
#include<stdio.h>
long factorial(int);
main()
{
int num;
long f;

printf("ENTER A NUMBER TO FIND FACTORIAL :");
scanf("%d",&num);

if(num<0)
printf("NEGATIVE NUMBERS ARE NOT ALLOWED");
else
{
f = factorial(num);
printf("%d!=%ld",num,f);
}
return(0);
}

long factorial(int n)
{
if(n==0)
return(1);
else
return(n*factorial(n-1));
}

Remove extra spaces from a sting


#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
  char a[]="  gr    fr frrfr f r     frfr " ; 
    int i,j,k,n;
   n=strlen(a);
   printf(" the len is %d",n);
  for(i=0;i<n;)
      {       
              if(a[i]==' ' &&  a[i+1]==' ')
              {
                 for(j=i;j<n;j++)
                   {
                    a[j]=a[j+1];;
                    } 
                  n--;
               }
               else
                   i++;
      }
if(a[0]==' ')
{
 for(i=0;i<n;i++)
  {
    a[i]=a[i+1];
   }
 n--;
}

a[n-1]='\0';
 n=strlen(a);
printf(" the len is %d",n);
printf("\n\n");
puts(a);
getch();
return 0;
}


Remove duplicate items from an array

#include<stdio.h>

int main() {
   int arr[20], i, j, k, size;

   printf("\nEnter array size : ");
   scanf("%d", &size);

   printf("\nAccept Numbers : ");
   for (i = 0; i < size; i++)
      scanf("%d", &arr[i]);
    printf("\nArray with Unique list  : ");
   for (i = 0; i < size; i++)
   {
      for (j = i + 1; j < size; )
      {
         if (arr[j] == arr[i])
           {
            for (k = j; k < size; k++)
             {
               arr[k] = arr[k + 1];
              }
            size--;
           }
         else
            j++;
      }
   }

   for (i = 0; i < size; i++) {
      printf("%d ", arr[i]);
   }

   return (0);

}

C program to convert the file contents in Upper-case & Write Contents in a output file

#include<stdio.h>
#include<process.h>

void main() {
   FILE *fp1, *fp2;
   char a;
   clrscr();

   fp1 = fopen("test.txt", "r");
   if (fp1 == NULL) {
      puts("cannot open this file");
      exit(1);
   }

   fp2 = fopen("test1.txt", "w");
   if (fp2 == NULL) {
      puts("Not able to open this file");
      fclose(fp1);
      exit(1);
   }

   do {
      a = fgetc(fp1);
      a = toupper(a);
      fputc(a, fp2);
   } while (a != EOF);

   fcloseall();
   getch();
}

Block I/O fread/fwrite- file handling

#include<stdio.h>
#include<conio.h>
void main()
{
typedef struct student
{
char name[20];
int age;
}stu;
stu s1[10],s2;
int n,i;
FILE *fp1;
clrscr();
fp1=fopen("stud.dat","wb");
if(fp1==NULL)
{
printf("file creation error\n");
getch();
exit(1);
}
printf("enter the no of students\n");
scanf("%d",&n);
printf("entre the details of students\n");
for(i=0;i<n;i++)
{
printf("enter the name and age\n");
scanf("%s%d",s1[i].name,&s1[i].age);
}
fwrite(s1,sizeof(stu),n,fp1);
//fwrite(s1,sizeof(s1),1,fp1);
fclose(fp1);

fp1=fopen("stud.dat","rb");
if(fp1==NULL)
{
printf("file does not exist\n");
getch();
exit(1);
}
else
{
//reading 1 record at a time from file
while(fread(&s2,sizeof(s2),1,fp1)==1)
{
printf("\nname->%s age->%d",s2.name,s2.age);
}
//reading all n records at once from file
fread(s1,sizeof(stu),n,fp1);
for(i=0;i<n;i++)
{
printf("\nname->%s age->%d",s1[i].name,s1[i].age);
}
}
fclose(fp1);
getch();
}

Copy Text From One File to Other File

#include <stdio.h>
#include <stdlib.h> // For exit()

int main()
{
    FILE *fptr1, *fptr2;
    char filename[100], c;

    printf("Enter the filename to open for reading \n");
    scanf("%s", filename);

    // Open one file for reading
    fptr1 = fopen(filename, "r");
    if (fptr1 == NULL)
    {
        printf("Cannot open file %s \n", filename);
        exit(0);
    }

    printf("Enter the filename to open for writing \n");
    scanf("%s", filename);

    // Open another file for writing
    fptr2 = fopen(filename, "w");
    if (fptr2 == NULL)
    {
        printf("Cannot open file %s \n", filename);
        exit(0);
    }

    // Read contents from file
    c = fgetc(fptr1);
    while (c != EOF)
    {
        fputc(c, fptr2);
        c = fgetc(fptr1);
    }

    printf("\nContents copied to %s", filename);

    fclose(fptr1);
    fclose(fptr2);
    return 0;
}