C-Tutorial

Latest

Sunday, 8 December 2019

sine series 1-x^2/2!+x^4/4!-x^6/6!+.....

Sunday, December 08, 2019 0

/* Program for sine series 1-x^2/2!+x^4/4!-x^6/6!+..... */

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
    double x, sum = 1, m;
      int n, i, j, fact, y=2, term=1;

    clrscr();

    printf("\n Enter x value: ");
    scanf("%lf",&x);
   printf("\n Enter no.of terms: ");
   scanf("%d",&n);

    for (i = 1; i < n; i++) {
        fact = 1;
        for (j = 1; j <= y; j++) {
            fact = fact * j;
        }
        term = term * (-1);
        m = term * pow(x, y) / fact;
        sum = sum + m;
        y += 2;
    }

    printf("\n Sine series: %.2lf",sum);

   getch();
}

Read More

program to convert hexadecimal to decimal

Sunday, December 08, 2019 0

/* program to convert number from hexadecimal to decimal*/

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

void main()
{
    char hex[32]={0};
    int  dec = 0, i, cnt = 0, dig;
    clrscr();
    printf("Enter hexa decimal value: ");
    gets(hex);
    for(i=(strlen(hex)-1);i>=0;i--)
    {
        switch(hex[i])
        {
            case 'A':
                dig=10; break;
            case 'B':
                dig=11; break;
            case 'C':
                dig=12; break;
            case 'D':
                dig=13; break;
            case 'E':
                dig=14; break;
            case 'F':
                dig=15; break;
            default:
                dig=hex[i]-48;
        }
        dec= dec + (dig)*pow((double)16,(double)cnt);
        cnt++;
    }
    printf("DECIMAL value is: %d", dec);
    getch();
}

Read More

Program for convert binary to decimal

Sunday, December 08, 2019 0

/*  Program for convert binary to decimal */

#include <stdio.h>
#include <math.h>

void main()
{
    long int n;
    int dec = 0, temp = 0, r;

    clrscr();

    printf("Enter a binary number: ");
    scanf("%ld", &n);

    while (n!=0)
    {
        r = n % 10;
        n = n / 10;
        dec = dec + r * pow(2,temp);
        temp++;
    }

    printf("\n\n Equivalent decimal number is: %d", dec);

   getch();
}

Read More

Program for convert octal to decimal

Sunday, December 08, 2019 0

/*  Program for convert octal to decimal */

#include <stdio.h>
#include <math.h>

void main()
{
    long int n;
    int dec = 0, temp = 0, r;

    clrscr();

    printf("Enter a octal number: ");
    scanf("%ld", &n);

    while (n!=0)
    {
        r = n % 10;
        n = n / 10;
        dec = dec + r * pow(8,temp);
        temp++;
    }

    printf("\n\n Equivalent decimal number is: %d", dec);

   getch();
}

Read More

convert decimal to binary / octal / hexadecimal

Sunday, December 08, 2019 0

/* Program for convert decimal to binary / octal / hexadecimal */

#include <stdio.h>
#include <conio.h>

void main()
{
long n;
int i=0,a[20],b;

clrscr();

printf("\n Enter a decimal value: ");
scanf("%ld", &n);
printf("\n Enter base value (2, 8, 16): ");
scanf("%d", &b);

while(n!=0)
{
a[i] = n % b;
n = n/b;
i++;
}

if(b==2) {
printf("\n After conversion binary value: ");
}
else if(b==8) {
printf("\n After conversion octal value: ");
}
else {
printf("\n After conversion hexadecimal value: ");
}

for(i=i-1;i>=0;i--)
printf("%d",a[i]);

getch();
}

Read More

program for histogram given array elements

Sunday, December 08, 2019 0

/* program printing stars equal to each element in the list */

#include<stdio.h>
#include<conio.h>

void main()
{
int a[20], i, j, n;
clrscr();

printf("\n Enter the how many array elements: ");
scanf("%d",&n);
printf("\n Enter %d elements: ",n);
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);

for(i=0;i<n;i++)
{
          printf("\n a[%d] = %d : ",i,a[i]);
          for(j=0;j<a[i];j++)
          printf(" *");
}

getch();

}





Read More

Array order reversal without using second array

Sunday, December 08, 2019 0

/*   Array order reversal without using second array */

#include<stdio.h>

void main()
{
   int arr[50], i, j, num, temp;

  clrscr();

   printf("\n Enter number of elements  to be entered to the array: ");
   scanf("%d", &num);
   printf("Enter %d number of elements into array arr", num);  
   for (i = 0; i < num; i++) {
      scanf("%d", &arr[i]);
   }

    j = i - 1;           
   i = 0;    
 
   while (i < j) {
      temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
      i++;                
      j--;                 
   }

    printf("\n Resultant array after reversal : ");
   for (i = 0; i < num; i++) {
      printf("%d \t", arr[i]);
   }

getch();
 }

Read More

sum of factorials in between range

Sunday, December 08, 2019 0

/* sum of factorials in between m and n */

#include<stdio.h>
#include<conio.h>

void main()
{
int i, j, m, n, fact=1, sum=0;
clrscr();
printf("\n Enter the starting and ending point :");
scanf("%d%d", &m, &n);
for(i=m ;i<=n ;i++)
{
fact=1;
for(j=1; j<=i; j++)
{
fact = fact * j ;
}
sum = sum + fact;
}
printf("\n The sum of factorials between %d and %d is: %d", m, n, sum);
getch();
}

Read More