React Native Developer

Friday, December 21, 2018

BM PRACTICAL PROGRAM IN GTU

//(a) Write a program to generate all Prime Numbers between 1 and a given integer N. a
#include<stdio.h>

int main()
{
   int n, i, j, count;

   printf("Prime no.series\n");
   printf("Enter any number\n");
   scanf("%d", &n);
   printf("The prime numbers between 1 to %d\n",n);
   for(i = 1; i <= n; i++)
   {
      count = 0;
      for(j = 1; j <=i; j++)
      if(i % j == 0)
      {
     count++;
      }
      if(count == 2)
      {
     printf("%d\t", i);
      }
   }
  return 0;
}


//Given an integer N, test whether it is a Prime Number or Composite Number. 
#include<stdio.h>
int main()
{
    int i,n,c=0;
    printf ("Enter a number \n");
    scanf ("%d",&n);
    for (i=1;i<=n;i++)
    {
        if(n%i==0)
        c=c+1;
    }
    if (c==2)
    printf ("The number is PRIME");
    else
    printf ("The number is COMPOSITE");
    return 0;



//Write a program to find Factorial of given number using i) an iterative algorithm, and ii) recursive algorithm.
//i) an iterative algorithm

#include<stdio.h> 
  
int factorial(int n) 
    int res = 1, i; 
    for (i=2; i<=n; i++) 
        res *= i; 
    return res; 
  
int main() 
    int num = 5; 
    printf("Factorial of %d is %d", num, factorial(num)); 
    return 0; 


//ii) recrecursive algorithm.ursive algorithm.

    #include <stdio.h>
     
    int main()
    {
      int c, n, fact = 1;
     
      printf("Enter a number to calculate its factorial\n");
      scanf("%d", &n);
     
      for (c = 1; c <= n; c++)
        fact = fact * c;
     
      printf("Factorial of %d = %d\n", n, fact);
     
      return 0;
    }


#include <stdio.h>
int main()
{
  int n, first = 0, second = 1, next, c;
  printf("Enter the number of terms\n");
  scanf("%d", &n);
  printf("First %d terms of Fibonacci series are:\n", n);
  for (c = 0; c < n; c++)
  {
    if (c <= 1)
      next = c;
    else
    {
      next = first + second;
      first = second;
      second = next;
    }
    printf("%d\n", next);
  }
  return 0;
}


//evaluate prime factors of a given positive integer


#include<stdio.h>

int main()
{
    int num,i=1,j,k;
    
    printf("\nEnter a number:");/* enter a positive integer */
    scanf("%d",&num);
    while(i<=num)
    {
        k=0;
        if(num%i==0)    // checks for factor
        {
            j=1;
            while(j<=i)
            {
                if(i%j==0)
                {
                    k++;
                }
                j++;
            }
            if(k==2)            /* checks for prime factor */
                printf("\n%d is a prime factor",i);
        }
        i++;
    }
    return 0;
}


//to find GCD  of two positive integer using euclidean algorithm 

#include <stdio.h>
int main()
{
    int m, n; /* given numbers */
    
    printf("Enter-two integer numbers: ");
    scanf ("%d %d", &m, &n);
    while (n > 0)
    {
        int r = m % n;
        m = n;
        n = r;
    }
    printf ("GCD = %d \n",m);
    return 0;



// Write Floor and Ceiling functions.
/*#include <stdio.h>
#include <math.h>
 #include <stdlib.h>
int main()
{
    float val;
    float fVal,cVal;
    printf("Enter a float value: ");
    scanf("%f",&val);
    fVal=floor(val);
    cVal =ceil(val);
    printf("floor value:%f \nceil value:%f\n",fVal,cVal);
    return 0;
}*/

#include <stdio.h>
#include <math.h>
 int main()
{
       float i=5.1, j=5.9, k=-5.4, l=-6.9;
       printf("floor of  %f is  %f\n", i, floor(i));
       printf("floor of  %f is  %f\n", j, floor(j));
       printf("floor of  %f is  %f\n", k, floor(k));
       printf("floor of  %f is  %f\n", l, floor(l));
       return 0;
}
#include <stdio.h>
#include <math.h>
 int main()
{
       float i=5.1, j=5.9, k=-5.4, l=-6.9;
       printf("floor of  %f is  %f\n", i, floor(i));
       printf("floor of  %f is  %f\n", j, floor(j));
       printf("floor of  %f is  %f\n", k, floor(k));
       printf("floor of  %f is  %f\n", l, floor(l));
       return 0;
}

//Evaluate LCM (Least Common Multiple) of two positive integers. 

#include <stdio.h>
int main()
{
   int num1, num2, maxValue;

   printf("Enter two numbers: ");
   scanf("%d %d", &num1, &num2);
   
   maxValue = (num1 > num2) ? num1 : num2;

   while(1)  
   {
      if ((maxValue % num1 == 0) && (maxValue % num2 == 0))
      {
         printf("LCM: %d", maxValue);
         break;
      }
      ++maxValue;
   }
   return 0;
}


//Convert a given Integer in decimal number system into Binary, Octal, and Hexadecimal number.
//decimal to binary
#include <stdio.h>

int main()
{
    long long decimal, tempDecimal, binary;
    int rem, place = 1;
    binary = 0;
    printf("Enter any decimal number: ");
    scanf("%lld", &decimal);
    tempDecimal = decimal;
    while(tempDecimal > 0)
    {
        rem = tempDecimal % 2;
        binary = (rem * place) + binary;
        tempDecimal /= 2;
        place *= 10;
    }
    printf("Decimal number = %lld\n", decimal);
    printf("Binary number = %lld", binary);
    return 0;
}


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

int convertDecimalToOctal(int decimalNumber)
{
    int octalNumber = 0, i = 1;

    while (decimalNumber != 0)
    {
        octalNumber += (decimalNumber % 8) * i;
        decimalNumber /= 8;
        i *= 10;
    }

    return octalNumber;
}  

int main()
{
    int decimalNumber;

    printf("Enter a decimal number: ");
    scanf("%d", &decimalNumber);

    printf("%d in decimal = %d in octal", decimalNumber, convertDecimalToOctal(decimalNumber));

    return 0;
}

#include <stdio.h>
int main()
{
    long decimalnum, quotient, remainder;
    int i, j = 0;
    char hexadecimalnum[100];
    printf("Enter decimal number: ");
    scanf("%ld", &decimalnum);
    quotient = decimalnum;
    while (quotient != 0)
    {
        remainder = quotient % 16;
        if (remainder < 10)
            hexadecimalnum[j++] = 48 + remainder;
        else
            hexadecimalnum[j++] = 55 + remainder;
        quotient = quotient / 16;
    }
    for (i = j; i >= 0; i--)
            printf("%c", hexadecimalnum[i]);
    return 0;
}

*********************************************************************************

#include <stdio.h>
int main()
{
    int i, j,m,n, c, d, first[10][10], second[10][10], sum[10][10];
     
    printf("Enter number of rows : ");
    scanf("%d", &m);
    printf("Enter number of columns : ");
    scanf("%d", &n);
    printf("Enter the elements of first matrix\n");
     
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("Enter data in [%d][%d]: ", i, j);
            scanf("%d", &first[i][j]);
        }
    }
     printf("Enter the elements of second matrix\n");
     
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("Enter data in [%d][%d]: ", i, j);
            scanf("%d", &second[i][j]);
        }
    }
       
       printf("Sum of entered matrices:-\n");
       
       for (c = 0; c < m; c++) {
          for (d = 0 ; d < n; d++) {
             sum[c][d] = first[c][d] + second[c][d];
             printf("%d\t", sum[c][d]);
          }
          printf("\n");
       }
     
       return 0;
    }


