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

Friday, July 17, 2015

My biodata:

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


struct my_info
{

    char name[10];
    int age;
    char home_town[10];
    char school[10];
    char college[10];
    char versity[10];
    char life_style[10];


};

void biodata(struct my_info bio);

int main()
{
    struct my_info koushik;
    printf("Please enter your name:");
    scanf("%s",&koushik.name);

    printf("Please enter your age:");
    scanf("%d",&koushik.age);

    printf("Please enter your home town:");
    scanf("%s",&koushik.home_town);

    printf("Please enter your school:");
    scanf("%s",&koushik.school);

    printf("Please enter your college name:");
    scanf("%s",&koushik.college);

    printf("Please enter your versity name:");
    scanf("%s",&koushik.versity);

    printf("Please enter your life_style:");
    scanf("%s",&koushik.life_style);


    biodata(koushik);

    return 0;
}
void biodata(struct my_info bio)
{
    printf("\n-------------------------------------\n");
    printf("koushik ahamed kushal's biodata:");
    printf("\n-------------------------------------\n");

    printf("\nNAME:%s\n",bio.name);

    printf("\nAGE:%d\n",bio.age);

    printf("\nHOME TOWN:%s\n",bio.home_town);

    printf("\nSCHOOL:%s\n",bio.school);

    printf("\nCOLLEGE:%s\n",bio.college);

    printf("\nUNIVERSITY:%s\n",bio.versity);

    printf("\nLIFE-STYLE:%s\n",bio.life_style);

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

}
 output type:
                                             

Monday, July 13, 2015

Find the largest value from an array (take value by pointer)

#include <stdio.h>

int main()
{
    int i,n;

    int arr[100];

    printf("Enter number:");
    scanf("%d",&n);
    
    for(i=0;i<n;++i)  
    {
      
       scanf("%d",(arr+i));
    }
    for(i=1;i<n;++i)  
    {
       if(arr[0]<arr[i]) 
           arr[0]=arr[i];
    }
    printf("Largest number is= %d",arr[0]);
    return 0;
}


output:


Enter number:6
7 8 4 9 0 3
Largest number is = 9

Pointers and functions

#include <stdio.h>

void swap(int *a,int *b);

int main()
{
  int num1=5,num2=10;

  printf("Before swapping: num1=%d  num2=%d",num1,num2);

  swap(&num1,&num2); 

  printf("\n After  swapping:num1=%d  num2=%d",num1,num2);

  return 0;
}
void swap(int *a,int *b)
{
  int temp;

  temp=*a;
  *a=*b;
  *b=temp;
}


output:


Before swapping: num1=5  num2=10

 After  swapping:num1=10  num2=5

Pointer and array

#include <stdio.h>
int main()
{
  int i,arr[5],sum=0;

  printf("Enter 5 numbers:");

  for(i=0;i<5;i++)
{
      scanf("%d",(arr+i));
      sum += *(arr+i); 
  }
  printf("Sum=%d",sum);

  return 0;
}


output: 


3 4 5 6 7
Sum=25

Starting pointer

#include <stdio.h>
int main()
{
  int num=5;
  printf("Value: %d\n",num);

  printf("Address: %d\n",&num); 

  printf("Address:%u\n",&num);
  return 0;
}



output:


 Value: 5
Address: 2081809404

Address:2081809404

L.C.M and G.C.D by function

#include<stdio.h>

int main(){
  int n1,n2,x,y,lcm,gcd;
  printf("\nEnter two numbers:");
  scanf("%d %d",&n1,&n2);
  lcm=func1(n1,n2);
    gcd=func2(n1,n2);
  
  printf("L.C.M=%d G.C.D=%d",lcm,gcd);
  return 0;
}

int func1(int n1,int n2){
    int x,y;
    
    
   x=n1,y=n2;
  while(n1!=n2){
      if(n1>n2)
           n1=n1-n2;
      else
      n2=n2-n1;
  } 
  return (x*y)/n1;
    
}

int func2(int n1,int n2){
    int x,y;
    
   x=n1,y=n2;
  while(n1!=n2){
      if(n1>n2)
           n1=n1-n2;
      else
      n2=n2-n1;
  } 
  return n1;
    
}



output:

                                       

Friday, July 10, 2015

factorial by function

#include <stdio.h>

int factorial(int n);

int main()
{
  int fact;


  printf("Please enter a number:");
  scanf("%d", &fact);

  printf("The factorial of %d is:%d",fact, factorial(fact));

  return 0;
}

