Friday, August 7, 2015

swapping string

#include <stdio.h>

#include<string.h>

int main()
{
    int  i ;
    char  temp ;

    char  profile[2][10] = {"koushik","Akif"} ;

    printf ( "\nBefore swapping: %s %s", &profile[0][0], &profile[1][0] ) ;

    for ( i=0 ; i <=10 ; i++ )
    {
        temp = profile[0][i] ;
        profile[0][i] = profile[1][i] ;
        profile[1][i] = temp ;
    }
    printf ( "\nAfter swapping: %s %s", &profile[0][0], &profile[1][0] ) ;


}




output:                                                                  


Wednesday, July 29, 2015

Palindrome without using string function

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

int main()
{
   char str1[20];
   int i, j, count=0, prove=0;
   printf("\nEnter any string: ");
   gets(str1);
   for (i=0; str1[i]!='\0'; i++)
   {
        count++;
   }
   
  for(i=0,j=count-1;i<count-1;i++,j--) 
 {
        if (str1[i] != str1[j])
        {
              prove = 1;
              break;
        }
       
   }
   if (prove == 0)
        printf("\nString is palindrome");
   else
        printf("\nString is not palindrome");
   

return 0;

}


output:
                                                                  

Sunday, July 26, 2015

String_upper to lower and lower to upper case

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

int main ()
{
   int c = 0;
   char ch, s[1000];

   printf("Input a string\n");
   gets(s);

   for(c=0;s[c] != '\0';c++) {
      ch = s[c];
      if (ch >= 'A' && ch <= 'Z')
         s[c] = s[c] + 32;
      else if (ch >= 'a' && ch <= 'z')
         s[c] = s[c] - 32;

   }

   printf("%s\n", s);

   return 0;
}

string_palindrome

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

int main()
{
   char a[100],b[100];
   while(1){
   printf("Enter the string to check if it is a palindrome:");
   scanf("%s",a);

   strcpy(b,a);
   strrev(b);

   if (strcmp(a,b) == 0)
   {
      printf("Entered string is a palindrome.\n");
   }
   else
   {
      printf("Entered string is not a palindrome.\n");
      break;
   }
   }

   return 0;
}

String_concat

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

int main()
{
    char s1[100], s2[100], i, j;

    printf("Enter first string: ");
    scanf("%s",s1);

    printf("Enter second string: ");
    scanf("%s",s2);

    for(i=0; s1[i]!='\0'; i++);
    for(j=0; s1[i]+s2[j]!='\0';i++,j++)
    {
        s1[i]=s2[j];
    }
    s1[i]='\0';
    printf("After concatenation: %s",s1);
    return 0;
}

String_copy

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

int main()
{
    char str1[100], str2[100], i;
    printf("\n");
    printf("input string : ");
    scanf("%s",str1);
    for(i=0; str1[i]!='\0';i++)
    {
        str2[i]=str1[i];
    }
    str2[i]='\0';

    printf("\noutput String: %s\n",str2);
    return 0;
}

String_length

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

int main()
{
    char str[100],i,count=0;

    printf("Enter a string: ");

    scanf("%s",str);

    for(i=0; str[i]!='\0'; i++){

        count++;
    }

    printf("Length of string: %d",count);

    return 0;
}

String_reverse a sentence

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

///reverse a sentence

int  main()
{
    int i,length,count=0;
    char ch[20];

    printf("Enter the string:");
    gets(ch);
    length=strlen(ch);
    for(i=0; i<length; i++)
    {
        count++;
    }
    printf("Reverse is:");
    for(i=count-1; i>=0; i--)
    {
        printf("%c",ch[i]);
    }
    return 0;
}

String_reverse_word

