Saturday, 23 November 2019

Series Program - 12



Series Program - 12


1 3 8 15 27 50 92 169 311
void main()
{
    int a=1, b=3, c=4, n=10, sum, i;
    printf("%d %d ",a,b,c);
    for(i=4; i<=n; i++)
    {
        sum = a+b+c;
        printf("%d ",sum);
      a = b;
      b = c;
      c = sum;
    }
}

Series Program 11



Series Program 11


2 15 41 80 132 197 275 366 470 587
int main()
{
    int a=2,i,n=10;
    for(i=1;i<=n;i++)
    {
        printf("%d ",a);
        a+=13*i;
    }
    return 0;
}

Series Program - 10



Series Program - 10

1 2 3 6 9 18 27 54...
int main()
{
    int a=1,b=2,i,n=10;
    printf("%d %d ",a, b);
    for(i=3;i<=n;i++)
    {
        if(i%2==1)
        {
            a=a*3;
            printf("%d ",a);
        }
        else
        {
            b=b*3;
            printf("%d ",b);
        }
    }
    return 0;
}

Series Program 9



Series Program 9

1/2 - 2/3 + 3/4 - 4/5 + 5/6 - ...... n

int main()
{
    double i, n,sum=0;
    n=10;
    for(i=1;i<=n;i++)
    {
        if ((int)i%2==1)
            sum+=i/(i+1);
        else
            sum-=i/(i+1);
    }
    printf("Sum: %lf",sum);
    return 0;
}

Series Program 8



Series Program 8


[(1^1)/1!] + [(2^2)/2!] + [(3^3)/3!] + [(4^4)/4!] + [(5^5)/5!] + ... + [(n^n)/n!]

double power(int a, int b)
{
    long i, p=1;
    for(i=1;i<=b;i++)
    {
        p=p*a;
    }
    return p;
}

double fact(int n)
{
    long i, f=1;
    for(i=1;i<=n;i++)
    {
        f=f*i;
    }
    return f;
}

int main()
{
    long i,n;
    double sum=0;
    n=5;
    for(i=1;i<=n;i++)
    {
        sum=sum+power(i,i)/fact(i);
    }
    printf("Sum: %lf",sum);
    return 0;
}

Series Program 7



Series Program 7

[(1^1)/1] + [(2^2)/2] + [(3^3)/3] + [(4^4)/4] + [(5^5)/5] + ... + [(n^n)/n]

long power(int a, int b)
{
    long i, p=1;
    for(i=1;i<=b;i++)
    {
        p=p*a;
    }
    return p;
}

int main()
{
    long i,n;
    double sum=0;
    n=5;
    for(i=1;i<=n;i++)
    {
        sum=sum+(power(i,i)/i);
    }
    printf("Sum: %lf",sum);
    return 0;
}

Series Program 6



Series Program 6

(1!/1) + (2!/2) + (3!/3) + (4!/4) + (5!/5) + ... + (n!/n)

long fact(int n)
{
    long i, f=1;
    for(i=1;i<=n;i++)
    {
        f=f*i;
    }
    return f;
}

int main()
{
    long i,n;
    double sum=0;
    n=5;
    for(i=1;i<=n;i++)
    {
        sum=sum+(fact(i)/i);
    }
    printf("Sum: %lf",sum);
    return 0;
}

Series Program 5



Series Program 5

(1^1) + (2^2) + (3^3) + (4^4) + (5^5) + ... + (n^n)

long power(int a, int b)
{
    long i, p=1;
    for(i=1;i<=b;i++)
    {
        p=p*a;
    }
    return p;
}

int main()
{
    long i,n,sum=0;
    n=5;
    for(i=1;i<=n;i++)
    {
        sum=sum+power(i,i);
    }
    printf("Sum: %d",sum);
    return 0;
}

Series Program 4



Series Program 4

1! + 2! + 3! + 4! + 5! + ... + n!

long fact(int n)
{
    long i, f=1;
    for(i=1;i<=n;i++)
    {
        f=f*i;
    }
    return f;
}

int main()
{
    long i,n,sum=0;
    n=5;
    for(i=1;i<=n;i++)
    {
        sum=sum+fact(i);
    }
    printf("Sum: %d",sum);
    return 0;
}

Series Program 3



Series Program 3

(1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)

int main()
{
    int i,j,n,sum=0;
    n=10;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            sum+=j;
        }
    }
    printf("Sum: %d",sum);
    return 0;
}

Series Program 2



Series Program 2

(1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)

int main()
{
    int i,n,sum=0;
    n=10;
    for(i=1;i<=n;i++)
    {
        sum+=i*i;
    }
    printf("Sum: %d",sum);
    return 0;
}

Series Program 1



Series Program 1

1 + 2 + 3 + 4 + 5 + ... + n

