Friday, 21 April 2017

Program to check whether string is palindrome or not without using library function

Program to check whether string is palindrome or not without using library function


#include<stdio.h>

 void main()
{
            char s[20];
            int i=0, len, flag = 0;
            printf("\nEnter s1: ");
            gets(s);

            /*To find the length of the string*/
            while(s[i]!='\0')
            {
                        i++;
            }

            len = i;

            for(i=0; i<len/2; i++)
            {
                        if(s[i] != s[len-i-1])
                        {
                                    flag = 1;
                                    break;
                        }
            }

            if(flag == 0)
                        printf("\nString is palindrome.");
            else
                        printf("\nString is not palindrome.");
}


Output:
Enter s1: abcba
Strings is palindrome.

Program to check whether string is palindrome or not

Program to check whether string is palindrome or not


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

void main()
{
          char s1[20], s2[20];
          printf("\nEnter s1: ");
          gets(s1);

          /*Copy s1 contents to s2*/
          strcpy(s2, s1);

          /*Reverse s2*/
          strrev(s2);


          /*Compare s1 and s2*/
          if(strcmp(s1, s2) == 0)
                  printf("\nString is palindrome.");
          else
                 printf("\nString is not a palindrome");


}

Output:
case 1:
Enter s1: abcde
String is not a palindrome


case 2:
Enter s1: abcba
String is palindrome.

Program to compare two strings without using library function strcmp

Program to compare two strings without using library function strcmp

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

void main()
{
       int len1, len2, flag = 0, i;
      char s1[20], s2[20];
      printf("\nEnter s1: ");
      gets(s1);
      printf("\nEnter s2: ");
      gets(s2);

      len1 = strlen(s1);
      len2 = strlen(s2);

      if(len1 != len2)
      {
             flag = 1;
      }
     else
     {
            for(i=0;i<len1;i++)
           { 
                 if(s1[i] != s2[i])
                {
                       flag = 1;
                       break;
                 }
          }
    }

    if(flag == 0)
        printf("\nStrings are same.");
    else
        printf("\nStrings are different.");
}

Output:
Case 1:

Enter s1: abcde
Enter s2: abcdef
Strings are different.


Case 2:
Enter s1: abcde
Enter s2: abfgh
Strings are different.

Case 3:
Enter s1: abcde
Enter s2: abcde
Strings are same.


Sunday, 16 April 2017

Program for arithmetic calculator using switch

Program to demo arithmetic calculator using switch


#include<stdio.h>
void main()
{
             int a, b;
             float res;
             char op;
             printf("\nEnter a op b :");
             scanf("%d %c %d", &a, &op, &b);
             switch(op)
            {
                         case  '+' :       res = a+b;
                                              break;
                         case  '-' :        res = a-b;
                                              break;
                         case  '*' :        res = a * b;
                                              break;
                          case  '/' :        if(!b)
                                                      printf("\n Divide by zero error");
                                               else
                                                      res = (float)a/b;
                                               break;
                          case  '%' :      res = a % b;
                                               break;
                          default:        printf("\nGive valid operator+");
                                              break;
            }
            printf("\n Result = %f", res);
}

Output:

Enter a op b : 1   /    2
Result = 0.500000

Write a program to check whether character is vowel or consonant

Program to check whether character is vowel or consonant


#include<stdio.h>
void main()
{
             char ch;
             printf("\nEnter the character:");
             scanf("%c", &ch);
             switch(ch)
            {
                      case 'a':
                      case 'e':
                      case 'i':
                      case 'o':
                      case 'u':    printf("\nGiven character is vowel");
                                       break;
                      default:     printf("\nGiven character is consonant");
                                       break;
           }
}

Output:
Enter the character: o
Given character is vowel

Enter the character: t
Given character is consonant

Program to print pattern using for loop

Program to print pattern using for loop

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5



#include<stdio.h>
void main()
{
                 int i,j,row;
                printf("Enter the number of rows you want:");
                scanf("%d",&row);
                 for(i=1;i<=row;i++)
                {
                                for(j=1;j<=i;j++)
                                {
                                                printf("%d ",i);
                                }
                                printf("\n");
                }  
}

Output:
Enter the number of rows you want: 5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Program to print pattern using for loop

Program to print pattern using for loop

  1
  2   3
  4   5   6
  7   8   9  10
 11  12  13  14  15


#include<stdio.h>
main()
{
                int row,i,j,c=1;
                printf("Enter the number of rows you want: \n");
                scanf("%d",&row);
                for(i=1;i<=row;i++)
                {
                                for(j=1;j<=i;j++)
                                {
                                                printf("%3d ",c);
                                                 c++;
                                }
                                printf("\n");
                }           
}

Enter the number of rows you want:  
  1
  2   3
  4   5   6
  7   8   9  10
 11  12  13  14  15

Program to print pattern using for loop

Program to print pattern using for loop

* * * * *
* * * *
* * *
* *
*


#include<stdio.h>
void main()
{
                int row,i,j;
                printf("Enter the number of rows you want: \n");
                scanf("%d",&row);
                for(i=row;i>=1;i--)
                {
                                for(j=1;j<=i;j++)
                                 {
                                                printf("* ");
                                }
                                printf("\n");
                }
}

Output:
Enter the number of rows you want: 5
* * * * *
* * * *
* * *
* *
*

Program to print pattern using for loop

Program to print pattern using for loop

*
* *
* * *
* * * *

#include<stdio.h>
void main()
{
                int row,i,j;
                printf("Enter the number of rows you want: ");
                scanf("%d",&row);
               
                for(i=1;i<=row;i++)
                {
                                for(j=1;j<=i;j++)
                                {
                                                printf("* ");
                                }
                                printf("\n");
                }             
}



Enter the number of rows you want: 5

*
* *
* * *
* * * *
* * * * *