//Equal Set
#include<stdio.h>
void main()
{
int a[4]={},b[5]={},j,i;
for(i=0;i<5;i++)
{
scanf("%d\n",&a[i]);
}
printf("Enter value for b");
for(j=0;j<5;j++)
{
scanf("%d\n",&b[j]);
}
/*if(a[i]=b[j])
printf("Equal");
else
printf("Not equal");
/*for(i=0;i<5;i++)
{
printf("%d\n",a[i]);
printf("%d\n",b[j];
}*/
}

//odd even
#include<stdio.h>
void main()
{
int a;
printf("enter any value\n");
scanf("%d",&a);
if(a % 2 == 0)
{
printf("%d  No is even\n",a);
}
else
{
printf("%d No is odd\n",a);
}
}

//Intersection
#include<stdio.h>

int main()

{
int arr1[]={1,2,4,5,7};

int arr2[]={2,3,5,7};
printf("sizeof 0 is %d",sizeof(arr1[0]));

int m=sizeof(arr1)/sizeof(arr1[0]);
int n=sizeof(arr2)/sizeof(arr2[0]);
int i=0,j=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(arr1[i]==arr2[j])
{
printf("%d\t",arr1[i]);
}
}
0;
}

MATRIX
#include <stdio.h>

int main()

{
    int i, j, m, n;
    
int matrix[10][20];

    
printf("Enter number of rows : ");
    
scanf("%d", &m);
    
printf("Enter number of columns : ");
   
 scanf("%d", &n);

    

/* Input data in matrix */
    
for (i = 0; i < m; i++)
    
{
       
 for (j = 0; j < n; j++)
       
 {
           
 printf("Enter data in [%d][%d]: ", i, j);
            
scanf("%d", &matrix[i][j]);
       
 }
   
 }

   
 /* Display the matrix */
    
for (i = 0; i < m; i++)
  
  {
        
for (j = 0; j < n; j++)
       
 {
          
  printf("%d\t", matrix[i][j]);
       
 }
        
printf("\n");
  
  }

    
return 0;

}

SUBSTRACTION
#include <stdio.h>
int main()
{
    int i, j,m,n, c, d, first[10][10], second[10][10], sum[10][10];
     
    printf("Enter number of rows : ");
    scanf("%d", &m);
    printf("Enter number of columns : ");
    scanf("%d", &n);
    printf("Enter the elements of first matrix\n");
     
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("Enter data in [%d][%d]: ", i, j);
            scanf("%d", &first[i][j]);
        }
    }
     printf("Enter the elements of second matrix\n");
     
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("Enter data in [%d][%d]: ", i, j);
            scanf("%d", &second[i][j]);
        }
    }
       
       printf("Sum of entered matrices:-\n");
       
       for (c = 0; c < m; c++) {
          for (d = 0 ; d < n; d++) {
             sum[c][d] = first[c][d] - second[c][d];
             printf("%d\t", sum[c][d]);
          }
          printf("\n");
       }
     
       return 0;
    }
SYMETRIC MATRIX
#include <stdio.h>
int main()
{
int arr1[] = {2,4,5,7,8,10,12,15};
int arr2[] = {5,8,11,12,14,15};
int n = sizeof(arr1)/sizeof(arr1[0]);
int m = sizeof(arr2)/sizeof(arr2[0]);
int i= 0; j=0;
while(i<n && j<m)
{
if(arr1[i] <arr2[j])
{
printf("%d\t",arr1[i]);
i++;
}

else if(arr2[j] <arr1[i])
{
printf("%d\t",arr2[j]);
j++;
}
else
{
i++;
j++;
}
}
return 0;
}



No comments:

Post a Comment