int main()
{
    int i,n,sum=0;
    n=10;
    for(i=1;i<=n;i++)
    {
        sum+=i;
    }
    printf("Sum: %d",sum);
    return 0;
}

Series Program

Alphabet Patterns



Alphabet Patterns

A
AB
ABC
ABCD
ABCDE
E
DE
CDE
BCDE
ABCDE
Code of above pattern

Code of above pattern

A
BA
CBA
DCBA
EDCBA
E
ED
EDC
EDCB
EDCBA
Code of above pattern

Code of above pattern

ABCDE
ABCD
ABC
AB
A
ABCDE
BCDE
CDE
DE
E
Code of above pattern

Code of above pattern

EDCBA
DCBA
CBA
BA
A
EDCBA
EDCB
EDC
ED
E
Code of above pattern

Code of above pattern

A
BB
CCC
DDDD
EEEEE
E
DD
CCC
BBBB
AAAAA
Code of above pattern

Code of above pattern

EEEEE
DDDD
CCC
BB
A
AAAAA
BBBB
CCC
DD
E
Code of above pattern

Code of above pattern

File Copy using command line arguments



File Copy using command line arguments

/* File Copy using command line arguments */

#include<stdio.h>
int main(int argc,char *argv[])
{
 FILE *fs,*ft;
 int ch;
 if(argc!=3)
 {
  printf("Invalide numbers of arguments.");
  return 1;
 }
 fs=fopen(argv[1],"r");
 if(fs==NULL)
 {
  printf("Can't find the source file.");
  return 1;
 }
 ft=fopen(argv[2],"w");
 if(ft==NULL)
 {
  printf("Can't open target file.");
  fclose(fs);
  return 1;
 }

 while(1)
 {
  ch=fgetc(fs);
  if (feof(fs)) break;
  fputc(ch,ft);
 }

 fclose(fs);
 fclose(ft);
 return 0;
}

File to upper case using command line arguments



File to upper case using command line arguments

/* File to upper case using command line arguments */

#include<stdio.h>
int main(int n,char *a[])
{
 int c;
 FILE *fr,*fw;

 if(n!=3)
 {
  printf("Invalide numbers of arguments.");
  return 1;
 }

 if((fr=fopen(a[1],"r"))==NULL)
 {
  printf("File can't be open.");
  return 1;
 }
 if((fw=fopen(a[2],"r+"))==NULL)
 {
  printf("File can't be open.");
  fclose(fr);
  return 1;
 }
 while(1)
 {
  c=fgetc(fr);
  if(feof(fr)) break;
  c=~c;
  fputc(c,fw);
 }
 fclose(fr);
 fclose(fw);
 return 0;
}

Count characters of file



Count characters of file

/* Count characters of file */

#include<stdio.h>
int main()
{
 FILE *fp;
 char a[10];
 long cnt=0;
 int c;

 printf("Type a file name : ");
 gets(a);
 
 if((fp=fopen(a,"r"))==NULL)
  printf("File dosen't exist.");
 else
 {
  while(1)
  {
   c=fgetc(fp);
   if(feof(fp)) break;
   cnt++;
  }
  printf("\nfile have %ld characters",cnt);
 }
 fclose(fp);
 return 0;
}

Write to text file



Write to text file

/* Write to text file */

#include<stdio.h>
int main()
{
 FILE *fp;
 char mystr[100];

 fp=fopen("mytext.txt","w");
 if(fp==NULL)
 {
  puts("Some error occured.");
  return 1;
 }

 printf("\nEnter some lines:\n");
 while(strlen(gets(mystr))>0)
 {
  fputs(mystr,fp);
  fputs("\n",fp);
 }
 fclose(fp);
 return 0;
}

Read text file



Read text file

/* Read text file */

#include<stdio.h>
int main()
{
 FILE *fp;
 int ch;
 
 fp=fopen("myfile.txt","r");
 if(fp==NULL)
 {
  printf("Can't find the source file.");
  return 1;
 } 
 while(1)
 {
  ch=fgetc(fp);
  if(feof(fp)) break;
  printf("%c",ch);
 } 
 fclose(fp);
 return 0;
}

Towers of Hanoi by recursion



Towers of Hanoi by recursion

/* Towers of Hanoi by recursion */

#include<stdio.h>
void toh(int,char,char,char);

int main()
{
 int n=3;
 toh(n,'A','B','C');
 return 0;
}

void toh(int n,char a,char b,char c)
{
 if(n==1)
  printf("\nMoved from %c to %c",a,c);
 else
 {
  toh(n-1,a,c,b);
  toh(1,a,' ',c);
  toh(n-1,b,a,c);
 }
}

Binary by recursion



Binary by recursion

/* Binary by recursion */

#include<stdio.h>
void binary(long);
int main()
{
 long n;
 printf("Type a value : ");
 scanf("%ld",&n);
 binary(n);
 return 0;
}

