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