Wednesday, October 5, 2016

গল্পটা প্রোগ্রামিং-এর (A tale of Programming): Tutorial 8- Data casting


#include<stdio.h>
main()
{
int i;
float f;
printf("Enter i such that i  integer:\n" );
scanf("%d",&i);
printf("Enter f such that f  floating point:\n");
scanf("%f",&f);
printf("The sum of %d and %f is = %f \n \n",i,f,(float)(i+f) );
}
Output




প্রোগ্রামিং-এর গল্প (A tale of Programming) : Numerical Method- Coding for Newton Raphson Method

//Program for Newton-Raphson Method
#include<stdio.h>
#include<math.h>
float f(float x)
{
    return x*x-4*x-10;
}
float fdx(float x)
{
    return 2*x-4;
}
main()
{
float x1,x2,f1,f2,e,error,root;
int i=0;
printf("Enter the initial value x1:");
scanf("%f",&x1);
printf("Enter the error limit e:");
scanf("%f",&e);
while(1)
{
i++;//i=i+1;
f1=f(x1);
f2=fdx(x1);
x2=x1-(f1/f2);
error=fabs((x2-x1)/x2);
if(error<e)
{
    root=x2;
    printf("\n\n\n\nNo. of iteration:%d",i-1);
    printf("\n\nFinal functional value:%f",f1);
    printf("\n\nFinal root:%f",root);
    break;
}
else
{
    x1=x2;
    printf("\n\nIteration=%d  x1=%.2f  x2=%.2f  f1=%.2f  f2=%.2f  error=%.2f",i,x1,x2,f1,f2,error);
}

}

}
Output for Newton Raphson Method

প্রোগ্রামিং-এর গল্প (A tale of Programming) : Numerical Method- Coding for False Position Method

//program for false position
#include<stdio.h>
#include<math.h>
float f(float x)
{
    return x*x-4*x-10;
}
main()
{
    float xl,xu,x0,fxl,fxu,fx0,e,error,root;
    int i=0;
    printf("Enter lower limit xl:");
    scanf("%f",&xl);
    printf("Enter upper limit xu:");
    scanf("%f",&xu);
    printf("Enter limit of error:");
    scanf("%f",&e);
    fxl=f(xl);
    fxu=f(xu);
    if(fxl*fxu>0)
    {
        printf("\nSorry, the limits doesn't bracket any root:");
        main();
    }
    else//    if(fxl*fxu<0)
    {
        printf("\n\n\nIteration  xl\txu\tx0\tfxl\tfxu\tfx0\terror");
        while(1)
        {
           i++;
           x0=xl-((fxl*(xl-xu))/(fxl-fxu));
           fxl=f(xl);
           fxu=f(xu);
           fx0=f(x0);
           if(fx0*fxl<0)
           {
               xu=x0;
           }
           else//           if(fx0*fxu<0)
           {
               xl=x0;
           }
           error=fabs((xl-xu)/xl);
           printf("\n%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f",i,xl,xu,x0,fxl,fxu,fx0,error);
           if(error<=e)
           {
               x0=xl-((fxl*(xl-xu))/(fxl-fxu));
               root=x0;
               printf("\n\n\n\nThe desired root is= %f\n\n",root);
               break;
           }
        }
    }
}

Output for False Position Method

Numerical Method- 3: Coding for Bisection Method (with error)

#include <stdio.h>
#include <math.h>
float fnct(float x)
{
return (x*x - 4*x -10);}
int main()
{
int i=0;
float xl, xu, x0, e = 0.0001;
float fxl,fxu,fx0,error,root;
printf("\nEnter values for xl and xu:\n");
scanf("%f %f", &xl, &xu);
fxl= fnct(xl);  fxu= fnct(xu);
if(fxl*fxu>0)
{ printf("Not in range\n");}
else
{   printf("Iter \t xl \t xu \t x0 \t fxo \t\t error \n\n ");
   while(1)
   {
        i++;
     x0 =(xl + xu)/2; fx0 = fnct(x0);
        if ( fxu* fx0 <= 0)
            xl = x0;
        else
            xu = x0;    error=fabs((xu - xl)/xl) ;
           printf("%d\t%.2f\t%.2f\t%.2f\t%f.2\t%.2f\n ",i,xl,xu,x0,fx0,error);
           if (fabs((xu - xl)/xl) < e)
         {  root = (xu+xl)/2;
            printf("Root= %6.4f\n", root);
            break;
         }
    }

}
return  0;
}


Output for Bisection Method



Numerical Method-2: Coding for Bisection Method

