Вы находитесь на странице: 1из 14

Program to add two numbers without using plus operator in c

Posted by Harris on September 22, 2010 in C · 0 Comment

I will explain three methods to add 2 number without using plus operator:

1. Using pointer
2. Using minus operator
3. Using bitwise operator

Method 1:

Here we use pointers to add two number

#include<stdio.h>
#include<conio.h>
int main()
{
int num1,num2,sum;
printf("\nEnter 1st number for addition:");
scanf("%d",&num1);
printf("\nEnter 2nd number for addition:");
scanf("%d",&num2);
char *p;
p=(char *)num1;
sum= (int)&p[num2]; //sum of num1 and num2
printf("\nSum of %d and %d is %d",num1,num2,sum);
getch();
return 0;
}

Explanation:

This one is very tricky and interesting, here we are adding two number through
pointer.
As we already know array
A[10] can also be written as *(A+10)

Similarly p[num2] can be written as *(p + num2)

Also & is address of operator and * is value at address operator


So if i is int like int i;
i = &*i
as address of operator and value at operator nullify each other
similarly
p[num2] = *(p + num2)
&p[num2] = &*(p + num2) =(p +num2)
As p is pointing to num1 so we can also write (p+num2) as (num1 + num2). Thus
the result would be num1+num2.

Method 2:

Here we use minus operator to add two number

#include<stdio.h>
#include<conio.h>
int main()
{
int num1,num2,sum;
printf("\nEnter 1st number for addition:");
scanf("%d",&num1);
printf("\nEnter 2nd number for addition:");
scanf("%d",&num2);
sum=num1-(-num2);
printf("\nSum of %d and %d is %d",num1,num2,sum);
getch();
return 0;
}

Explaination:

sum=num1-(-num2);
As we know minus * minus = plus. Thus the result would be num1+num2.

Method 3:

Here we use bitwise operator to add two number

//with bitwise operators:


#include<stdio.h>
#include<conio.h>
int add(int a, int b) {
int x,y;
do {
x = a & b;
y= a ^ b;
a = x << 1;
b = y;
} while (x);
return y;
}
int main()
{
int num1,num2,sum;
printf("\nEnter 1st number for addition:");
scanf("%d",&num1);
printf("\nEnter 2nd number for addition:");
scanf("%d",&num2);
sum=add(num1,num2);
printf("\nSum of %d and %d is %d",num1,num2,sum);
getch();
return 0;
}
Ads by Google

21) Write a program to find the smallest and largest element in a two dimensional
array.
22) Write a program to illustrate call by value.
23) Write a function to calculate the factorial of a given number.
24) Write a function to sort the elements of an array.
25) Write a recursive function to find the roots of the quadratic expression using
bisection
method.
26) Write a program to copy string to another without using string handling
functions.
27) Write a program to check whether a given string is a palindrome or not.
28) Write a function to find the largest element in an array.
29) Write a recursive function power (base, exponent) that when invoked returns
base
exponent.
30) Program to sort the characters in a given string.
31) Write a function to convert all the upper case letters to lower case and lower
case
letters to upper case in a given string.
32) Program to undefined an already defined macro.
33) Program to illustrate conditional compilation using #if, #end if, #else.
UNIT-III
34) Write a program to calculate length of the string using pointers.
35) Write a function to swap two variables using pointers.
36) Program to illustrate pointer arithmetic.
37) Write a function to calculate sum of two numbers using pointers to functions.
38) Write a program to perform matrix multiplication using pointers.
39) Write a program to find the largest in an array using pointers.
40) Program to arrange the given numbers in ascending order.
41) Write a C program using pointer for string comparison.
42) Program to reverse the string using pointers.
43) Program to perform sorting using command line arguments.
44) The names of employees of an organization are stored in three arrays, namely,
first
name, second name, and last name. Write a program using pointers to concatenate
the
three parts into one string to be called name.
45) Program to merge two sorted arrays so that the resultant array is in sorted
order using
pointers.
UNIT-IV
46) Write a C program to compute the monthly pay of 100 employees using each
employee’s name, basic-pay. The DA is computed as 52% of the basic pay. Gross-
salary (Basic-pay+DA).Print the employees name and gross salary.
47) Write a program to calculate and print student wise total for 50 students and 3
subjects. The structure should contain 3 subjects and total.
48) Write a program to calculate and print student wise total for 50 students and 3
subjects using pointers. The structure should contain 3 subjects.
49) Write a program to store the information of vehicles use bit fields to store the
status information. Assume the vehicle object consists of type, fuel and model
member fields. Assume appropriate number of bits for each field.
50) Program for illustration of user defined data types using typedef.
51) Program for illustration of nested structures.