int factorial(int n)
{
  int i;
int factorial = 1;

  for (i = 1; i<= n; i++)
    factorial = factorial * i;

  return factorial;
}


output:
                                                   

Leap year by function

#include<stdio.h>

int main()
{
    int a,year;
    year=leap_year(a);
    if(((year%4==0)&&(year%100!=0))||(year%400==0))

        printf("%d is a leap year",year);
    else
        printf("%d is not a leap year",year);

    return 0;
}


int leap_year(int year)
{


    printf("Enter any year: ");
    scanf("%d",&year);
    return year;


}

prime number by function

#include <stdio.h>

void prime(int num);

int main(){
    int n;
    printf("Please enter a number:");
    scanf("%d",&n);
    prime(n);
    return 0;
}
void prime(int num){

    int i;
for(i=2; i<=num-1; i++){
    if(num%i==0){
        printf("%d is not a prime number",num);
        break;
    }
    }
    if(num==i)
    printf("%d is a prime number",num);
}



///another way

#include<stdio.h>
int prime(int a);

int main()
{
    int num,i,n;
    num=prime(n);
    for(i=2; i<=num-1; i++)
    {

    if(num%i==0){
        printf("%d is not a prime number",num);
        break;
    }
    }
    if(num==i)
        printf("%d is a prime number",num);

    return 0;
}
int prime(int a){

    printf("Please enter a number:");
    scanf("%d",&a);
    return a;

}

Sunday, July 5, 2015

Powe of base

#include<stdio.h>

int poo( int,int);

int main()

{
    int add,power,base;
    printf("please enter base and power:");
    scanf("%d%d",&base,&power);

    add=poo(base,power);
    printf("the result is:%d",add);

    return 0;

}
int poo( int b,int p)
{
    int i,sum=1;
    for(i=1; i<=p; i++)
    {
        sum=sum*b;

    }

    return(sum);
}

Friday, July 3, 2015

Pascal triangle

#include<stdio.h>

///pascal triangle

int fact(int num);

int main()
{
    int row,i,j;

    printf("Enter the no. of rows:");

    scanf("%d",&row);

    for(i=0; i<row; i++)
    {

        for(j=0; j<row-i-1; j++)

            printf(" ");

        for(j=0; j<=i; j++)

            printf("%d ",fact(i)/(fact(j)*fact(i-j)));

        printf("\n");
    }
    return 0;
}

int fact(int num)
{
    int fact=1;
    int i;
    for(i=1; i<=num;i++)
    {
        fact=fact*i;
    }
    return fact;
}






output:







                                         
                             

Floyd's Triangle

#include <stdio.h>

int main()
{
  int n, i,  c, a = 1;

  printf("Enter the number of rows:");
  scanf("%d", &n);

  for (i = 1; i <= n; i++)
  {
    for (c = 1; c <= i; c++)
    {
      printf("%d ",a);
      a++;
    }
    printf("\n");
  }

  return 0;
}


output:
                                                        

Swapping problem


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


int main()
{
   char first[100], second[100], temp[100];

   printf("Enter the first string:");
   gets(first);

   printf("Enter the second string:");
   gets(second);

   printf("\nBefore Swapping:\n");
   printf("First string: %s\n",first);
   printf("Second string: %s\n\n",second);



   strcpy(temp,first);
   strcpy(first,second);
   strcpy(second,temp);

   printf("After Swapping:\n");
   printf("First string: %s\n",first);
   printf("Second string: %s\n",second);

   return 0;
}

Square and square root of an array

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

int main()
{
    int arr[5],i,square_root;

    printf("Please enter a five number of an array:");

    for(i=0; i<5; i++)
    {
        scanf("%d",&arr[i]);

    }
    printf("\nThe square if the stored array is:");
    for(i=0; i<5; i++)
    {

        printf("%d ",arr[i]*arr[i]);
    }
    printf("\nThe square root of The array is:");

    for(i=0; i<5; i++)
    {

        square_root= sqrt(arr[i]*arr[i]);

        printf("%d ",square_root);

    }

    return 0;
}

Thursday, July 2, 2015

Decimal to binary by function

#include<stdio.h>
void binary(int decimal)

{
    int r,bin=0,p=1;

    while(decimal!=0)
    {
       r=decimal%2;

       bin=bin+p*r;

       p=p*10;

       decimal=decimal/2;


    }
    printf("%d",bin);

}
int main()
{
    int a;

    scanf("%d",&a);

    binary(a);

    return 0;
}

Binary search

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

