DS ASSIGNMENT 1(CSIT 2nd)

 DISCRETE STRUCTURE ASSIGNMENT-1

All the questions with solution and output is given below:

  • Program for factorial:-
#include<stdio.h>
int fact(int num);
int main(){
int n;
int facto;
printf("Enter any number:\n");
scanf("%d",&n);
facto=fact(n);
printf("Factorial is %d\n",facto);
}
int fact(int a)
{
if(a<=1)
return 1;
else
return a*fact(a-1);
}


Output of Factorial:


  • Program for Sum of Natural Numbers:-
#include<stdio.h>

int Sum(int n);

int main(){
int n;
int sum;
printf("Enter any number:\n");
scanf("%d",&n);
sum=Sum(n);
printf("Sum is %d\n", sum);
}

int Sum(int a)
{
if (a==0)
return 0;
else
return a + Sum(a-1);
}

Output of Sum of Natural Numbers:


  • Program to find power of numbers:-
#include<stdio.h>

int Power(int,int);

int main(){
int a,b;
int power;
printf("Enter any two numbers:\n");
scanf("%d %d", &a,&b);
power=Power(a,b);
printf("The %d to the power %d is %d\n",a,b,power);
}

int Power(int a, int b)
{
if(b==0)
return 1;
else if(b>0)
return a*Power(a,b-1);
else
return 1/Power(a,-b);
}

Output of Power of Numbers:


  • Program for Basic Gates:-
#include<stdio.h>

int AND(int, int);
int OR(int, int);
int Implication(int, int);

int main(){
int a,b;
printf("A\tB\tA^B\tAvB\tA->B\n");
for (a=0;a<=1;a++)
{
for(b=0;b<=1;b++)
{
printf("%d\t%d\t%d\t%d\t%d\n",a,b,AND(a,b),OR(a,b),Implication(a,b));
}
}
}

int AND(int a, int b)
{
if(a==1 && b==1)
return 1;
else
return 0;
}

int OR(int a, int b)
{
if (a==1 || b==1)
return 1;
else
return 0;
}

int Implication(int a, int b)
{
if (a==1 && b==0)
return 0;
else
return 1;
}


Output of Basic Gates:

        

  • Program for Fibonacci:-
#include<stdio.h>
int Fib(int n);
int main(){
int a=0, b=1, c, i, k;
printf("How many numbers do you want in the series?\n");
scanf("%d",&k);
printf("The fibonacci series is\n");
for(i=0;i<k;i++)
{
printf("%d\t",Fib(i));
}
return 0;
}
int Fib(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return(Fib(n-1)+Fib(n-2));
}

Output of Fibonacci:

  • Program for GCD:-
#include<stdio.h>

int gcd(int, int);

int main(){
int a,b,g;
printf("Enter first and Second number:\n");
scanf("%d %d",&a,&b);
g=gcd(a,b);
printf("GCD of %d and %d is %d", a,b,g);
return 0;
}

int gcd(int a, int b)
{
if(b==0)
return a;
else
return gcd(b, a%b);
}

Output of GCD:


  • Program for Extended Euclidean Algorithm:-
#include<stdio.h>

int EGCD(int, int, int*, int*);

int main()
{
int a, b, x, y, gcd;
printf("Enter Two integers:\n");
scanf("%d %d", &a, &b);
gcd = EGCD(a, b, &x, &y);
printf("GCD(%d, %d) = %d, x = %d, y = %d\n", a, b, gcd, x, y);
return 0;
}

int EGCD(int a, int b, int *x, int *y)
{
if (a == 0)
{
*x = 0;
*y = 1;
return b;
}
int x1, y1;
int gcd = EGCD(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}


Output of Extended Euclidean Algorithm:



  • Program for Binary Search:-
#include <stdio.h>

int binarysearch(int arr[], int left, int right, int target)
{
if (left > right)
{
return -1;
}
int mid = left + (right - left) / 2;
if (arr[mid] == target)
{
return mid;
}
if (target < arr[mid])
{
return binarysearch(arr, left, mid - 1, target);
}
return binarysearch(arr, mid + 1, right, target);
}

int main()
{
int n, target;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d sorted elements:\n", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &target);
int result = binarysearch(arr, 0, n - 1, target);
if (result != -1)
{
printf("Element found at index %d\n", result);
}
else
{
printf("Element not found\n");
}
return 0;
}

Output of Binary Search:


These were all the program and output of DS's Assignment-1.

 ******

May Devine Goddess protect you all ! Keep faith and work hard..

! "GOOD LUCK" !


No need to thank, but you can definitely help UJJU_Rizz-ALL by listening this vid full:


Name: Ujjwal Rijal  

Address: Earth

Instagram: www.instagram.com/rijalujjwal87

Twitter: www.x.com/ujjwalreezal

Facebook: www.facebook.com/UjjuGod

Youtube : www.youtube.com/@fyaste








Comments

Popular posts from this blog

CONQUER Digital Logics(1st SEM) EXAM TARGATED ! [SUJAL'S FAREWELL SPECIAL]

STAT-II GUIDE FOR BOARD AND QT BY UJJU