void binary(long n)
{
 if(n>1)
  binary(n/2);
 printf("%ld",n%2);
}

GCD by Recursion



GCD by Recursion

/* GCD by recursion */

#include<stdio.h>
int gcd(int,int);

int main()
{
 int a,b;
 printf("Type 2 values to find GCD :\n");
 scanf("%d %d",&a,&b);
 printf("GCD : %d",gcd(a,b));
 return 0;
}

int gcd(int m,int n)
{
 if(n>m) return gcd(n,m);
 if(n==0) return m;
 return gcd(n,m%n);
}

Sum of digit by Recursion



Sum of digit by Recursion

/* Sum of digit by recursion */

#include<stdio.h>
int sod(int);

int main()
{
 int i;
 printf(" Type any value : ");
 scanf("%d",&i);
 printf("Sum of digit : %d",sod(i));
 return 0;
}

int sod(int n)
{
 if(n<1)
  return 0;
 return(n%10+sod(n/10));
}

Reverse by Recursion



Reverse by Recursion

/* Reverse by Recursion */

#include<stdio.h>
int rev(int,int);

int main()
{
 int a;
 printf("Type a value : ");
 scanf("%d",&a);
 printf("Reverse: %d",rev(a,0));
 return 0;
}

int rev(int i,int r)  
{
 if(i > 0)  
  return rev(i/10,(r*10)+(i%10));  
 return r;  
}

Fibonacci by Recursion



Fibonacci by Recursion

/* Fibonacci by Recursion */

#include<stdio.h>
int fib(int);

int main()
{
 printf("Type any value : ");
 printf("\nNth value: %d",fib(getche()-'0'));
 return 0;
}

int fib(int n)
{
 if(n<=1)
  return n;
 return(fib(n-1)+fib(n-2));
}

Power by Recursion



Power by Recursion

/* Power by Recursion */

#include<stdio.h>
int pow(int,int);

int main()
{
 int i,j;
 printf("Type two values : \n");
 scanf("%d %d",&i,&j);
 
 printf("i pow j = %d",pow(i,j));
 return 0;
}

int pow(int i,int j)
{
 if(j==1)
  return i;
 return (i*pow(i,j-1));
}

Factorial by Recursion



Factorial by Recursion

/* Factorial by Recursion */

#include<stdio.h>
int fact(int);

int main()
{
  int n;
  printf("Type any value : ");
  scanf("%d",&n);
  n=fact(n);
  printf("\nFactorial : %d ",n);
  return 0;
}

int fact(int x)
{
  if(x==1)
    return(x);
  else
   x=x*fact(x-1);
}

Sort array of 10 strings



Sort array of 10 strings

/* Sort array of 10 strings. */

#include<string.h>
#include<stdio.h>

int main()
{
 int i,j;
 char str[10][10],temp[10];
 printf("Type 10 names :\n");
 for(i=0;i<10;i++)
 {
  // gets(str[i]);
  // fgets is a better option over gets to read multiword string .
  fgets(str[i], 10, stdin);
 }
 for(i=0;i<10;i++)
 {
  for(j=0;j<10-1-i;j++)
  {
   if(strcmpi(str[j],str[j+1])>0)
   {
    strcpy(temp,str[j]);
    strcpy(str[j],str[j+1]);
    strcpy(str[j+1],temp);
   }
  }
 }
   
 printf("\nSorted Names :\n");
 for(i=0;i<10;i++)
  puts(str[i]);
 return 0;
}

Locate a word in String - STRSTR



Locate a word in String - STRSTR

/* Locate a word in String - STRSTR */

#include<stdio.h>

int main()
{
 char str1[100], str2[100];
 int i,j,k;
 printf("Please enter first string: ");
 // gets(str1); 
 // fgets is a better option over gets to read multiword string .
 fgets(str1, 100, stdin);
 // Following can be added for extra precaution for '\n' character
 // if(str1[length(str1)-1] == '\n') str1[strlen(str1)-1]=NULL;

 printf("Please enter a word: ");
 // gets(str2); 
 fgets(str2, 100, stdin);
 // if(str2[length(str2)-1] == '\n') str2[strlen(str2)-1]=NULL;
 
 for(i=0;str1[i]!=NULL;i++)
 {
  for(k=i,j=0;str2[j]!=NULL;j++,k++)
   if(str1[k]!=str2[j])
    break;
  if(str2[j]==NULL)
  {
   printf("\"%s\" is the sub string of \"%s\" at %d location",str2,str1,k+1);
   i=-1;
   break;
  }
 }
 if(i!=-1)
  printf("\"%s\" is not the sub string of \"%s\"",str2,str1);

 return 0;
}
 

Locate a character in String - STRCHR



Locate a character in String - STRCHR

/* Locate a character in String - STRCHR */

#include<stdio.h>