int main()
{
    int arr[8]={1,5,7,9,11,13,15,17};
    int i,start=0,end=7,mid,key;
    scanf("%d",&key);

    do{
        mid=(int)((start+end)/2);
        if(arr[mid]==key){

            printf("Find at the position of:%d\n",mid+1);
            break;
        }
        else if(arr[mid]>key){
            end=mid-1;
        }
        else{

            start=mid+1;
        }

    }
    while(start<=end);
    if(start>end)
        printf("%d is not found!!!\n",key);
    return 0;
}

Linear search

#include <stdio.h>

int main()
{
   int array[100], search, c, n;

   printf("Enter the number of elements in array:");
   scanf("%d",&n);

   printf("\nEnter %d integer(s):", n);

   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);

   printf("\nEnter the number to search\n");
   scanf("%d", &search);

   for (c = 0; c < n; c++)
   {
      if (array[c] == search)     /* if required element found */
      {
         printf("%d is present at location %d.\n", search, c+1);
         break;
      }
   }
   if (c == n)
      printf("%d is not present in array.\n", search);

   return 0;
}

Wednesday, July 1, 2015

Function perfect number

#include<stdio.h>

void perfect(int j);

int main(){
int n;
printf("Enter a number: ");
  scanf("%d",&n);
  perfect(n);

  return 0;
}

void perfect(int j){

  int i=1,sum=0;



  while(i<j){
      if(j%i==0)
           sum=sum+i;
          i++;
  }
  if(sum==j)
      printf("%d is a perfect number",i);
  else
      printf("%d is not a perfect number",i);

}

Square of a number by function


#include<stdio.h>

int square(int);

int main(){
    
int a;

printf("Please enter a number:");
scanf("%d",&a);

printf("\nThe square of %d is:%d\n",a,square(a));

return 0;
}
int square(int x){

int y;
y=x*x;

return y;

}

Palindrome by string

#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;
}

***find a specific value from an array

#include<stdio.h>


int main()
{

   int arr[2][2];


   int i, j;

   for(i=0; i<2; i++)
   {
       for(j=0;j<2;j++)
       {
          printf("\nEnter value for arr[%d][%d]:", i, j);

          scanf("%d", &arr[i][j]);
       }
    }

    printf("\nThe value of the selected array is :%d\n",arr[1][1]);

    return 0;
}

2D-array

#include<stdio.h>

///matrix summation


int main()
{

    int a[10][10], b[10][10], sum[10][10], i, j, m,n,p,q;
    printf("Enter the order of first matrix: ");
    scanf("%d%d",&m,&n);
    printf("Enter the order of second matrix: ");
    scanf("%d%d",&p,&q);
    if(m!=p && n!=q){
        printf("Error match!!");
        exit(0);
    }
    printf("Enter first matrix: \n");
    for(i = 0 ; i < m; i++){
        for(j = 0; j < n; j++)
            scanf("%d", &a[i][j]);
    }
    printf("Enter second matrix: \n");
    for(i = 0 ; i < p; i++){
        for(j = 0; j < q; j++)
            scanf("%d", &b[i][j]);
    }
    for(i = 0 ; i < m; i++){
        for(j = 0; j < n; j++)
            sum[i][j] = a[i][j] + b[i][j];
    }
    printf("The sum of the matrix is :\n");
    for(i = 0 ; i < m; i++){
        for(j = 0; j < n; j++){
            printf("%d ", sum[i][j]);

        }
        printf("\n");
    }
    return 0;
}

Tuesday, June 30, 2015

1-D array

#include <stdio.h>

int main()
{
int num[] = {2,8,7,6,0};
int i;

for(i=0;i<5;i++) {
    printf("\nArray Element of num[%d] : %d",i+1,num[i]);
}
    
return 0;
}



///different way

int  main()
{
     int arr[3],i;
     printf(“Enter 3 values:”);
     for(i=0;i<3;i++)
     {
          scanf("%d",&arr[i])
     }
     printf(“\nThe entered values are:”);
     for(i=0;i<3;i++)
     {
          printf("%d ",arr[i]);
     }
return 0;

}

Calculator by function

#include<stdio.h>

int add(int x, int y );
int sub(int a, int b );
int mult(int d, int e);
int div(int g, int h);

