NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

//Random Number Generator:
#include <stdio.h>
int Q=100, a=17, c=43;
int X0 = 0;
int random() {
X0 = (a * X0 + c) % Q;
return X0;
}
int main() {
int i, n;
printf("Enter the number of random numbers to generate: ");
scanf("%d", &n);
printf("Enter any random number: ");
scanf("%d", &X0);
printf("n Random Numbers are:n");
for (i = 0; i < n; ++i) {
printf("%dn", random());
}
return 0;
}



//Euclidean Algorithm to find GCD
#include<stdio.h>
int euclidean(int a,int b){
int r;
while(b!=0){
r=a%b;
a=b;
b=r;
}
return a;
}

int main(){
int a,b;
printf("n Enter two numbers ");
scanf("%d%d",&a,&b);
printf("GCD(%d,%d)=%d ",a,b,euclidean(a,b));
}


// Extended Euclidean algorithm
#include <stdio.h>
int extendedEuclidean(int a, int b, int *x, int *y) {
if (b == 0) {
*x = 1;
*y = 0;
return a;
}
int x1, y1;
int gcd = extendedEuclidean(b, a % b, &x1, &y1);
*x = y1;
*y = x1 - (a / b) * y1;
return gcd;
}
int main() {
int a, b, x, y;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
int gcd = extendedEuclidean(a, b, &x, &y);
printf("GCD of %d and %d is %dn", a, b, gcd);
printf("Coefficients: x = %d, y = %dn", x, y);
return 0;
}


//Sum of two binary numbers
#include<stdio.h>
#include<string.h>
void addBinary(char a[], char b[],char result[]){
int i=strlen(a)-1;
int j=strlen(b)-1;
int carry=0, sum=0,l;
int k=(i>j?i:j)+1;
result[k+1]='';
k--;
while(i>=0||j>=0||carry==1){
sum=carry;
if(i>=0) sum+=a[i--]-'0';
if(j>=0) sum+=b[j--]-'0';
carry=sum>>1;
result[k--] = (sum&1)+'0';
}
if(k==0){
for(l=0;result[l+1]!='';l++){
result[l]=result[l+1];
}
result[strlen(result)-1]='';
}
}
int main(){
char a[100],b[100],result[101];
printf("Enter first binary number: ");
scanf("%s",a);
printf("Enter second binary number: ");
scanf("%s",b);
addBinary(a,b,result);
printf("Sum of the binary numbers is: %sn",result);
return 0;
}





//Union of two sets:
#include<stdio.h>
#define max 100
int count=0;
void input(int a[], int n){
int i;
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
}
void display(int a[], int n){
int i;
for(i=0;i<n;i++){
printf("%dt",a[i]);
}
}
void unionOfSet(int a[], int b[], int c[], int m, int n){
int i,j;
count = 0;
for(i=0;i<m;i++){
c[i] = a[i];
count++;
}
for(i=0;i<n;i++){
for(j=0;j<count;j++){
if(b[i] == c[j]){
break;
}
}
if(b[i] != c[j]){
c[count] = b[i];
count++;
}
}
}
int main(){
int a[max], b[max], c[max],m,n,i;
printf("nEnter the size of set A: ");
scanf("%d",&m);
printf("nEnter the size of set B: ");
scanf("%d",&n);
printf("nEnter the elements of set A: n");
input(a,m);
printf("nEnter the elements of set B: n");
input(b,n);
unionOfSet(a, b, c, m, n);
printf("nAUB is:n");
display(c,count);
return 0;
}



//Intersection of two sets
#include<stdio.h>
#define max 100
int count=0;

void input(int a[], int n){
int i;
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
}
void display(int a[], int n){
int i;
for(i=0;i<n;i++){
printf("%dt",a[i]);
}
}
void intersection(int a[], int b[], int c[], int m, int n){
int i,j;
count = 0;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(a[i]==b[j]){
c[count] = a[i];
count++;
break;
}
}
}
}


int main(){
int a[max], b[max], c[max],m,n,i;
printf("nEnter the size of set A: ");
scanf("%d",&m);
printf("nEnter the size of set B: ");
scanf("%d",&n);
printf("nEnter the elements of set A: n");
input(a,m);
printf("nEnter the elements of set B: n");
input(b,n);
intersection(a, b, c, m, n);
printf("nAnB is:n");
display(c,count);
return 0;
}




