Welcome to W3Courses
 Like Us on Facebook

Source Code

Source Code for Math Functions Sin Cos Tan ArcTan Absolute Value in C Programming

Following is the source code for math functions Absolute Value, Sin, Cos, Tan, and ArcTan:

#include <math.h>
main()
{
        printf("%d %d\n",abs(-4),abs(5));
        printf("%f %f %f\n",sin(1/6.0*M_PI),cos(1/6.0*M_PI),tan(1/6.0*M_PI));
        printf("%f\n",atan(0.5)*180/M_PI);
}

Share

Source Code for Math Functions Log10 Floor Round Power Square Root in C Programming

Following is the source code for math functions Log10, Floor, Round, Power, and Square Root:

#include <math.h>
main()
{
        printf("%f %f\n",log10(64)/log10(2),pow(32,0.2));
        printf("%f %f\n",floor(6.6),rint(6.6));
        printf("%f\n",pow(12,3));
        printf("%f\n",sqrt(25));
}

Share

Source Code for Calculating Factorial of a Number using a For Loop in C Programming

Following is the source code for calculating the factorial of a number using a For Loop in C Programming.

Share

Source Code for Calculating Factorial of a Number using a DO WHILE Loop in C Programming

Following is the source code for calculating the factorial of a number using a DO WHILE Loop in C Programming.

main()
{
  int a,b,i;
  printf("Enter a number between 0 and 33: ");
  scanf("%d",&i);
  b=1;
  a=1;
  if(i>=1)
  do
  { b*=a;
    a++;
  }while(a<=i);
   printf("%d\n",b);
}

Share

Source Code for Calculating Factorial of a Number using a Recursive Function in C Programming

Following is the source code for calculating the factorial of a number using a Recursive Function in C Programming.

int f(int n)
{
  if (n==0)
    return 1;
  if (n>0)
    return n*f(n-1);
}
main()
{
  int n;
  printf("Enter a number between 0 and 33: ");
  scanf("%d",&n);
  printf("%d\n",f(n));
}

Share

Source Code for Calculating Factorial of a Number using a WHILE Loop in C Programming

Following is the source code for calculating the factorial of a number using a WHILE Loop in C Programming.

main()
{
  int a,b;
  printf("Enter a number between 0 and 33: ");
  scanf("%d",&a);
  b=1;
  while(a>=1)
   { b*=a;
     a--;
   }
  printf("%d\n",b);
}

Share

Source Code to Change any Number in Base 10 to Base 2 or Binary in C Programming

Following source code will change any number in base 10 to base 2 or binary in C Programming.

void printit(int num)
{
 if (num)
 {
  int rem = num%2;
  printit(num/2);
  putchar('0'+rem);
 }
}
main()
{
 int num;
 printf("Enter a number: ");
 scanf("%d", &num);
 printit(num);
 printf(" base 2\n");
}

Share

Source Code for the Tribonacci Function in C Programming

Following is the source code for the Tribonacci Function in C Programming

int f (int n)
{  if (n<3)
       return 1;
    if (n>=3)
        return f(n-1) + f(n-2) + f(n-3);
}
main()
{  int n;
    printf ("Enter a number: ");
    scanf ("%d", &n);
    printf ("%d\n", f(n));
}

Share

Source Code to Print out the Length of a String in C Programming

The following source code will print out the length of a string in C Programming

#include <stdio.h>
main()
{  char s[81];
printf("Enter string: ");
scanf("%s", s);
printf("length = %d\n", len(s));
}
int len(char s[])
{  if(*s)
        return 1+len(++s);
return 0;
}

Share

Source Code for Writing to and Reading from a File in C Programming

Following is the three different type of source code for writing to and reading from a file using C programming

1)
#include <stdio.h>
int main()
{
   char c;
   FILE *fpntr;
   fpntr = fopen("textFile","r");
   while(fscanf(fpntr,"%c", &c)==1)
        putchar(c);
   fclose(fpntr);
}

Share