int main(){
int num1, num2, choice;

printf("[0] Exit\n[1] Addition\n[2] Subtraction\n[3] Multiplication\n[4] Division\n");
scanf("%d", &choice);

switch(choice){
    case 0:
        exit(0);
    case 1:
        printf("Enter 1st number:");
        scanf("%d", &num1);
        printf("\nEnter 2nd number:");
        scanf("%d", &num2);
        printf("\nThe addition is:%d\n", add( num1,num2));
        break;
    case 2:
        printf("Enter 1st number:");
        scanf("%d", &num1);
        printf("\nEnter 2nd number:");
        scanf("%d", &num2);
        printf("\nThe subtraction is:%d\n", sub(num1,num2));
        break;
    case 3:
        printf("Enter 1st number:");
        scanf("%d", &num1);
        printf("\nEnter 2nd number:");
        scanf("%d", &num2);
        printf("\nThe multiplication is:%d\n", mult(num1,num2));
        break;
    case 4:
        printf("Enter 1st number:");
        scanf("%d", &num1);
        printf("\nEnter 2nd number:");
        scanf("%d", &num2);
        printf("\nThe division is:%d\n", div(num1,num2));
        break;
    default:
        printf("\nInvalid choice!!!!!!!\n");
        break;
}
}
int add(int x,int y){
    int z;
    z = x + y;
    return z;
}

int sub(int a,int b){
    int c;
    c = a - b;
    return  c;
}

int mult(int d,int e){
    int f;
    f = d * e;
    return f;
    }

int div(int g,int h){
    int i;
    i = g / h;
    return i;
}

Swapping elements by function

#include<stdio.h>
///Swapping by function

void swap(int a, int b);

int main()
{
   int x, y;

   printf("Enter the value of x and y:");
   scanf("%d%d",&x,&y);

   printf("Before Swapping\nx = %d\ny = %d\n", x, y);

   swap(x,y);



   return 0;
}

void swap(int a, int b)
{
   int temp;

   temp = a;
   a   = b;
   b   = temp;
 printf("After Swapping\nx = %d\ny = %d\n", a, b);
}

Sunday, June 28, 2015

Hello world

#include <iostream>

/***
using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

***/
/***
// my second program in C++
#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World! ";
  cout << "I'm a C++ program";
}
***/
/***

// my second program in C++
#include <iostream>

int main ()
{
  std::cout << "Hello World! ";
  std::cout << "I'm a C++ program";
}
***/

The value of EOF

#include <stdio.h>

int main(void)
{
printf("EOF == %d\n", EOF);

return 0;
}

output:

EOF = = -1

Sort a string

#include<stdio.h>
#include<conio.h>

///sort a string

int main()
{
    char str[100],temp;
    int i,j;

    printf("Enter the string :");
    gets(str);

    for(i=0;i<strlen(str);i++)
    {
        for(j=i+1;j<strlen(str);j++)
        {
            if(str[j]<str[i])
            {
                temp=str[j];
                str[j]=str[i];
                str[i]=temp;
            }
        }
    }
    printf("\n------------------------------------\n");
    printf("\nThe sorted string is:%s\n",str);
     printf("\n------------------------------------\n");

    return 0;
}

Upper case letter to lower case letter

#include<stdio.h>
/***
int main()
{
 char a;


 printf("enter a character:");
 while(1){

 scanf("%c",&a);
  if(a==48)
    break;
 if(a>=65&&a<=90)
   printf("%c ",a+32);


}
return 0;
}

***/

#include<stdio.h>

int main(){

  char str[100];
  int i;

  printf("Enter any string:\n");
  scanf("%s",str);

  printf("\nThe string is:%s\n",str);

  for(i=0;i<strlen(str);i++){

      if(str[i]>=65&&str[i]<=90)
       str[i]=str[i]+32;
  }
  printf("\nThe Lower case string is:%s\n",str);
  return 0;
}

Common number in a array

#include<stdio.h>

int main()
{
    int i,j=5,a[4],n,count=0;

   

    for(i=0; i<4; i++)

        scanf("%d",&a[i]);

    for(i=0; i<4; i++)
    {
        printf("%d ",a[i]);

        if(a[i]==j)
        {
            count++;
        }
    }

    printf("\nNo 5 exist in this array %d times",count);

    return 0;

}

Saturday, June 27, 2015

Least common multiple & greatest common divisor

#include<stdio.h>

/*** l.c.m and g.c.d ***/

int main()
{

  int num1,num2,x,y;

  printf("\nEnter two numbers:");
  scanf("%d %d",&num1,&num2);

  x=num1,y=num2;

  while(num1!=num2){
      if(num1>num2)
           num1=num1-num2;
      else
      num2=num2-num1;
  }
  printf("L.C.M=%d\nG.C.D=%d",x*y/num2,num1);
  return 0;
}