52) Program to add or delete a record of a particular employee based on his code.
The structure should contain name, designation, code, salary. Program should also
provide the flexibility to update any employees record.
53) Define a structure that represent a complex number (contains two floating point
members, called real and imaginary). Write functions to add, subtract, multiply and
divide two complex numbers.
54) A company markets Hardware items. Create a structure “hwitem” that stores the
title of the item, it’s price, an array of three floats so that it can record the sale in
rupees of a particular item for the last three months, category of the item and it’s
original equipment manufacturer. Write a short program that provides facility to
read N number of items information, append new item, and displays all records.
55) Define a structure to represent a data. Use your structures that accept two different
dates in the format mmdd of the same year. And do the following: Write a C
program to display the month names of both dates.
56) Consider a structure master include the information like name, code, pay,
experience write a program to delete and display the information contained in
master variables for a given code.
57) Write a program to use structure within union. Display the contents of structure
elements.
UNIT-V
58) Write a C program to read a text file and to count Number of characters,
Number of
words and Number of sentences and write in an output file.
59) Write a C program to read the text file containing some paragraph. Use fseek()
and
read the text after skipping n characters from the beginning of the file.
60) Write a C program to read the text file containing some paragraph. Print the
first n
characters from the beginning of the file.
61) Write a program to print the output of the following format in an OUTPUT file.
Number
Square
Cube
2
4
8
3
9
27
62) Write a C program to read data from a file named INPUT containing numbers.
Write
all even numbers to file called EVEN and odd numbers to file called ODD.
63) Program to print the n characters from mth position in the file.
64) Program to print the current position value of the file pointer.
65) Program to check whether end of file has been reached or not using feof()
function.
66) Program to check whether any data errors are present and print the relevant
information using ferror() function.
67) Write a program to detect error while opening a file that does not exist.
68) Write a C program to open a pre-existing file and add information at the end of
file.
Display the contents of the file before and after appending
69) Program to copy one file to another.
70) Write program to read a text file and convert the file contents in capital (upper-
case)
and write the contents in an output file.
71) Candidates have to score 90 or above in the IQ test to be considered eligible for
taking further tests. All candidates who do not clear the IQ test are sent reject
letters

and others are sent call letters for further tests. The selected list and other
conversation have to be written to file.
UNIT-VI
72) Write a non-recursive simulation of towers of Hanoi.
73) Write recursive function for towers of Hanoi.
74) Program to convert Prefix to postfix using stacks.
75) Program to convert postfix expression to prefix expression.
76) Write a program to evaluate postfix expression
77) Program to evaluate prefix expression.
78) Write a program to implement dequeues
79) Program to implement priority queues.
80) Program to check whether a given string is palindrome or not using stacks.
UNIT-VII
81) Write a program in ‘C’ to form a list consisting the intersection of the elements
of two
lists
82) Write a routine to reverse elements of a doubly linked list by traversing the list
only
once
83) Write a program to count the number of non-leaf nodes in a binary tree
84) Write a program to count the number of leaf nodes in a binary tree
85) Write a program to implement circular using double linked list.
86) Write a program to perform polynomial addition.
87) Write a program to perform polynomial multiplication.
88) Function to reverse the single linked list.
89) Write non recursive functions for binary tree traversals.
90) Program to implement binary search tree using arrays.
91) Write a C program to exchange two nodes of a singly linked list.
92) Program to implement breadth first search.
93) Program to implement depth first search.
94) Represent a singly linked list using an array. Write routines to insert and delete
elements for this representation.
95) Write a routine SPLIT() to split a singly linked list into two lists so that all
elements
in odd position are in one list and those in even position are in another list.
96) Represent a doubly linked list using an array. Write routines to insert and delete
elements for this representation.
97) Write a function to merge to doubly linked lists and arrange the numbers in
ascending
order.
UNIT-VIII
98) Write a program to implement merge sort
99) Derive the efficiency of merge sort
100)
Derive the efficiency of binary search.

Write a program to reverse a string using stack?


In: Java Programming, Microprocessors [Edit
categories]

Download Google Chromewww.Google.com/Chrome


Searching is fast and easy with Google's web browser.
Ads

[Improve]