int main()
{
 char str[500];
 int i;
 char ch;
 printf("Please enter your string: ");
 // gets(str); 
 // fgets is a better option over gets to read multiword string .
 fgets(str, 500, stdin);
 // Following can be added for extra precaution for '\n' character
 // if(str[length(str)-1] == '\n') str[strlen(str)-1]=NULL;

 printf("Please enter a character: ");
 getchar(ch); 
 
 for(i=0;str[i]!=NULL;i++)
 {
  if(str[i]==ch)
   break;
 }
 
 if(str[i] == NULL)
  printf("%c not found in %s",ch,str);
 else
  printf("\"%c\" found in \"%s\" at %d location",ch,str,i+1);

 return 0;
}

String Concatenation - STRCAT



String Concatenation - STRCAT

/* STRING CONCATENATION - STRCAT */

#include<stdio.h>

int main()
{
 char str1[500], str2[100];
 int i, j;
 printf("Please enter first string: ");
 // gets(str); 
 // fgets is a better option over gets to read multiword string .
 fgets(str1, 500, stdin);
 // Following can be added for extra precaution for '\n' character
 // if(str1[length(str1)-1] == '\n') str1[strlen(str1)-1]=NULL;

 printf("Please enter second string: ");
 // gets(str2); 
 fgets(str2, 100, stdin); 
 // if(str2[length(str2)-1] == '\n') str2[strlen(str2)-1]=NULL;
 
 for(i=0;str1[i]!=NULL;i++);
 for(j=0;str2[j]!=NULL;j++)
 {
  str1[i++]=str2[j];
 }
 str1[i]=NULL;
 
 printf("Concatenated string is: %s",str1);

 return 0;
}

String Reverse - STRREV



String Reverse - STRREV

/* STRING REVERSE - STRREV */

#include<stdio.h>

int main()
{
 char str[100];
 int i,j;
 char c;
 printf("Please enter a string: ");
 // gets(str); 
 // fgets is a better option over gets to read multiword string .
 fgets(str, 100, stdin);
 // Following can be added for extra precaution for '\n' character
 // if(str[length(str)-1] == '\n') str[strlen(str)-1]=NULL;
   
 for(i=0;str[i];i++);
 for(j=0,i--;j<i;j++,i--)
 {
  c=str[i];
  str[i]=str[j];
  str[j]=c;
 }

 printf("Reversed string is: %s",str);

 return 0;
}

String Compare Ignore Case - STRICMP



String Compare Ignore Case - STRICMP

/* STRING COMPARE IGNORE CASE - STRICMP */

#include<stdio.h>

int main()
{
 char str1[100], str2[100];
 int i, d=0, flag=0;
 char x,y;
 printf("Please enter first string: ");
 // gets(str1); 
 // fgets is a better option over gets to read multiword string .
 fgets(str1, 100, stdin);
 // Following can be added for extra precaution for '\n' character
 // if(str1[length(str1)-1] == '\n') str1[strlen(str1)-1]=NULL;

 printf("Please enter second string: ");
 // gets(str2); 
 fgets(str2, 100, stdin);
 // if(str2[length(str2)-1] == '\n') str2[strlen(str2)-1]=NULL;

 for(i=0;str1[i]!=NULL&&str2[i]!=NULL;i++)
 {
  x=str1[i];
  y=str2[i];
  if(x>='a'&&x<='z')
   x-=32;
  if(y>='a'&&y<='z')
   y-=32;
  if(x!=y)
  {
   break;
   flag=1;
  }
 }
 if(flag=0)
 {
  x=str1[i];
  y=str2[i];
  if(x>='a'&&x<='z')
   x-=32;
  if(y>='a'&&y<='z')
   y-=32; 
 }
 d=x-y;
 
 if(d==0)
  printf("Strings are equal");
 else
  printf("Strings are not equal");

 return 0;
}

String Compare - STRCMP



String Compare - STRCMP

/* STRING COMPARE - STRCMP */

#include<stdio.h>

int main()
{
 char str1[100], str2[100];
 int i,d=0;
 printf("Please enter first string: ");
 // gets(str1); 
 // fgets is a better option over gets to read multiword string .
 fgets(str1, 100, stdin);
 // Following can be added for extra precaution for '\n' character
 // if(str1[length(str1)-1] == '\n') str1[strlen(str1)-1]=NULL;

 printf("Please enter second string: ");
 // gets(str2); 
 fgets(str2, 100, stdin);
 // if(str2[length(str2)-1] == '\n') str2[strlen(str2)-1]=NULL;

 for(i=0;str1[i]!=NULL&&str2[i]!=NULL;i++)
 {
  if(str1[i]!=str2[i])
   break;
 }
 d=str1[i]-str2[i];
 
 if(d==0)
  printf("Strings are equal");
 else
  printf("Strings are not equal");

 return 0;
}