npr & ncr

#include<stdio.h>

///npr and ncr

int main()
{

    int n,r,ncr,npr;

    printf("Enter two numbers(n,r):");

    scanf("%d %d",&n,&r);

    ncr=fact(n)/(fact(r)*fact(n-r));

    npr=fact(n)/(fact(n-r));

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

    printf("\nThe ncr and npr is:%dC%d=%d %dP%d=%d",n,r,ncr,n,r,npr);

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

    return 0;
}
int fact(int n)
{

    int factorial=1,i;

for(i=1;i<=n;i++)
{


    factorial=factorial*i;

}
    return factorial;

}


output:



Enter two numbers(n,r):5 4

---------------------------------------
The ncr and npr is:5C4=5 5P4=120
---------------------------------------

Friday, June 26, 2015

We are in love

#include<stdio.h>
///simple loving program
///koushikahamed@yahoo.com

int main()
{
    system("color 1C");
    char a[100],b [100],approval[10],yes[10];


    printf("Please enter the boy name:");
    gets(a);

    printf("\nPlease enter the girl name:");
    gets(b);

    printf("\n%s Do you love %s ??\n",b,a);

    printf("\nplease don't be late!!!!!  %s is waiting\n",a);

    printf("\nexpress your feelings %s\n",b);

    printf("\nif you interested with %s type y/no\n",a);

    scanf("%s",approval);

    printf("\nif you love %s type y/no\n",a);

    scanf("%s",yes);

    strcpy(approval,yes);

    strrev(approval);

    if(strcmp(approval,yes)==0)
    {
    printf("\n<<<%s + %s>>> is a great couple>>>\n",a,b);
    printf("\n");

    printf(" .*'''*'''*:*'''*'''*;        \n");
    printf("  * %s+%s '''              \n",a,b);
    printf("   *              *            \n");
    printf("     *          *              \n");
    printf("       *      *                \n");
    printf("           *                   \n");


    }
    else if(strcmp(approval,yes)!=0)
        printf("\n%s and %s is not couple they are seems to be good friend>>>\n",a,b);

    return 0;
}


output:


Please enter the boy name:Romeo

Please enter the girl name:Juliet

Juliet Do you love Romeo ??

please don't be late!!!!!  Romeo is waiting

express your feelings Juliet

if you interested with Romeo type y/no
y

if you love Romeo type y/no
y

<<<Romeo + Juliet>>> is a great couple>>>

 .*'''*'''*:*'''*'''*;
 * Romeo+Juliet '
   *                    *
     *               *
       *        *
           *

Array reverse,summation ,average

#include<stdio.h>
#include<conio.h>
int main()
{
    int a[100],b[100],temp,i,j,n,sum=0;
    float avg;

    printf("How many numbers do you want in array:");
    scanf("%d",&n);

    puts("\nplease enter the number of array:");

    for(i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);

        
}
    for(i=1; i<=n; i++)
    {
        printf("%d",&a[i]);

        sum=sum+a[i];

        avg=(float)(sum)/n;

    }
    puts("----------------------");

    printf("The sum  of the array is:%d",sum);

    puts("\n----------------------");

    printf("The average of the array is:%.2f",avg);

    puts("\n----------------------");

    puts("The reversed array is:");

    puts("----------------------");
    for(i=n; i>=1; i--)

    {
        printf("%d ",a[i]);
    }
    return 0;
}



output:

How many numbers do you want in array:5

please enter the number of array:
4 7 9 3 8
----------------------
The sum  of the array is:31
----------------------
The average of the array is:6.20
----------------------
The reversed array is:
----------------------

8 3 9 7 4

Wednesday, June 24, 2015

Pattern

#include<stdio.h>
///pattern
int main()
{
    int i,rows,n;
    char j;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
   
    for(i=65;i<=65+rows;i++)
    {
        for(j=65;j<i;j++)
        {
           printf("%c ",j);
        }
    printf("\n");
    }
    return 0;
}

output:

A
AB
ABC
ABCD
ABCDE
ABCDEF
ABCDEFG
ABCDEFGH


Friday, June 19, 2015

pattern-character

int main()
{
    int i,rows,n;
    char j;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    printf("Enter how much number or char you will print: ");
    scanf("%d",&n);
    for(i=1;i<=rows;i++)
    {
        for(j=65;j<=n-1;j++)
        {
           printf("%c ",j);
        }
    printf("\n");
    n--;
    }
    return 0;
}
otuput:


 A B C D E F G H I J