#include
#include
typedef struct stack
{
char b[100];
int top;
}stack;
void push(stack *s,char k)
{
if(s->top==99)
printf("\n Stack is full ");
else
s->b[++s->top]=k;
}
char pop(stack *s)
{
char c;
if(s->top==(-1))
printf("\n Stack is empty");
else
else
c=s->b[s->top--];
return c;
}
void main()
{
char name[100];
stack s;
clrscr();
s.top=-1;
printf("\nEnter name :");
gets(name);
int i=0;
while(name[i]!='\0')
{
push(&s,name[i]);
i++;
}
printf("\n Reverse string is :");
while(s.top!=-1)
{
printf("%c",pop(&s));
}
getch();
}

/* To reverse the given string using pointer By $ */

#include <stdio.h>
#include <string.h>

void main ()
{
char str[15];
char rev[15];
int i;
int count;
char* output_ptr;
gets(str);

count = strlen(str);

output_ptr = rev + strlen(str)-1;

for(i = 0; i < count; i++)


{
*(output_ptr - i) = *(str + i);
}

/* verify results */

printf("Original string: %s\n",str);


printf("Reversed string: %s\n",rev);

Write a C Program to find the length of the string


using pointer?
In: PHP Programming, C Programming [Edit
categories]

5930+ of C ProgrammingTimesJobs.com/C_Programming
Exp: 2 - 15 Yrs.Sal : 4 L - 17 L PA Upload Resume & Get a Pay Hike Now!
Ads

[Improve]

int strLen(char* str) {

// point current at the beginning of str


char *current = str;

// loop until we get to the null character


while( current[0] != '\0' ) {

++current;

return (current - str);

/* To reverse the given string using pointer By $ */

#include <stdio.h>
#include <string.h>

void main ()
{
char str[15];
char rev[15];
int i;
int count;
char* output_ptr;
gets(str);

count = strlen(str);

output_ptr = rev + strlen(str)-1;

for(i = 0; i < count; i++)


{
*(output_ptr - i) = *(str + i);
}

/* verify results */

printf("Original string: %s\n",str);


printf("Reversed string: %s\n",rev);

//reverse a string using pointers

# include<stdio.h>
# include<string.h>
void main()
{
char *first,*last;
char temp;
int len;
char str[20];

printf("enter ur string to be reverse: ");


gets(str);

len=strlen(str);

first=str; //point at first character


last=&str[len-1]; //point at last character

for(int i=0;i<=len/2;i++)
{
temp=*first;
*first=*last;
*last=temp;
first++;
last--;

puts(str);

Write a program to compare two


strings?
In: Computer Programming [Edit
categories]

Download Google Chromewww.Google.com/Chrome


Searching is fast and easy with Google's web browser.
Ads

[Improve]

#include <stdio.h>

#include <string.h>

int main()
{

char *str1 = "sample", *str2 = "sample";

if(strcmp(str1,str2)==0)

printf("strings are equal");

retrun 0;

Read more:
http://wiki.answers.com/Q/Write_a_program_to_compare_two_strings#ixzz0Aqr11hg
c

/*To compare two strings using pointer.*/

#include
#include

main()
{
char a[10],b[10];
char *p,*q;
int i,j,status=0;
p=a;
q=b;
clrscr();
printf("\nInput two strings\n");
gets(a);
gets(b);
for(;*p!='\0' || *q!='\0';*p++,*q++)
if(*p!=*q)
{
printf("\nStrings are different");
status=1;
break;
}
if(status==0)
printf("\nStrings are same");
}
Convert a string to uppercase using pointer.

#include
#include

main()
{
char a[20];
char *p;
int i;
p=a;
clrscr();
printf("\nInput a string ");
gets(a);
puts("\nEntered string is ");
puts(a);
printf("\nEntered string in UPPERCASE is");
for(;*p!='\0';*p++)
{
if(*p>='a' && *p<='z')
*p-=32;
}
puts(a);
}

Program to combine two strings


using pointer & add them.
#include
#include

main()
{
char a[10],b[10],c[20];
char *p,*q;
int i,j;
p=a;
q=b;
clrscr();
printf("\nInput two strings ");
gets(a);
gets(b);
for(i=0;*p!='\0';c[i]=*p++,i++);
for(j=0;*q!='\0';c[i]=*q++,j++,i++);
c[i]='\0';
puts("Entered strings are ");
puts(a);
puts(b);
printf("\nAfter combining ");
puts(c);
}

/*Program to Copy one string to another using pointer.*/

#include
#include

main()
{
char a[80],b[80],*pa,*pb;
int i=0;
clrscr();
printf("Given first string ");
scanf("%s",a);
pa=&a[0];
pb=&b[0];
while(*pa!='\0')
{
*pb=*pa;
pa++;
pb++;
}
*pb='\0';
printf("\nCopied string is");
puts(b);
}

Вам также может понравиться