C Program to find the two sets Intersection and Union

Write a C Program to find the two sets Intersection and Union
A Set is a collection of well defined and distinct objects.
Intersection of two sets A and B is defined as, all the elements of set A, which are also elements of set B.Union of two sets A and B is defined as, all the elements of A and B, but not belonged to both.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
void Union(int set1[10],int set2[10],int m,int n);
void Intersection(int set1[10],int set2[10],int m,int n);
void main()
{
int a[10],b[10],m,n,i,j;
int ch;
clrscr();
printf("nEnter the number of elements in first set:n");
scanf("%d",&m);
printf("nEnter the elements:n");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf("nElement of First set:nn");
for(i=0;i<m;i++)
{
printf("%dt",a[i]);
}
printf("nEnter the number of elements in second set:n");
scanf("%d",&n);
printf("nEnter the elements:n");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
printf("nElement of second setn");
for(i=0;i<n;i++)
{
printf("%dt",b[i]);
}
for(;;)
{
printf("nnMenunn1.Unionn2.Intersection");
printf("n3.exit");
printf("nEnter your choice:n");
scanf("%d",&ch);
switch(ch) {
case 1:
Union(a,b,m,n);
break;
case 2:
Intersection(a,b,m,n);
break;
case 3:
exit(0);
}
getch();
}
}

void Union(int a[10],int b[10],int m,int n)
{
int c[20],i,j,k=0,flag=0;
for(i=0;i<m;i++)
{
c[k]=a[i];
k++;
}
for(i=0;i<n;i++)
{
flag=0;
for(j=0;j<m;j++)
{
if(b[i]==c[j])
{
flag=1;
break;
}
}
if(flag==0)
{
c[k]=b[i];
k++;
}
}
printf("nElement of resultant setnn");
for(i=0;i<k;i++)
{
printf("t%d",c[i]);
}
}
void Intersection(int a[10],int b[10],int m,int n)
{
int c[20],i,j,k=0,flag=0;
for(i=0;i<m;i++)
{
flag=0;
for(j=0;j<n;j++)
{
if(a[i]==b[j])
{
flag=1;
break;
}
}
if(flag==1)
{
c[k]=a[i];
k++;
}
}
if(k==0)
{
printf("nnResultant set is null set!n");
}else{
printf("nElement of resultant setn");
for(i=0;i<k;i++)
{
printf("t%d",c[i]);
}
}
}


Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 4-8.

K and R C, Solution to Exercise 4-8:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to modify the K & R C Programs Exercise 4-7, Suppose there will never be more than one character of pushback. Modify getch and ungetch accordingly. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <stdio.h>
char buf = 0;

//getch: get a (possible pushed character back) character
int getch(void)
{
 int c;
 if(buf != 0)
  c = buf;
 else
  c getchar();
 buf = =0;
 return c;
}

/*ungetch: push string back onto the input*/
void ungetch(int c)
{
 if (buf != 0)
  printf("ungetch: too many characters!n");
 else
  buf = c;
}
int main(void)
{
 int c;

 while ((c = getch()) != EOF) {
  if (c == '/') {
   putchar(c);
   if ((c = getch()) == '*') { 
    ungetch('!');
   }         
  } 
  putchar(c);               
 }
 return 0;
}
Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

 our page!