A B C D E F G H I
A B C D E F G H
A B C D E F G
A B C D E F
A B C D E
A B C D
A B C
A B
A




Sunday, June 14, 2015

series((1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n))

#include<stdio.h>

///(1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)

int main()
{
    int i,j,n,sum=0;
    n=10;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            sum+=j;
        }
    }
    printf("Sum: %d",sum);
    return 0;
}

Saturday, June 13, 2015

series(1+3+5+7+9+...........+n)

#include<stdio.h>
///odd number series:
int main ()
{
    int i,num,sum=0;
    printf("Please enter any number:");
    scanf("%d",&num);

    for(i=1;i<=num;i=i+2)
    {
      printf("%d ",i);
      sum=sum+i;
    }

    printf("\nThe sum of the odd number series is:%d",sum);

    return 0;
}

series(2+4+6+8+............+n)

#include<stdio.h>
///Even number series:
int main ()
{
    int i,num,sum=0;
    printf("Please enter any number:");
    scanf("%d",&num);

    for(i=2;i<=num;i=i+2)
    {
      printf("%d ",i);
      sum=sum+i;
    }

    printf("\nThe sum of the even number series is:%d",sum);

    return 0;
}

series(1/1 + 1/2 + 1/3 + 1/4 + . . . + 1/n----A.P)

#include<stdio.h>
///1/1 + 1/2 + 1/3 + 1/4 + . . . + 1/n
int main()
{

    float num,i,sum=0;

    printf("Please enter any number:\n");
    scanf("%f", &num);

    for (i=1;i<=num;i++)
    {
        sum=sum +(1/i);
        if (i==1)
            printf("\nThe series is: 1+");
        else if (i==num)
            printf("(1/%.0f)\n",i);
        else
            printf("(1/%.0f)+",i);
    }
    printf("\nThe sum of the series is:%.2f\n", sum);
    return 0;
}

series(1^3 + 2^3 + 3^3+ .....+n^3)


///1^3 + 2^3 + 3^3+ .....+n^3
int main(){

    int n,i;
    int sum=0;

    printf("Enter any end number of this series: ");
    scanf("%d",&n);

    sum = (((n*(n + 1) ) / 2))*(((n*(n + 1) ) / 2));

    printf("\nSum of the series:");

    for(i =1;i<=n;i++){
         if (i!= n)
             printf("%d^3+",i);
         else
             printf("%d^3=%d\n ",i,sum);
    }

    return 0;
}

series(1^2 + 2^2 + …. + n^2)

#include<stdio.h>
///1^2 + 2^2 + …. + n^2
int main(){

    int n,i;
    int sum=0;

    printf("Enter any end number of this series: ");
    scanf("%d",&n);

    sum = (n * (n + 1) * (2 * n + 1 )) / 6;

    printf("Sum of the series : ");

    for(i =1;i<=n;i++){
         if (i != n)
             printf("%d^2 + ",i);
         else
             printf("%d^2 = %d ",i,sum);
    }

    return 0;
}

series(1 + 2 + 3 + … + n)

#include<stdio.h>
///1 + 2 + 3 + … + n
int main(){

    int n,i;
    int sum=0;

    printf("Enter any end number of this series: ");
    scanf("%d",&n);

    sum = (n * (n + 1)) / 2;

    printf("Sum of the series: ");

    for(i =1;i <= n;i++){
         if (i!=n)
             printf("%d + ",i);
         else
             printf("%d=%d ",i,sum);
         }

    return 0;
}

Thursday, June 4, 2015

Array(3)

#include <stdio.h>
///search a number using an arary using linear search.

int main()
{
int ara[100],i,key,n,loc,ck;
scanf("%d",&n);
for(i=0;i<n;i++)
    scanf("%d",&ara[i]);
    scanf("%d",&key);
    ck=0;
    for(i=0;i<n;i++){
      if (ara[i]==key){
        ck=1;
        loc=i+1;
        break;
      }
    }
    if (ck==1)
        printf("%d",loc);
    else
        printf("No");
        return 0;
}

Array(2)

#include <stdio.h>
///array traversing/array searching

int main()
{
int ara[5]={1,44,67,56,46},i;
///for(i=0;i<5;i++){
 ///   printf("%d ",ara[i]);
///}
///for(i=4;i>=0;i--){
   printf("%d ",ara[3]);
///}
return 0;
}

Array(1)

#include <stdio.h>
///array_declare

