Reverse A String

Program to reverse a string into another using a user-defined function (i.e. without using string handler function)



#include<stdio.h>
#include<conio.h>
main()
{
  char s1[25],s2[25];
  int x,count=0;
  char reverse(char[],int,char[]);
  clrscr();
  printf("Enter your string: ");
  gets(s1);
  for(x=0;s1[x]!='\0';x++)
    count++;
  reverse(s1,count,s2);
  printf("\nReversing your string...\n");
  puts(s2);
  return 0;
}
char reverse(char a[25], int len, char b[25])
{
  int i=len-1,j=0;
  while(i>=0)
    b[j++]=a[i--];
  b[j]='\0';
  return *b;
}

Concatenation

Program to concatenate a string into another using a user-defined function(i.e. without using string handler function)


#include<stdio.h>
#include<conio.h>
main()
{
  char s1[25],s2[25],s3[50];
  char cate(char[],char[],char[]);
  clrscr();
  s1[25]=0,s2[25]=0,s3[50]=0;
  printf("Enter string1: ");
  gets(s1);
  printf("\nEnter string2: ");
  gets(s2);
  printf("\nUsing concatenation function...");
  cate(s1,s2,s3);
  printf("\nResultant string: ");
  puts(s3);
  return 0;
}
char cate(char a[25], char b[25], char c[50])
{
  int i=0,j=0;
    while(a[i]!='\0')
      c[j++]=a[i++];
    i=0;
    while(b[i]!='\0')
      c[j++]=b[i++];
    c[j]='\0';
  return *c;
}

Copy A String

Program to copy a string into another using a user-defined function (i.e. without using string handler function)


#include<stdio.h>
#include<conio.h>
main()
{
  char s1[25],s2[25];
  char copy(char[],char[]);
  clrscr();
  s1[25]=0,s2[25]=0;
  printf("Enter string1: ");
  gets(s1);
  printf("\nUsing copy function...");
  copy(s1,s2);
  printf("\nString2: ");
  puts(s2);
  return 0;
}
char copy(char a[25], char b[25])
{
  int i,j;
  for(i=0,j=0;a[i]!='\0';i++,j++)
    b[j]=a[i];
  b[j]='\0';
  return *b;
}

Print A Design-V

Print the following design:
1
01
101
0101


#include<stdio.h>
#include<conio.h>
main()
{
  int i,j;
  clrscr();
  for(i=1;i<=4;i++)
{
  for(j=1;j<=i;j++)
{
  if((i+j)%2==0)
  printf("1");
  else
  printf("0");
  }
  printf("\n");
  }
  return 0;
}

Print A Design-IV

Print the following design:

   *
  * *
 * * *
* * * *



#include<stdio.h>
#include<conio.h>
main()
{
  int k,x;
  clrscr();
  for(i=1;i<5;i++)
  {
  for(x=5-i;x>1;x--)
     printf(" ");
  for(k=1;k<=i;k++)
     printf("* ");
  printf("\n");
  }
  return 0;
}

Print A Design-III

Print the following design:

   1
  212
 32123
4321234



#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,x;
clrscr();
for(i=1;i<5;i++)
{
for(x=5-i;x>1;x--)
  printf(" ");
for(j=i;j>=1;j--)
  printf("%d",j);
for(k=2;k<=i;k++)
  printf("%d",k);
printf("\n");
}
return 0;
}

Print A Design-II

Print the following design:

A
AB
ABC
ABCD



#include<stdio.h>
#include<conio.h>
main()
{
  int i,j;
  char ch;
  clrscr();
  for(i=1;i<5;i++)
  {
   for(j=1,ch='A';j<=i;j++,ch++)
    printf("%c",ch);
   printf("\n");
  }
  return 0;
}

Print A Design-I

Print the following design:

$
$$
$$$
$$$$



#include<stdio.h>
#include<conio.h>
void main()
{
  int i,j;
  clrscr();
  for(i=1;i<5;i++)
  {
   for(j=1;j<=i;j++)
    printf("$");
  printf("\n");
  }
  return 0;
}

Addition Operation

C program to add two numbers and display the result.


#include<stdio.h>
#include<conio.h>
main()
{
int a,b,sum;
clrscr();
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
sum=a+b;
printf("The sum is %d",sum);
return 0;
}

Addition Of The Digits Of A Number

C program to add all the digits of a number and print the sum.


#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,d,x;
clrscr();
printf("Enter a five digit number: ");
scanf("%d",&x);
a=x%10; x/=10;
b=x%10;         x/=10;
c=x%10; x/=10;
d=x%10; x/=10;
printf("\nSum of all the digits of the number is %d",a+b+c+d+x);
return 0;
}

Swapping Numbers

C program to swap two numbers.


Method 1:

Using only two variables

#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
printf("Enter two numbers a and b: ");
scanf("%d %d",&a,&b);
a+=b;
b=a-b;
a-=b;
printf("\nAfter swapping, a: %d and b: %d",a,b);
return 0;
}


Method 2:

Using three variables

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("Enter two numbers a and b: ");
scanf("%d %d",&a,&b);
temp=a;
a=b;
b=temp;
printf("\nAfter swapping, a: %d and b: %d",a,b);
getch();
}


Method 3:

Using bitwise operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter two numbers a and b: ");
scanf("%d %d",&a,&b);
a=a^b;
b=a^b;
a=a^b;
printf("\nAfter swapping, a: %d and b: %d",a,b);
getch();
}

Temperature Coversion

C program to convert temperature in degree Celsius to degree Fahrenheit and vice-versa.


#include<stdio.h>
#include<conio.h>
main()
{
float c,f;
printf("Enter temperature in Celsius: ");
scanf("%f",&c);
f=((9*c)/5)+32;
printf("\nCorresponding temperature in Fahrenheit: %f",f);
printf("\nEnter temperature in Fahrenheit: ");
scanf("%f",&f);
c=((f-32)*5)/9;
printf("\nCorresponding temperature in Celsius: %f",c);
return 0;
}

Print A Message

C program to print a message.


#include<stdio.h>       // for printf()
#include<conio.h>      // for clrscr()
main()
{
    clrscr();
    printf("Hello People!");
}
/******************
To view the result,
press Alt+F5
******************/