//Difference of two sets
#include<stdio.h>
#include<conio.h>
#define MAX 40
void input(int set[], int);
int SetDifference(int a[],int, int B[],int, int C[]);
int main()
{
int a[MAX],B[MAX],C[MAX],L,i;
int N,M;
printf("nNo. of elements in the Minuend (set A): ");
scanf("%d",&M);
printf("nNo. of elements in the Subtrahend (set B) : ");
scanf("%d",&N);
printf("Enter elements of Minuend (Set A)");
input(B,M);
printf("Enter elements of Subtrahend (Set B)");
input(a,N);
L=SetDifference(a,N,B,M,C);
printf("n Difference of two sets: A-B: ");
for(i=0;i<L;i++)
printf("%d ",C[i]);
}
void input(int set[],int n)
{
int i,x;
printf("nEnter set elements : n");
for(i=0;i<n;i++)
scanf("%d",&set[i]);
}

int SetDifference(int a[],int n, int B[],int m,int C[]){
int k=0,i,j,flag;
for(i=0;i<m;i++){
flag=1;
for(j=0;j<n;j++) {
if(B[i]==a[j]){
flag=0;
break;
}
}
if(flag==1)
C[k++]=B[i];
}
return k--;
}




//TT of conjunction, disjunction and negation:
#include <stdio.h>
#include <stdbool.h>
bool conjuction(bool p,bool q){
return p && q;
}
bool disjunction(bool p, bool q){
return p || q;
}
bool negation(bool p){
return !p;
}
void display(bool p){
if(p==true){
printf("Ttt");
}
else{
printf("Ftt");
}
}
int main() {
bool p[10]={true,true,false,false};
bool q[10]={true, false, true, false};
int i;
printf("n---------------------------------------------------------------------n");
printf("pttqttp^qttpVqtt~pn");
printf("---------------------------------------------------------------------n");
for(i=0;i<4;i++){
display(p[i]);
display(q[i]);
display(conjuction(p[i],q[i]));
display(disjunction(p[i],q[i]));
display(negation(p[i]));
printf("n");
}
return 0;
}



//Truth table of Implication and biconditional
#include<stdio.h>
#include<stdbool.h>

bool imlication(bool p, bool q){
return !p||q;
}
bool bicondition(bool p,bool q){
return (!p||q)&&(!q||p);
}
void display(bool p){
if (p==true)
printf("Ttt");
else
printf("Ftt");
}
int main()
{
bool p[]={true,true,false,false};
bool q[]={true,false,true,false};
int i;
printf("n------------------------------------------------------------------n");
printf("PttQttP-->qttP<-->Qn");
printf("------------------------------------------------------------------n");
for(i=0;i<4;i++){
display(p[i]);
display(q[i]);
display(imlication(p[i],q[i]));
display(bicondition(p[i],q[i]));
printf("n");
}
}




//Construct TT of given proposition: (P^Q)🡪R)
#include <stdio.h>
#include <stdbool.h>
bool conjuction(bool p,bool q){
return p && q;
}
bool preposition(bool p, bool q, bool r){
return !conjuction(p, q) || r;
}
void display(bool p){
if(p==true){
printf("Ttt");
}
else{
printf("Ftt");
}
}

int main(){
bool p[10]={true, false, true, false, true, false, true, false};
bool q[10]={true, true, false, false, true, true, false, false};
bool r[10]={true, true, true, true, false, false, false, false};
int i;
printf("Truth Table of the proposition (P ^ Q) --> R.n");
printf("n----------------------------------------------------------------------------n");
printf("PttQttRttP^Qtt(P^Q)-->Rn");
printf("----------------------------------------------------------------------------n");
for(i=0; i<8; i++){
display(p[i]);
display(q[i]);
display(r[i]);
display(conjuction(p[i],q[i]));
display(preposition(p[i],q[i],r[i]));
printf("n");
}
}




//Check Tautology, Contradiction or Contengency:
#include <stdio.h>
#include <stdbool.h>
bool disjunction(bool p,bool q){
return p || q;
}
bool proposition(bool p, bool q, bool r){
return !disjunction(p, q) || r;
}
void display(bool p){
if(p==true){
printf("Ttt");
}
else{
printf("Ftt");
}
}

int main(){
int i;
bool p[10]={true, false, true, false, true, false, true, false};
bool q[10]={true, true, false, false, true, true, false, false};
bool r[10]={true, true, true, true, false, false, false, false};
bool isTautology = true;
bool isContradiction = true;
printf("Truth Table of the proposition (P V Q) --> R.n");
printf("n----------------------------------------------------------------------------n");
printf("PttQttRttPVQtt(PVQ)-->Rn");
printf("----------------------------------------------------------------------------n");
for (i = 0; i < 8; i++) {
bool result = proposition(p[i], q[i], r[i]);
display(p[i]);
display(q[i]);
display(r[i]);
display(disjunction(p[i],q[i]));
display(proposition(p[i],q[i],r[i]));
printf("n");
if (!result) {
isTautology = false;
}
if (result) {
isContradiction = false;
}
}

if (isTautology) {
printf("nThe proposition is a Tautology.n");
} else if (isContradiction) {
printf("nThe proposition is a Contradiction.n");
} else {
printf("nThe proposition is a Contingency.n");
}
return 0;
}