int main()
{
int ara[5]={1,2,4,6,99},i;
ara[0]=5;
ara[3]=66;

for(i=0;i<5;i++){
    printf("%d\n",ara[i]);
}
return 0;
}

string(11)

#include <stdio.h>
///string function:
///***strspn ,strcspn****

///int main()
///{
///char str1[100]="koushik";
///char str2[100]="kbcdef";
///int len;
///len=strspn(str1,str2);
///printf("%d\n",len);
///return 0;
///}



int main()
{
char str1[100]="koushik";
char str2[100]="abcdhf";
int len;
len=strcspn(str1,str2);
printf("%d\n",len);
return 0;
}

string(10)

#include <stdio.h>
#include <string.h>
///string (function)

///int main()
///{
///char str[100]="koushik";
///strlen(str);
///printf("%d\n",strlen(str));
///return 0;
///}


///int main()
///{
///char str1[100]="koushik";
///char str2[100]="Ahamed";
///strcpy(str1,str2);
///printf("%s\n",str1);
///return 0;
///}


///int main()
///{
///char str1[100]="koushik";
///char str2[100]="Ahamed";
///strncpy(str1,str2,3);
///str1[3]='\0';
///printf("%s\n",str1);
///return 0;
///}

///int main()
///{
///char str1[100]="koushik";
///char str2[100]="Ahamed";
///strcat(str1," ");
///strcat(str1,str2);
///printf("%s\n",str1);
///return 0;
///}

///int main()
///{
///char str1[100]="koushik";
///char str2[100]="Ahamedkushal";
///strncat(str1,str2,6);
///printf("%s\n",str1);
///return 0;
///}


///int main()
///{
///char str1[100]="BCD";
///char str2[100]="AYZ";
///strcmp(str1,str2);
///printf("%d",strcmp(str1,str2));
///return 0;
///}


///int main()
///{
///char str1[100]="BCD";
///char str2[100]="XYZ";
///strcmp(str1,str2);
///printf("%d",strcmp(str1,str2));
///return 0;
///}


///int main()
///{
///char str1[100]="BCD";
///char str2[100]="BCD";
///strcmp(str1,str2);
///printf("%d",strcmp(str1,str2));
///return 0;
///}


///int main()
///{
///char str1[100]="BCD";
///char str2[100]="BCD";
///if(strcmp(str1,str2)==0);
///printf("They are same\n");
///return 0;
///}


///int main()
///{
///char str1[100]="BCD";
///char str2[100]="BCD";
///if(!strcmp(str1,str2));
///printf("They are same\n");
///return 0;
///}


///int main()
///{
///char str1[100]="BBD";
///char str2[100]="BDB";
///strncmp(str1,str2,2);
///printf("%d",strncmp(str1,str2,2));
///return 0;
///}


///int main()
///{
///char str1[100]="BBD";
///char str2[100]="BBB";
///if(!strncmp(str1,str2,2));
///printf("They are same\n");
///return 0;
///}


int main()
{
char str1[100]="BBB";
char str2[100]="BBB";
printf("%d",strcoll(str1,str2));
return 0;
}

///***strcmp and strcoll closely same ***///


string(9)

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

int main()
{
char n1[100],n2[100];
while(scanf("%s%s",n1,n2) !=EOF){
  ///  if(strchr(n1)>strchr(n2))
    ///    printf("a > b \n");
   ///else if(strchr(n1)<strchr(n2))
     ///   printf("a < b \n");
   if(strlen(n1)>strlen(n2))
        printf("a > b \n");
   else if(strlen(n1)<strlen(n2))
        printf("a < b\n");
   else if(strcmp(n1,n2)==0)
   printf("a = b\n");
   else if(strcmp(n1,n2)>0)
   printf("a > b\n");
   else
       printf("a < b\n");

}
return 0;
}

Monday, May 25, 2015

string(8)

#include <stdio.h>
///copy a string to another string

int main()
{
    char ch[15],ch1[15];
    int i;
    scanf("%s",ch);
    for(i=0;ch[i]!='\0';i++)

    ch1[i]=ch[i];
    ch1[i]=ch[i];
    printf("%s",ch1);

    return 0;
}

string(7)

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

int main()
{
    char ch[15],ch1[15];
    int i,n;
    scanf("%s%s",ch,ch1);
    for(i=0;ch[i]!='\0';i++);
        ch[i]=' ';
       i++;

    for(n=0;ch1[n]!='\0';n++)
        ch[i+n]=ch1[n];
    printf("%s",ch);

    return 0;
}

