Swap Two Numbers (Without Using A Temporary Variable)
Swap Two Numbers (Without Using A Temporary Variable)
//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();
}
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;
}
#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;
}
Recursive solution:
#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
#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