555
int divide1(int a,int b) { int t=1; while(b*t<=a) { t++; } return t-1; }
The set A union B can be decomposed into three disjoint sub sets A\ (A int B), B\(A int B), and (A int B). So in this case (A union B) and (A int B) are fixed but "moving" elelments from A\ (A int B) into B\(A int B) will not affect (A union B) and (A int B). You should be able to fill in the details now.
#include <stdlib.h> #include <stdio.h> #include <conio.h> int sum(int, int); int sub(int, int); int mul(int, int); int div1(int, int); int main() { int a,b,d; int ch; printf("\t\tCalculator Implementation\n"); printf("\t\t~\n"); printf("\t\t 1: Add\n"); printf("\t\t 2: Sub\n"); printf("\t\t 3: Multiply\n"); printf("\t\t 4: Divide\n"); printf("\t\t 5: Exit\n\n\n"); printf("\t\t Enter the choice: "); scanf("%d", &ch); if(ch==5) exit(0); printf("\nEnter first number: "); scanf("%d", &a); printf("\nEnter Second number: "); scanf("%d", &b); printf("\na = %d; b = %d;\n", a, b); printf("==============\n\n"); switch(ch) { case 1 : d=sum(a,b); printf("Sum %d + %d = %d",a,b,d); break; case 2: d=sub(a,b); printf("Subtraction %d - %d = %d",a,b,d); break; case 3: d=mul(a,b); printf("Multiplication %d * %d = %d",a,b,d); break; case 4 : d=div1(a,b); printf("Division %d / %d = %d",a,b,d); break; default: printf("Invalid Choice!"); getch(); } getch(); return 0; } int sum(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int mul(int a, int b) { return a * b; } int div1(int a, int b) { return a / b; }
#include<stdio.h> int main() { int a,b,c,d; for(a=1; a<5; a++) { for(b=1; b<5; b++) { for(c=1; c<5; c++) { for(d=1; d<5; d++) { if(!(a==b a==c a==d b==c b==d c==d)) printf("dd\n",a,b,c,d); } } } } return 0; }
555
int a = 1 + 2; ---------- int a = 1; int b = 2; a += b;
int gcd (int a, int b) { if (a<0) a= -a; if (b<0) b= -b; while (a!=0 && b!=0 && a!=1 && b!=1) { int tmp; if (a>b) tmp= b, b= a, a= tmp; a= a%b; } if (a==0) return b; else if (b==0) return a; else return 1; }
int pow (int a, int b) { if (b==0) return 1; else return a*pow(a,b-1); }
Java solutionpublic static final int[] getMiddle(final int[] a, final int[] b) {return new int[] {a[1], b[1]};}C solution void getMiddle(const int a[], const int b[], int ret[]) {ret[0] = a[1];ret[1] = b[1];}
// Swapping values of a and b int a = 1; int b = 50; int temp = a; // temp = 1 a = b; // a = 50 b = temp; // b = 1
//with a function: int exponent(int a, int b){ int returnValue; if (x==0) return 1; else return(a*exponent(a,x-1)); } main(){ printf("%d\n", exponent(1, 1)); }
I suppose you wanted to ask: how to do multiplication with repeated addition. int mul (int a, int b) { int sum= 0; int sign= 1; if (b<0) { sign= -1; b= -b;} for (; b; --b) sum = sum + a; return sign*sum; }
int a = 1; int b = 2; int c = a + b; // Sum
The following shows how this can be achieved: typedef struct fraction { int n; // numerator int d; // denominator }; struct fraction subtract (struct fraction a, struct fraction b) { struct fraction r; // result r.d = lcm (a.d, b.d); // lowest common multiple r.n = (a.n * (r.d/a.d)) - (b.n * (r.d/b.d)); return r; } The lcm() function has the following definition: int lcm (int a, int b) { int sign = 1; if (a<0) a*=-1, sign *=(-1); if (b<0) b*=-1, sign *=(-1); return (a*b) / gcd (a,b) * sign; } The gcd() function (greatest common divisor) has the following definition: int gcd (int a, int b) { int sign = 1; if (a<0) a*=-1, sign *=(-1); if (b<0) b*=-1, sign *=(-1); while (a!=b) a>b?a-=b:b-=a; return a * sign; }
#include <iostream> int main (void) { std::cout << "\nBitwise AND (&):\n"; for (int a=0; a<=1; ++a) for (int b=0; b<=1; ++b) std::cout << a << " & " << b << " is " << a & b << std::endl; std::cout << "\nLogical AND (&&):\n"; for (int a=0; a<=1; ++a) for (int b=0; b<=1; ++b) std::cout << a << " && " << b << " is " << a && b << std::endl; return 0; }
There are two ways in which you can swap without a third variable. 1. Using xor operation swap( int *a, int *b) { *a = *a ^ *b; *b = *a ^ *b; *a = *a ^ *b; } 2. Using addition and subtraction swap( int *a, int *b) { *a = *a + *b; *b = *a - *b; *a = *a - *b; } }