//Sum of Natural Numbers:
#include<stdio.h>
int Sum(int n){
int sn=0;
if (n<=0){
return 0;
}
else(n>=1);
return n+Sum(n-1);
}
int main(){
int c;
int n;
printf("Up to how many natural numbers?n");
scanf("%d",&n);
c=Sum(n);
printf("The Sum of natural number is: %d",c);
return 0;
}




//Factorial:
#include<stdio.h>

long int facto(int n){
if(n==0 || n==1){
return 1;
}
else{
return n*facto(n-1);
}
}

int main(){
int c;
int n;
printf("Factorial of?n");
scanf("%d",&n);
c=facto(n);
printf("The factorial of given number is %d",c);
return 0;
}





//Fibonacci Series, recursion:
#include<stdio.h>
int fibo(int n){
if(n==0){
return 0;
}
else if(n==1){
return 1;
}
else{
return fibo(n-2)+ fibo (n-1);
}
}
int main(){
int c;
int n;
printf("Which fibonacci number?n");
scanf("%d",&n);
c=fibo(n);
printf("The %dth fibo number is %d",n,c);
return 0;
}




//Power (a,b) Recursion:
#include <stdio.h>
int Power(int a, int b)
{
if(b==0)
return 1;
else
return(a* Power(a,b-1));
}
int main()
{
int a,b, result;
printf("Enter a and bn");
scanf("%d%d",&a,&b);
result=Power(a,b);
printf("Power(%d,%d) = %d n", a, b, result);
return 0;
}




//TOH
#include<stdio.h>
void TOH(int n, char A, char B, char C) {
if (n == 1) {
printf("Move disk 1 from rod %c to rod %cn", A, B);
return;
}
TOH(n - 1, A, C, B);
printf("Move disk %d from rod %c to rod %cn", n, A, B);
TOH(n - 1, C, B, A);
}
int main() {
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
TOH(n, 'A', 'C', 'B'); // A, B and C are names of rods
return 0;
}



//Binary Search:
#include <stdio.h>
#include <math.h>
#define Max 50
int binarySearch(int arr[], int low, int high, int key)
{
int mid;
if (high >= low)
{
mid = floor ((low + high) / 2 );
if (arr[mid] == key)
return mid;

else if (arr[mid] > key)
return binarySearch(arr, low, mid - 1, key);

else if (arr[mid] < key)
return binarySearch(arr, mid + 1, high, key);
}
return -1;
}
int main()
{
int i,low,high,key,index;
// Must be Sorted Array
int A[]={20,25,42,52,64,79,86,92,96,100};
low=0;
high=9;
printf("Array:");
for(i=0;i<=high;i++)
printf(" t %d",A[i]);
printf("nEnter the Key to be searched: ");
scanf("%d", &key);
index=binarySearch(A,low,high,key);
if(index==-1)
printf("n Data don't present in the given Array");
else
printf(" n Data present at %d index.", index);
return 0;
}



//Merge Sort:
#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 50
void Display_Array_Element( int A[MAX_SIZE], int len)
{
int i;
for(i=0; i<len; i++)
printf(" t %d ", A[i]);
}
void Merge(int A[MAX_SIZE], int left, int Mid, int right )
{
int i=0,k,p, Temp[MAX_SIZE];
k=left; p=Mid+1;
while(left<=Mid && p<=right)
{
if(A[left]< A[p])
Temp[i++]=A[left++];
else
Temp[i++]=A[p++];
}
while(left<=Mid)
Temp[i++]=A[left++];
while(p<=right)
Temp[i++]=A[p++];
for(i=k; i<=right; i++)
A[i]=Temp[i-k];
}
void Merge_Sort(int A[MAX_SIZE], int left, int right)
{
int Mid;
if(left<right)
{
Mid=(left+right)/2;
Merge_Sort(A, left, Mid);
Merge_Sort(A, Mid+1, right);
Merge(A, left, Mid,right);
}
}
void main()
{
int Len=8;// total number of elements in the array
int A[]={100,2,21,59,46,28,9,99};
printf("n Before Sorting:");
Display_Array_Element(A,Len);
Merge_Sort(A,0,Len-1);
printf("n After Sorting: ");
Display_Array_Element(A,Len);
getch();
}



     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.