#include <stdio.h>
#include <math.h>
float fnct(float x)
{
return (x*x - 4*x -10);}
int main()
{
int i=0;
float xl, xu, x0, e = 0.0001;
float fxl,fxu,fx0,error,root;
printf("\nEnter values for xl and xu:\n");
scanf("%f %f", &xl, &xu);
fxl= fnct(xl);  fxu= fnct(xu);
if(fxl*fxu>0)
{ printf("Not in range\n");}
else
{   printf("Iter \t xl \t xu \t x0 \t fxo \t\t error \n\n ");
   while(1)
   {
        i++;
     x0 =(xl + xu)/2; fx0 = fnct(x0);
        if ( fxu* fx0 <= 0)
            xl = x0;
        else
            xu = x0;    error=fabs((xu - xl)/xl) ;
           printf("%d\t%.2f\t%.2f\t%.2f\t%f.2\t%.2f\n ",i,xl,xu,x0,fx0,error);
           if (fabs((xu - xl)/xl) < e)
         {  root = (xu+xl)/2;
            printf("Root= %6.4f\n", root);
            break;
         }
    }

}
return  0;
}


Output for Bisection Method



: Numerical Method- 1: Coding for Bisection Method

#include <stdio.h>
#include <math.h>
float fnct(float x)
{
return (x*x - 4*x -10);}
int main()
{
int i=0;
float xl, xu, x0, e = 0.0001;
float fxl,fxu,fx0,error,root;
printf("\nEnter values for xl and xu:\n");
scanf("%f %f", &xl, &xu);
fxl= fnct(xl);  fxu= fnct(xu);
if(fxl*fxu>0)
{ printf("Not in range\n");}
else
{   printf("Iter \t xl \t xu \t x0 \t fxo \t\t error \n\n ");
   while(1)
   {
        i++;
     x0 =(xl + xu)/2; fx0 = fnct(x0);
        if ( fxu* fx0 <= 0)
            xl = x0;
        else
            xu = x0;    error=fabs((xu - xl)/xl) ;
           printf("%d\t%.2f\t%.2f\t%.2f\t%f.2\t%.2f\n ",i,xl,xu,x0,fx0,error);
           if (fabs((xu - xl)/xl) < e)
         {  root = (xu+xl)/2;
            printf("Root= %6.4f\n", root);
            break;
         }
    }

}
return  0;
}


Output for Bisection Method



C Tutorial 8- Calculate year, months and days

#include<stdio.h>
main()
{
    int years,month,days;
    printf("Enter days so that greater than 365 days:");
    scanf("%d",&days);
    years=days/365;
    printf("%d years %d months %d days \n\n\n\n ",(days/365),((days%365)/30),((days%365)%30));
}
Output



C Tutorial 7- Calculate year, months and days

#include<stdio.h>
main()
{
    int years,month,days;
    printf("Enter days so that greater than 365 days:");
    scanf("%d",&days);
    years=days/365;
    printf("%d years %d months %d days \n\n\n\n ",(days/365),((days%365)/30),((days%365)%30));
}
Output



C Tutorial 6- Calculate months and days

#include<stdio.h>
main()
{
    int month,days;
    printf("Enter days:");
    scanf("%d",&days);
    printf("%d Months and  %d days\n\n\n",(days/30),(days%30));
}

Output

C Tutorial 5- Find modulous of two numbers

#include<stdio.h>
main()
{
int a,b,sum;
printf("Enter a such that a>b:\n" );//10
scanf("%d",&a);
printf("Enter b such that b<a:\n");//3
scanf("%d",&b);
printf("The mod of %d and %d is = %d \n \n",a,b,(a%b) );//1
}

Output

C Tutorial 4- Add two numbers and take inputs from keyboard


#include<stdio.h>
main()
{
int a,b,sum;
printf("Enter a:" );
scanf("%d",&a);
printf("Enter b:");
scanf("%d",&b);
printf("The sum of %d and %d is = %d \n \n ",a,b,(a+b) );
}
Output

C Tutorial 2- Print two sentences in two new lines


#include<stdio.h>
int main()
{
printf("Hey! I am Mubin.\nI am a good boy.\n");

return 0;
}

Output

C Tutorial 3- Add two numbers

#include<stdio.h>
main()
{
int a,b,sum;
a=2;
b=3;
sum=a+b;
printf("The sum of %d and %d is = %d  \n\n\n",a,b,sum);
}
Output

C Tutorial 1- Print your name

#include<stdio.h>int main()
{
printf("Hey! I am Mubin");//press F9
return 0;
}
Output