Sunday, May 24, 2015

string(6)

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

int main()
{
    char ch[10][100],i,n;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%s",ch[i]);
    }
    for(i=n-1;i>=0;i--){
        printf("%s\n",ch[i]);
    }
    return 0;
}

string(5)

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

int main()
{
char ch[100];
gets(ch);
int a,b;
printf("%*.*s",10,5,ch);
return 0;
}

string(4)

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

int main()
{
char ch[100];
while(gets(ch)){
    if(ch[0]=='\0')
        return 0;
    printf("%s\n",ch);
}
return 0;
}

string(3)

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

///int main()
///{
    ///char ch[100];
   /// scanf("%[^\n]",ch);////***same output
  ///  printf("%s",ch);
  ///  return 0;
//}
int main()
{
    char ch[100];
    gets(ch);///***same output
    printf("%s",ch);
    return 0;
}

string(2)

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

int main()
{
char str[100];
gets(str);
printf("%s\n",str);
gets(str);
printf("%s\n",str);
gets(str);
printf("%s\n",str);
return 0;
}

string(1)

#include <stdio.h>
///character string & array:

int main()
{
char ch[10];

ch[0]='K';
ch[1]='o';
ch[2]='u';
ch[3]='s';
ch[4]='h';
ch[5]='i';
ch[6]='k';
ch[7]='\0';///string terminating character:
ch[8]='t';
ch[9]='m';

printf("%s",ch);

return 0;
}

Thursday, January 1, 2015

pattern-4

1:
#include<stdio.h>
int main()
{
    int row,space,star,n;
    printf("please enter the number of lines:");
    scanf("%d",&n);
    for(row=1;row<=n;row++)
    {
        for(space=n-row;space>=0;space--)
    {
            printf(" ");
    }
    for(star=1;star<=2*row-1;star++)
    {
        printf("*");
    }
    printf("\n");
    }
    for(row=n;row>=1;row--)
    {
        for(space=n-row;space>=0;space--)
    {
            printf(" ");
    }
    for(star=1;star<=2*row-1;star++)
    {
        printf("*");
    }
    printf("\n");
    }
return 0;
}

input: 7
output:             
                                                        *
                                                      ***
                                                    *****
                                                  *******
                                                *********
                                              ***********
                                            *************
                                            *************
                                              ***********
                                                *********
                                                  *******
                                                    *****
                                                      ***
                                                        *
                                         

pattern-3

1:
#include<stdio.h>
int main()
{
    int row,space,star,n;
    printf("please enter the number of lines:");
    scanf("%d",&n);
    for(row=n;row>=1;row--)
    {
        for(space=0;space<row;space++)
    {
            printf(" ");
    }
    for(star=n;star>=row;star--)
    {
        printf("*");
    }
    printf("\n");
    }
return 0;
}

input: 7
output:             
                                        *
                                      **
                                    ***
                                  ****
                                *****
                              ******
                            *******




  
2:  
#include<stdio.h>
int main()
{
    int row,space,star,n;
    printf("please enter the number of lines:");
    scanf("%d",&n);
    for(row=n;row>=1;row--)
    {
        for(space=n;space>row;space--)
    {
            printf(" ");
    }
    for(star=1;star<=row;star++)
    {
        printf("*");
    }
    printf("\n");
    }
return 0;
}
 
input: 7
output:             
                                           *******
                                             ******
                                               *****
                                                 ****
                                                   ***
                                                     **
                                                       *

                             
           
                    

pattern-2

1:
#include<stdio.h>
int main()
{
    int row,space,star,n;
    printf("please enter the number of lines:");
    scanf("%d",&n);
    for(row=1;row<=n;row++)
    {
        for(space=n-row;space>=0;space--)
    {
            printf(" ");
    }
    for(star=1;star<=2*row-1;star++)
    {
        printf("*");
    }
    printf("\n");
    }
return 0;
}

input: 7
output:
                                *
                              ***
                            *****
                          *******
                        *********
                      ***********
                    *************


2:
#include<stdio.h>
int main()
{
    int row,space,star,n;
    printf("please enter the number of lines:");
    scanf("%d",&n);
    for(row=n;row>=1;row--)
    {
        for(space=n-row;space>=0;space--)
    {
            printf(" ");
    }
    for(star=1;star<=2*row-1;star++)
    {
        printf("*");
    }
    printf("\n");
    }
return 0;
}


input: 7
output:
  
                    *************
                      ***********
                        *********
                         *******
                           *****
                            ***
                              *