#include<stdio.h>
#include<string.h>
/***
///reverse a word
int  main()
{
    int i,count=0;
    char ch[20];

    printf("Enter the string:");
    gets(ch);
    for(i=0; ch[i]!='\0'; i++)
    {
        count++;
    }
    printf("Reverse is:");
    for(i=count-1; i>=0; i--)
    {
        printf("%c",ch[i]);
    }
    return 0;
}

Saturday, July 25, 2015

Recursion_find largest number from an array

#include<stdio.h>

int Max(int arr[],int size);


int main(){

    int arr[100],max,i,size;

    printf("Enter the size of the array: ");
    scanf("%d",&size);

    printf("Enter %d elements of an array: ", size);
    for(i=0;i<size;i++)
      scanf("%d",&arr[i]);

    max=Max(arr,size);

    printf("Largest element of an array is: %d",max);

    return 0;
}

int Max(int arr[],int size){

    static int i=0,max =-99;

    if( i<size){
         if(max<arr[i])
           max=arr[i];
       i++;
      Max(arr,size);
    }

    return max;
}

Friday, July 24, 2015

Recursion_prime number

#include<stdio.h>

int isPrime(int num,int i);

int main(){

    int num,prime;

    printf("Enter a positive number: ");
    scanf("%d",&num);

    prime = isPrime(num,num/2);

   if(prime==1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);

   return 0;
}

int isPrime(int num,int i){

    if(i==1){
        return 1;
    }else{
       if(num%i==0)
         return 0;
       else
         isPrime(num,i-1);
    }
}

Recursion_Multiplication of two number


#include<stdio.h>

int multiply(int a,int b);

int main(){

    int a,b,result;
    printf("Enter two integer: ");
    scanf("%d%d",&a,&b);

    result = multiply(a,b);

    printf("Multiplication of two integer is:%d",result);

    return 0;
}

int multiply(int a,int b){

    static int result=0,i=0;

    if(i < a){
         result= result + b;
         i++;
         multiply(a,b);
    }

    return result;
}

Recursion_GCD

#include <stdio.h>

int gcd(int a,int b);

int main()
{
    int a, b, result;

    printf("Enter the two numbers to find their GCD: ");
    scanf("%d%d", &a, &b);
    result = gcd(a, b);
    printf("The GCD of %d and %d is:%d\n", a, b, result);
}

int gcd(int a, int b)
{
    while (a != b)
    {
        if (a > b)
        {
            return gcd(a - b, b);
        }
        else
        {
            return gcd(a, b - a);
        }
    }

    return b;   ///return a
}

Thursday, July 23, 2015

Recursion_natural number

#include<stdio.h>

int sum(int n);

int main()
{
    int n,concat;
    printf("Enter an positive integer: ");
    scanf("%d",&n);
    concat=sum(n);
    printf("Sum = %d",concat);
    return 0;
}
int sum(int n)
{
    if(n!=0)
     return n+sum(n-1);  
}

Recursion_LCM

#include<stdio.h>

int Lcm(int a,int b);

int main(){

    int a,b,Lcm;
    printf("Please Enter Two Number: ");
    scanf("%d%d",&a,&b);

    if(a>b)
    Lcm = lcm(a,b);
    else
        Lcm = lcm(b,a);

    printf("LCM of %d and %d is:%d",a,b,Lcm);

    return 0;
}

int lcm(int a,int b){

   static int temp = 1;

    if(temp % a == 0 && temp % b == 0)
    {
    return temp;
    }
    temp++;
    lcm(a,b);

}

Recursion_factorial


#include<stdio.h>
int factorial(int n);
int main()
{
    int n;
    printf("Enter an positive integer: ");
    scanf("%d",&n);
    printf("%d! = %d", n, factorial(n));
    return 0;
}
int factorial(int n)
{
if(n==1 || n==0){

return 1;
}
    else if (n!=1)
     return n*factorial(n-1);
}

Tuesday, July 21, 2015

Calculator by structure

#include <stdio.h>
#include <stdlib.h>

struct calculator{

int add1,add2,sub1,sub2,div1,div2,mul1,mul2;


};

void display(struct calculator pos);


int main()
{
 struct calculator cal;

 printf("\nplease enter tow number for addition:");
 scanf("%d %d",&cal.add1,&cal.add2);

  printf("\nplease enter tow number for subtraction:");
 scanf("%d %d",&cal.sub1,&cal.sub2);

  printf("\nplease enter tow number for division:");
 scanf("%d %d",&cal.div1,&cal.div2);

  printf("\nplease enter tow number for multiplication:");
 scanf("%d %d",&cal.mul1,&cal.mul2);

 display(cal);


 return 0;

}

void display(struct calculator pos){


printf("\n-------------------------------------\n");
printf("THIS IS A SIMPLE CALCULATOR");
printf("\n-------------------------------------\n");

printf("ADDITION= %d\n",pos.add1+pos.add2);

printf("\nSUBTRACTION= %d\n",pos.sub1-pos.sub2);

printf("\nDIVISION= %d\n",pos.div1/pos.div2);

printf("\nMULTIPLICATION= %d\n",pos.mul1*pos.mul2);

printf("\n-------------------------------------\n");
printf("SOLVED");
printf("\n-------------------------------------\n");



}

array passing in function

#include <stdio.h>
pass_arr( int arr[])
{
    int i;
    for( i=0; i<7; i++)
    {
        printf("arr[%d] is: %d \n", i, arr[i]);
       
      
    }
}

int main()
{
     int arr[] = {11, 22, 33, 44, 55, 66, 77};
     pass_arr(arr);
     return 0;
}



output:


arr[0] is: 11
arr[1] is: 22
arr[2] is: 33
arr[3] is: 44
arr[4] is: 55
arr[5] is: 66

arr[6] is: 77