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

Swap Two Numbers (Without Using A Temporary Variable)

The document contains C code snippets for several algorithms and programs: 1) A program that swaps two numbers without using a temporary variable by using XOR operations. 2) A program that prints patterns of letters in a pyramid shape using nested for loops. 3) A recursive function to calculate the nth Fibonacci number. 4) A program that checks if a number is an Armstrong number.

Uploaded by

Gov g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Swap Two Numbers (Without Using A Temporary Variable)

The document contains C code snippets for several algorithms and programs: 1) A program that swaps two numbers without using a temporary variable by using XOR operations. 2) A program that prints patterns of letters in a pyramid shape using nested for loops. 3) A recursive function to calculate the nth Fibonacci number. 4) A program that checks if a number is an Armstrong number.

Uploaded by

Gov g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Swap two numbers (without using a temporary variable )

//Sorting without using temporary variables


#include
#include
int main(){
int a,b;
printf("Before swapping:\n");
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
//Sorting using
a=a^b; //uses xor operator
b=a^b; //to swap the values
a=a^b; //of a and b
printf("After swapping:\n");
printf("Value of a: %d \nValue of b: %d",a,b);
getch();
}

Patterns (one of their favorites)


A B C D E D C B A
A B C D C B A
A B C B A
A B A
A

//Printing pattern
#include
#include
int main(){
int n,i,j;
printf("Enter no of lines: ");
scanf("%d",&n);
for(i=0;i<n;i++){
for(j=0;j<i;j++){ //for printing spaces
printf(" ");
}
for(j=0;j<n-i;j++){ //for printing the left side
printf("%c ",'A'+j); //the value of j is added to 'A'(ascii value=65)
}
for(j=n-i-2;j>=0;j--){ //for printing the right side
printf("%c ",'A'+j);
}
printf("\n");
}
getch();
}

Finding the nth Fibonacci number using recursion


#include
#include
int y;
fibonacci(int x){
if(x==1 || x==0) //terminating step
return x;
y=fibonacci(x-1)+fibonacci(x-2); //recursive definition
return y;
}
int main(){
int a,r;
printf("Enter the position : ");
scanf("%d",&a);
r=fibonacci(a);
printf("The number at position %d is %d",a,r);
getch();
return 0;
}

Armstrong number

5^1=5 (so its an Armstrong number)

1^4+6^4+3^4+4^4=1634 (so its another Armstrong number)

#include
#include
void checkArmstrong(int temp){
int sum=0,remainder,num=temp,noOfDigits=0;
while(temp != 0){ //counts no of digits
noOfDigits++;
temp=temp/10;
}
temp=num;
while(temp != 0){ //calculates the sum of the digits
remainder = temp%10; //to the power noOfDigits
sum = sum + pow(remainder,noOfDigits);
temp = temp/10;
}
if (num == sum) //checks if the number is an armstrong no. or not
printf("%d is an armstrong number.",num);
else
printf("%d is not an armstrong number.",num);
}

int main(){
int n;
printf("Enter a number: ");
scanf("%d",&n);
checkArmstrong(n);
getch();
return 0;
}

Concatenate two strings without using strcat()

#include
#include
#include
void concatenate(char a[],char b[]){
char c[strlen(a)+strlen(b)]; //size of c is sum of a and b
int i=0,j=0;
while(i<strlen(a)) //adds the first string to c
c[i++]=a[i];
while(j<strlen(b)) //adds the second string to c
c[i++]=b[j++];
c[i]='\0'; //finally add the null character
printf("After concatenation:\n");
printf("Value = %s",c);
}
int main(){
char a[30], b[30];
printf("Enter the first string: ");
gets(a);
printf("Enter the second string: ");
gets(b);
concatenate(a,b);
getch();
return 0;
}

Enter and print details of n employees using structures and dynamic memory allocation
#include
#include
typedef struct{ //structure of emp
char name[30];
int age;
float salary;
}emp;
int main(){
int n,i;
emp *employee;
printf("Enter no of employees: ");
scanf("%d",&n);
employee=(emp*)malloc(n*sizeof(emp)); //dynamic memory allocation using
malloc()
for(i=0;i<n;i++){
printf("\n\nEnter details of employee %d\n",i+1);
printf("Enter name: ");
scanf("%s",employee[i].name);
printf("Enter age: ");
scanf("%d",&employee[i].age);
printf("Enter salary: ");
scanf("%f",&employee[i].salary);
}
printf("\nPrinting details of all the employees:\n");
for(i=0;i<n;i++){
printf("\n\nDetails of employee %d\n",i+1);
printf("\nName: %s",employee[i].name);
printf("\nAge: %d",employee[i].age);
printf("\nSalary: %.2f",employee[i].salary);
}
getch();
return 0;
}

Tower of Hanoi using recursion

Recursive solution:

1. move n−1 discs from A to B. This leaves disc n alone on peg A


2. move disc n from A to C
3. move n−1 discs from B to C so they sit on disc n

#include
#include
void towers(int n,char frompeg,char topeg,char auxpeg){
// If only 1 disk, make the move and return
if(n==1){
printf("\nMove disk 1 from peg %c to peg %c",frompeg,topeg);
return;
}
// Move top n-1 disks from A to B, using C as auxiliary
towers(n-1,frompeg,auxpeg,topeg);
// Move remaining disks from A to C
printf("\nMove disk %d from peg %c to peg %c",n,frompeg,topeg);
// Move n-1 disks from B to C using A as auxiliary
towers(n-1,auxpeg,topeg,frompeg);
}
int main(){
int n;
printf("Enter the number of disks : ");
scanf("%d",&n);
printf("The Tower of Hanoi involves the moves :\n\n");
towers(n,'A','C','B');
getch();
return 0;
}
Add numbers using Command line arguments

#include
#include
#include

int main(int argc,char *argv[])


{
int sum=0,i;
//Compare if proper number of arguments have been entered
if(argc<3)
{
printf("Insufficient number of arguments...\n");
getch();
return 0;
}

//Add all the numbers entered using atoi function


for(i=1;i<argc;i++)
{
sum+=atoi(argv[i]);
}

//print the sum


printf("Ans=%d",sum);
getch();
}

Print its own source code using File

#include
#include
int main(int argc,char *argv[])
{
FILE *fp;
argv[0]=strcat(argv[0],".c"); //to add .c to the name of the file
fp=fopen(argv[0],"r"); //opens the file in read mode
char ch;
while(!feof(fp)) //till it reaches the end of file
{
fscanf(fp,"%c",&ch);
printf("%c",ch);
}//end while
getch();
return 0;
}//end main

You might also like