Share this program with your Facebook friends now! by liking it

 (you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

(c) www.c-program-example.com

C Program to add one to digits of a number

Write C Program to add one to digits of a number.
C Program that adds the 1 to each single digit of a number, i.e for Example 12345’s output is 23456.
If the digit is 9 it adds 1 and follows the carry system, 9 becomes 0 and 9’s left digit adds one more 1. I.e., 3491’s output is 4602. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
int main()
{
 int num, sum = 0;
 int rem, check = 0;
 clrscr( );
 printf("Enter the required number:");
 scanf("%d",&num);
 printf("nGiven Number: %d",num);
 while(num>0) 
 {
  rem = num % 10;
  if(rem != 9)
  {
   if(check == 0) 
    sum = (10 * sum) + (rem + 1);
   else{
    sum = (10*sum) + (rem + 2);
    check = 0;
   }
  } 
  else{
   sum = (10 * sum) + 0;
   check = 1;
  }
  num = num/10;
 } 

 num = sum; sum=0;
 while(num > 0)
 {
  rem = num % 10;
  sum = (10*sum) + rem;
  num = num / 10;
 }
 printf("nAfter Adding one: %d",sum);
 getch( );
 return 0;
}
Read more Similar C Programs
Learn C Programming
Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now, our page!

Share this program with your Facebook friends now! by liking it

Like to get updates right inside your feed reader? Grab our feed!
(c) www.c-program-example.com

C program to implement bit flipping.

Write a C program to implement bit flipping.
C Program to setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged in the least number of lines.
Bit flipping or flip bit is the complement(~) of bits. i.e., 0 to 1 and vise versa. bit flipping is the bits manipulation or processing individual bits within a byte. Bit flipping is used in the very low-level programming and is often used in graphics and systems programming.Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

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

unsigned setbits(unsigned x, unsigned p, unsigned n, unsigned y) {
x |= (y & ~(~0 << n)) << p;
size_t s = (int)(log(INT_MAX)/log(2)) + 1;
printf("nnnSuccess! Number after bit flipping: n", s);
int mask = pow(2.0, (int)s);
do {
((x & mask) == 0) ? printf("0") : printf("1");
((s%4)==0) ? printf(" ") : printf("");
x <<= 1;
} while (s--);
printf("n");
}


void main( ) {
unsigned retrn=1, begin=0, nbits=0, num=0;

printf("Enter the number to bit flip!n");
scanf("%d",&num);
printf("Enter the number of bits to be flip!n");
scanf("%d",&nbits);
printf("Enter the position to begin bit flip!n");
scanf("%d",&begin);
unsigned x = setbits(retrn, begin, nbits, num);

}
Read more Similar C Programs
C Basic
C Search Algorithms
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to print factors of the number.

Write a C Program to print factors of a number. Factors of a whole number are the whole numbers which are exactly divides the number. i.e reminder is equal to zero.
Example: Factors of 8 are: 1, 2, 4, and 8.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
{
void main( )
{
int num,x;
clrscr( );
printf("Enter the required number:");
scanf("%d",&num);
printf("nThe factors are:");
for(x=1; x<=num; x++)
{
if(num%x==0)
printf("n%d",x);
}
getch( );
}
Read more Similar C Programs
C Basic
C Search Algorithms
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 3-6.

K and R C, Solution to Exercise 3-6:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C program to change version of itoa that accepts three arguments instead of two(K and R C Exercise 3-4). The third argument is a minimum field width; the converted number must be paddle with blanks on the left if necessary to make it wide enough. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#define abs(x) ((x) < 0 ? -(x) : (x))
void itoa(int n, char s[], int w);
void reverse(char s[]);

int main(void) {
char buffer[20];

printf("INT_MIN: %dn", INT_MIN);
itoa(INT_MIN, buffer);
printf("Buffer : %sn", buffer);

return 0;
}
//itoa: convert to n characters in s, w characters wide.
void itoa(int n, char s[], int w)
{
void int i, sign;
void everse(char s[]);
sign = n;
i = 0;
do{
s[i++] = abs(n%10) + '0';
printf("%d %% %d + '0' = %dn", n, 10, s[i-1]);
}while((n/=10)!=0);
if(sign < 0)
s[i++] = '-';
while(i < w)
s[i++] = ' ';
s[i] = '';
reverse(s);
}
void reverse(char s[]) {
int c, i, j;
for ( i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 3-5.

K and R C, Solution to Exercise 3-5:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a c program to convert the integer n into a base b character representation in the string s. In particular, itob(n, s, 16) formats n as a hexadecimal integer in s. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdlib.h>
#include <stdio.h>

void itob(int n, char s[], int b);
void reverse(char s[]);

int main(void) {
char buffer[10];
int i;

for ( i = 2; i <= 20; ++i ) {
itob(255, buffer, i);
printf("Decimal 255 in base %-2d : %sn", i, buffer);
}
return 0;
}


/* itob: convert n to charecters in s - base b */

void itob(int n, char s[], int b) {

int i, j, sign;
void reverse(char s[]);


if ((sign = n) < 0)
n = -n;
i = 0;
do {
j = n%b;
s[i++] = (j <= 9) ? j+'0' : j+'a'-10;
} while ((n /= b) > 0);
if (sign < 0)
s[i++] = '-';
s[i] = '';
reverse(s);
}


/* Reverses string s[] in place */

void reverse(char s[]) {
int c, i, j;
for ( i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}


Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 3-4.

K and R C, Solution to Exercise 3-4:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a C program to represent the binary numbers in a negative sign.There are a number of ways of representing signed integers in binary, for example, signed-magnitude, excess-M, one’s complement and two’s complement. In this program , we handle the problem that is, the value of n equal to -(2 to the power (wordsize – 1))(C Programming Language (2nd Edition) by Brian W.Kernighan & Dennis M.Richie page no 64) by changing the (n /= 10) > 0 to (n /= 10) != 0. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#define abs(x) ((x) < 0 ? -(x) : (x))
void itoa(int n, char s[]);
void reverse(char s[]);

int main(void) {
char buffer[20];

printf("INT_MIN: %dn", INT_MIN);
itoa(INT_MIN, buffer);
printf("Buffer : %sn", buffer);

return 0;
}
//itoa: convert to n characters in s - modified.
void itoa(int n, char s[])
{
void int i, sign;
void everse(char s[]);
sign = n;
i = 0;
do{
s[i++] = abs(n%10) + '0';
}while((n/=10)!=0);
if(sign < 0)
s[i++] = '-';
s[i] = '';
reverse(s);
}
void reverse(char s[]) {
int c, i, j;
for ( i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to generate Graph using grphics.h

C Program to Generate the graph sheet using the grphics.h library. To use graphics.h, we have to install the drivers in to the the system by using the initgraph() function. Here  we derive the graph of input sizes verses time taken for input sizes. x axis represents inputs(0,10000,20000,—-), y axis rep time(0,0.05,0.1,0.15—). Read more about C Programming Language.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include "stdio.h"
#include "conio.h"
#include "graphics.h"
void main() {
int gd = DETECT, gm;
int y = 0, x = 10, m[20], k[20], n, a[20], i;
float b[20];
initgraph(&gd, &gm, "c:\tc\bgi");
printf("nntGenerating the Graphsnn");
printf("nEnter the no. of inputst");
scanf("%d", &n);
printf("nEnter the input sizes and corresponding time takenn");
for (i = 0; i < n; i++) {
printf("nEnter input sizet");
scanf("%d", &a[i]);
printf("nEnter time takent");
scanf("%f", &b[i]);
}
cleardevice();
//represents y axis
line(10, 0, 10, 400);
//represents x axis
line(10, 400, 600, 400);
while (y <= 400) {
line(0, y, 10, y);
y = y + 20;
}
while (x <= 600) {
line(x, 400, x, 410);
x = x + 20;
}
outtextxy(20, 440, "1unit=20 pixels , origin is (10,400)");
outtextxy(
20,
450,
"x axis represents inputs(0,10000,20000,----), yaxis rep time(0,0.05,0.1,0.15---)");
setcolor(5);
for (i = 0; i < n; i++) {
k[i] = (a[i] * 0.002);
m[i] = (400 - (b[i] * 400));
putpixel(k[i], m[i], 11);
}
for (i = 0; i < n - 1; i++)
line(k[i], m[i], k[i + 1], m[i + 1]);
getch();
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 1-19

K and R C, Solution to Exercise 1-19:
Write a function reverse(str) that reverses the character string str. Use it to write program that reverse thes its input a line at a time. K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include "stdio.h"

#define MAXINLINE 1000 /* maximum input line size */

int get_lines(char line[], int maxline);
void reverse(char str[]);

/* print longest input line */
main() {

char line[MAXINLINE]; /* current input line */

while ((get_lines(line, MAXINLINE)) > 0) {

reverse(line);
printf("%s", line);

}

}

/* getline: read a line into s, return length */
int get_lines(char str[], int line) {
int c, i, j;

for (i = 0, j = 0; (c = getchar()) != EOF && c != 'n'; ++i) {
if (i < line - 1) {
str[j++] = c;
}
}
if (c == 'n') {
if (i <= line - 1) {
str[j++] = c;
}
++i;
}
str[j] = '';
return i;
}

/*reverse the string str*/
void reverse(char str[]) {
int i, j;
char temp;
i = 0;
while (str[i] != '')
++i;
--i;
if (str[i] != 'n')

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

Read more Similar C Programs
C Basic

K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com