0% found this document useful (0 votes)
143 views

1.C Program Without Using Semicolon (At Least Do Addition of Two Numbers)

The document contains 5 sections that describe C programs: 1. A program to perform addition of two numbers without using the semicolon. 2. A program to perform addition without using arithmetic operators by using logical operators instead. 3. Two methods for a program to reverse the words in a string - a non-recursive method for a single line input, and a recursive method to handle multiple line input until a dot is entered. 4. A program to swap two variable values without using arithmetic operators, temporary variables, or pointers by using logical XOR operations. 5. A program to generate all prime numbers between two given numbers for a given number of test cases by checking if a number is only divisible by 1

Uploaded by

prabhusct
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views

1.C Program Without Using Semicolon (At Least Do Addition of Two Numbers)

The document contains 5 sections that describe C programs: 1. A program to perform addition of two numbers without using the semicolon. 2. A program to perform addition without using arithmetic operators by using logical operators instead. 3. Two methods for a program to reverse the words in a string - a non-recursive method for a single line input, and a recursive method to handle multiple line input until a dot is entered. 4. A program to swap two variable values without using arithmetic operators, temporary variables, or pointers by using logical XOR operations. 5. A program to generate all prime numbers between two given numbers for a given number of test cases by checking if a number is only divisible by 1

Uploaded by

prabhusct
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

C program without using semicolon (at least do addition of two numbers)

# include<stdio.h>
# include<conio.h>

void main(int a ,int b)


{

if(clrscr(),printf("\nEnter two integers:"),scanf("%d %d",&a,&b))


while(printf("\nSuccessful addition:%d",a+b),getch(),0)
{
//condition for while loop fail this block will not be executed
}
}

2. C program to perform Addition of two numbers without using arithmetic operators.

#include <stdio.h>

int add(int a, int b)


{
if (!a)
return b; // return sum along with carry
else
return add((a & b) << 1, a ^ b);

/* Left shift the result of logical & gives input a and xor operation of a ^ b gives input b */

int main()
{
unsigned int a,b;
printf("Enter the two numbers: \n");
scanf("%d",&a);
scanf("%d",&b);
printf("Sum is: %d",add(a,b));
getch();
}
3. C program to reversing words ..
INPUT: hi how r u.
OUTPUT: u. r how hi

Method 1: (Non recursive) // can get only one line input string words

void reverse(char *,int b);


void main()
{
char a[26];
int len;
clrscr();
printf("enter string ");
gets(a);
len=strlen(a);
reverse(a,len);
getch();
}

/* Reading string from last postion to beginning of last string before whitespace and then store
it.Repeat it until whitespace not found. Use another loop to print first string word at last. */

void reverse(char * a,int len)


{
int i,j,n;
if(len==0)
printf("%c",a[len]);
else
{
j=len;
for(i=len-1;i>=0;i--)
{
if(a[i]==' ')
{
for(n=i+1;n<j;n++)
printf("%c",a[n]); printf(" ");
j=i;
}
}
for(n=i+1;n<j;n++)
printf("%c",a[n]);
}
}
Method 2: (Recursive) ) // can get multiple line input string words until we press ‘.’ Dot character.

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

void print()

{
char str[100],c,*p;
int i;

i=0;
scanf("%s",str);
i=strlen(str);
p=str;
i=strlen(str);
i--;
p=p+i;
c=*p; //pointing to last character of a string in a word

if(c!='.') //To get word upto dot found and then end reading input.
print();
// principle of stack last in first out so print string word in reverse order.
printf(" %s",str);

}
void main()
{
char str,temp;
int i,len;
clrscr();
printf("Enter String : ");
print();
getch();
}
4.Write a program to swap two values of variable say a,b without using any arithmetic
operators,temporary variables or pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
int x=1;
int y=5;
printf(“before swapping”);
printf(“x=%d,y=%d”,x,y);
x=x^y; // logical xor operation to compute x=x+y
y=x^y; // logical xor operation to compute y=x-y
x=x^y; // logical xor operation to compute x=x+y
printf(“after swapping”);
printf(“x=%d,y=%d”,x,y);
getch();
}

5. Write a C/C++,java program :


Your task is to generate all prime numbers between two given numbers for given number of
test cases.
Input:
The first line contains t, the number of test cases (less then or equal to 10).
Separate the answers for each test case by an empty line.

Example :
Input:
2 //input a integer for no.of test cases/limits.
1 10 // input two integers for limit1
3 5 // input two integers for limit2

Output:
// printing prime numbers for limit1
2
3
5
7
// printing prime numbers for limit2
3
5
#include <stdio.h>
#include<conio.h>
#include<math.h>

void prime(int i,int n)


{ int c,j;
while(i<=n)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
// print prime number if satisfies two condition i.e divisible by itself and by 1.
if(c==2)
printf("\n%d ",i);

i++;
}
printf("\n");

void main()
{
int n[22],m[21],t,i,j;
clrscr();
printf("Enter no.of test case:");
scanf("%d",&t);
for(i=0;i<t;i++)
{
printf("Enter Limits m and n:");
scanf(" %d %d",&m[i],&n[i]);
}
printf("\nPrime Numbers Are Follwing");
for(i=0;i<t;i++)
prime(m[i],n[i]); // passing ranges m and n for t no.of test cases
getch();

You might also like