Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Friday, June 24, 2016

C program to understand the concept of initialization in 2D arrays

#include<stdio.h>
void main()
{
//here inner curly braced values will be assigned to 0th array and outer to 1th array
 int arr_new[3][4]={{1,2},3,4};
 arr_new[2][3]=0;
 printf("\n%d %d %d %d %d",arr_new[0][0],arr_new[0][1],arr_new[0][2],arr_new[1][0],arr_new[1][1]);
}

output-
1 2 0 3 4

Sunday, June 19, 2016

C program to make two matrix dynamically and show thier addition

#include <stdio.h>
#include<stdio.h>
int main()
{
   int row, colum, row_element, colum_element, first[50][50], second[50][50], sum[50][50];
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &row, &colum);
   if(row>0&&colum>0)
   {
   printf("Enter the elements of first matrix\n");
   for (row_element = 0; row_element < row; row_element++)
   {
     for (colum_element= 0; colum_element <colum; colum_element++)
      {
         scanf("%d", &first[row_element][colum_element]);
      }
   }
   printf("Enter the elements of second matrix\n");
   for (row_element = 0; row_element < row; row_element++)
   {
      for (colum_element = 0 ; colum_element< colum; colum_element++)
      {
            scanf("%d", &second[row_element][colum_element]);
      }
   }
   printf("Sum of entered matrices:-\n");
   for (row_element = 0; row_element< row; row_element++)
    {
      for (colum_element= 0 ; colum_element < colum; colum_element++)
  {
    sum[row_element][colum_element] = first[row_element][colum_element] +                                        second[row_element][colum_element];
    printf("%d\t", sum[row_element][colum_element]);
      }
 printf("\n");
   }
   }
   else
   printf("please enter positive number only");
}

output
Enter the number of rows and columns of matrix
2 2
Enter the elements of first matrix 1
2
3
2
Enter the elements of second matrix 2
3
2
1
Sum of entered matrices:-
3  5
5  3

Wednesday, June 8, 2016

C program to select elements from three given array and make sum of zero

Problem Statement: you're given three arrays and you have to pick one element from each array to make sum of zero
Example
 int a[]={1,7,5,9};
 int b[]={-2,-1,1,2};
 int c[]={-8,-7,1,-8};
taking 1 from a,-2 from b and 1 from c is making a combination with sum as zero
1 + -2 + 1 = 0

now ignoring these values, and repeating the process
taking 7 from first array, taking -1 from b isn't suiting with any element of c, taking 1 from b and -8 from c making combination with sum as zero
7 + 1 + -8 = 0

repeat this procedure

#include<stdio.h>
void main()
{
 int a[]={1,7,5,9};
 int b[]={-2,-1,1,2};
 int c[]={-8,-7,1,-8};
 int i,j,k;
 int occupy[100];
 int count=0, flag=0,temp;
 for(i=0;i<4;i++)
  {
   for(j=0;j<4;j++)
    {
     for(k=0;k<4;k++)
      {
       if((a[i]+b[j]+c[k])==0)
{
flag=0;
temp=count;
while(temp>0)
{
 if((i==occupy[temp*3-3])||(j==occupy[3*temp-2])||(k==occupy[3*temp-1]))
  {
   flag=1;
   break;
  }
  temp--;
 }
 if(flag==0)
  {
   printf("%d %d %d \n",a[i],b[j],c[k]);
   count++;
   occupy[3*count-3]=i;
   occupy[3*count-2]=j;
   occupy[3*count-1]=k;
  }
}
      }
    }
  }
}

output
1 -2 1
7 1 -8
5 2 -7
9 -1 -8

Saturday, June 4, 2016

C program to check given number in strong number or not

#include<stdio.h>
//function to calculate factorial
int fact(int i)
 {
  int k=1;
  if(i==1)
   {
    return 1;
   }
  else
   {
    k=i*fact(i-1);
   }
   return k;
 }
void main()
 {
  int n,m,sum=0,a;
  int i;
  printf("Enter a no");
  scanf("%d",&n);
  m=n;
  while(n>0)
   {
    a=n%10;
    sum=sum+fact(a);
    n=n/10;
   }
  if(m==sum)
   {
    printf("No is strong no");
   }
  else
   {
    printf("Not a strong no");
   }
}

output
Enter a no 32
Not a strong no

Thursday, June 2, 2016

c program to count number of 1 in binary representation of a number

Problem Statement: count the no of ones in binary repesentation of a decimal number
#include<stdio.h>
#include<math.h>
void main()
{
 int i,k,l,j=0;
 printf("\nEnter a no");
 scanf("%d",&i);
 l=i;
//it will give the size of any data type in bits
 k=8*sizeof(int);
 while(k!=0)
  {
   if(i&1==1)
    {
     j++;
    }
   i=i>>1;
   k--;
  }
 printf("\nNo of one set in number is %d",j);
}

output
Enter a no10
No of one set in number is 2

Tuesday, May 31, 2016

C program to print binary representation of number

#include<stdio.h>
#include<math.h>
//function to print binary number of a decimal
void binary(int i)
{
 if(i!=0)
  {
   binary(i>>1);
  }
 printf("%c",i&1==1?'1':'0');
}
void main()
{
 int i,k,l,j=0;
 printf("\nEnter a no");
 scanf("%d",&i);
 l=i;
 //To print binary equivalent
 printf("\nBinary representation of the no is ");
 binary(l);
}

output
Enter a no 54
Binary representation of the no is 110110

Sunday, May 29, 2016

C program to sort list of names

#include<stdio.h>
#include<string.h>
void main()
{
 int i,j;
//list of names to be sort, you can customize to take input from keyboard
 char *list[]={"aj","su","punit","pushp"},*s;
printf("Input List is :")
 for(i=0;i<4;i++)
 {
  printf("\n%s ",*(list+i));
 }
 for(i=0;i<4;i++)
  {
   for(j=0;j<4-i-1;j++)
    {
     if(strcmp(*(list+j),*(list+j+1))>0)
      {
       s=*(list+j);
       *(list+j)=*(list+j+1);
       *(list+j+1)=s;
      }
    }
   }
 printf("\nSorted List is:\t");
 for(i=0;i<4;i++)
  {
   printf(" %s",*(list+i));
  }
}

output
Input List is:
aj
su
punit
pushp
Sorted List is: aj punit pushp su

Friday, May 27, 2016

C program to make a binary tree and print the tree in inorder, preorder and postorder form

#include<stdio.h>
#include<stdlib.h>
struct node
{
 int info;
 struct node *left,*right;
}*root;
//function to create a new node in tree
node *create(struct node *ptr,int n)
 {
  struct node *curr;
  curr=(node *)malloc(sizeof(node));
  if(ptr==NULL)
   {
    curr->info=n;
    curr->left=NULL;
    curr->right=NULL;
    ptr=curr;
   }
  else
   {
    if(ptr->info>n)
     ptr->left=create(ptr->left,n);
    else
     ptr->right=create(ptr->right,n);
   }
   return ptr;
 }
void preorder(struct node *ptr)
 {
  if(ptr!=NULL)
   {
    printf(" %d",ptr->info);
    preorder(ptr->left);
    preorder(ptr->right);
   }
 }
void inorder(struct node *ptr)
{
 if(ptr!=NULL)
  {
   inorder(ptr->left);
   printf(" %d",ptr->info);
   inorder(ptr->right);
  }
}
void postorder(struct node *ptr)
 {
  if(ptr!=NULL)
   {
    postorder(ptr->left);
    postorder(ptr->right);
    printf(" %d",ptr->info);
   }
 }
void main()
{
//initially root will be null as there is no node
 root=NULL;
 int n;
 int i;
 do
  {
   printf("\n\n1.insert\n\n2.preorder.\n\n3.inorder\n\n4.postorder\n\n5.exit");
   scanf("%d",&n);
   if(n==1)
    {
     printf("Enter value to insert");
     scanf("%d",&i);
     root=create(root,i);
    }
   else if(n==2)
    {
     preorder(root);
    }
   else if(n==3)
    {
     inorder(root);
    }
   else if(n==4)
    {
     postorder(root);
    }
   }while(n!=5);
 }

output

1.insert
2.preorder
3.inorder
4.postorder
5.exit
1
Enter value to insert 4

1.insert
2.preorder
3.inorder
4.postorder
5.exit
1
Enter value to insert 3
1.insert
2.preorder
3.inorder
4.postorder
5.exit
2
3 4
1.insert
2.preorder
3.inorder
4.postorder
5.exit
5

Tuesday, May 24, 2016

C program to find whether given string or sentence is armstrong or not

#include<stdio.h>
#include<string.h>
#include<math.h>
void main()
{
//str1 is input string here you can input here using your choice
 char str1[50]="hey its its hey",str2[50]="",strtemp[50];
 printf("Input string is %s",str1)
 strlwr(str1);
 int s[50],l[50],c=0;
 for(int i=0;i<=strlen(str1);i++)
  {
   if(str1[i]>=97&&str1[i]<=122)
    continue;
   else
    {
     if(c==0)
      {
       s[c]=0;
       l[c]=i-1;
       c++;
      }
     else
      {
       s[c]=l[c-1]+2;
       l[c]=i-1;
       c++;
      }
    }
  }
 c=c-1;
 for(i=c;i>=0;i--)
  {
   int temp=0;
   if((i!=c))
    {
     strtemp[temp]=' ';
     temp++;
    }
   for(int j=s[i];j<=l[i];j++)
    {
     strtemp[temp]=str1[j];
     temp++;
    }
   strtemp[temp]='\0';
   strcat(str2,strtemp);
  }
 printf("%s",str2);
 if((strcmp(str1,str2)==0))
  {
   printf("\nString are armstrong");
  }
 else
  {
   printf("\nString is not armstrong");
  }
}

output
Input string is hey it's it's hey
String are armstrong

Friday, May 20, 2016

C program to find whether given string is palindrome or not

Problem Statement:- you are given a string and you need to check whether the given string is palindrome or not, if yes you need to produce output yes
Palindrome:- if the string is equal to its reverse of string then it is called palindrome numbers

#include<stdio.h>
#include<string.h>
void check(char *t)
 {
  char *i;
//strlen() is used to calculate length of the string defined in string.h
  int l=strlen(t);
  while(*t!=NULL)
   {
    *i=*t;
    i++;
    t++;
   }
   *i='\0';
   t=t-l;
   i--;
   int count=0;
   while(*t!=NULL)
    {
     if(*t!=*i)
      {
       count++;
       break;
      }
      t++;
      i--;
    }
  if((count==0)&&(*t==NULL))
   {
    printf("String is palindrome");
   }
 else
  {
   printf("String is not palindrome");
  }
}

void main()
{
 char s[20];
 printf("Enter a string");
 scanf("%s",s);
 check(s);
}

output
Enter a string
swswwsws
String is palindrome

Wednesday, May 18, 2016

c program to sort a given string alphabetically

Problem Statement:- you are given a string and you need to sort that string into their alphabetical order. Like in "enter" the first alphabet also comes first alphabetically so it will stay there and after that the secode 'e' must come.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
 //random string as input
 char s[15]="enter";
 int i;
 char temp;
 printf("String entered is %s ",s);
//applying logic as bubble sort for string sorting
 for(i=0;i<strlen(s);i++)
  {
   for(int k=0;k<strlen(s)-i-1;k++)
    {
     //this  will use ASCII codes for characters
     if(s[k]>s[k+1])
     {
      temp=s[k];
      s[k]=s[k+1];
      s[k+1]=temp;
     }
    }
   }
 printf("\nString is %s",s);
}

output
String entered is  enter
String is eenrt

Monday, May 16, 2016

c program to print box using stars

Problem Statement:- print square box using stars in c, you can use nested loops to solve this problem.

#include<stdio.h>
void main()
 {
  int a,n=6,i,j,k;
  for(i=1;i<=n-2;i++)
   {
    if(i==1||i==(n-2))
     {
      for(j=1;j<=n;j++)
       printf("*");
      printf("\n");
     }
    else
     {
      printf("*");
      for(j=1;j<=n-2;j++)
       printf(" ");
      printf("*");
      printf("\n");
     }
   }
}

output
******
*        *
*        *
******

Saturday, May 14, 2016

C program to print stars in diamond form

#include<stdio.h>
#include<conio.h>
void main()
 {
  clrscr();
  int a,n=4,i,j,k;
  for(i=1;i<=n;i++)
   {
    for(k=1;k<=n-i;k++)
     printf(" ");
    for(j=1;j<=2*i-1;j++)
     printf("*");
     printf("\n");
   }
   for(i=n;i>=1;i--)
   {
    for(j=1;j<=n-i;j++)
     printf(" ");
    for(k=1;k<=2*i-1;k++)
     printf("*");
     printf("\n");
   }
 getch();
}

output
      *
    ***
  *****
*******
*******
  *****
   ***
     *

Thursday, May 12, 2016

C program to find factorial of a numbe using recursive technique

Problem Statement: factorial of any number is equal to multiplication of all the numbers from 1 to that number. Factorial of 5 will be equal to multiplication of numbers from 1 to 5 which will be 1*2*3*4*5 = 120
Recursive Technique: If a function calls itself then it is called recursive function.

#include<stdio.h>
//recursive function for calculating factorial
int fact(int i)
 {
  int k=1;
  if(i<=1)
   {
    return 1;
   }
  else
   {
    k=i*fact(i-1);
   }
   return k;
 }
void main()
 {
  int n,factorial=1;
  printf("Enter a no");
  scanf("%d",&n);
  factorial=fact(n);
  printf("Factorial = %d",factorial);
}

output
Enter a no 5
Factorial = 120

Friday, May 6, 2016

C program to sort an array using insertion sort

#include<stdio.h>
void main()
{
 int a[10]={1,8,6,0,6,68,44,66,45,38};
 int i,temp,k,n=10,pos;
 for(i=1;i<n;i++)
  {
   k=i-1;
   while(k>=0&&a[k]>a[k+1])
    {
     temp=a[k];
     a[k]=a[k+1];
     a[k+1]=temp;
     k--;
    }
  }
 printf("sorted array\n");
 for(i=0;i<n;i++)
  {
   printf("%d ",a[i]);
  }
}

output
sorted array
0 1 6 6 8 38 44 45 66 68

Sunday, May 1, 2016

C program to sort an array using selection sort

#include<stdio.h>
void main()
{
 int a[10]={1,8,6,0,6,68,44,66,45,38};
 int i,temp,k,n=10,pos;
 printf("Array before sort");
 for(i=0;i<n;i++){
  printf("%d ",a[i]);
 }
 for(i=0;i<n;i++)
  {
   pos=i;
   for(k=i+1;k<n;k++)
    {
     if(a[pos]>a[k])
      {
       pos=k;
      }
    }
   if(pos!=i)
    {
     temp=a[pos];
     a[pos]=a[i];
     a[i]=temp;
    }
  }
 printf("Array after sorting\n");
 for(i=0;i<n;i++)
  {
   printf("%d ",a[i]);
  }
}
output
Array before sort
1 8 6 0 6 68 44 66 45 38
Array after sort
0 1 6 6 8 38 44 45 66 68

Friday, April 29, 2016

C program to sort array using bubble sort

#include<stdio.h>
#include<string.h>
void main()
{
 int a[10]={1,8,6,0,6,68,44,66,45,38};
 int i,temp,k,n=10;
 for(i=0;i<n;i++)
  {
   for(k=0;k<n-i-1;k++)
    {
     if(a[k]>a[k+1])
      {
       temp=a[k];
       a[k]=a[k+1];
       a[k+1]=temp;
      }
    }
  }
 for(i=0;i<n;i++)
  {
   printf("%d ",a[i]);
  }
}

output
0 1 6 6 8 38 44 45 66 68

Wednesday, April 27, 2016

c program to print matrix after rotation

#include<stdio.h>
void main()
{
 int arr[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
 int arr_new[4][4];
 int i,j;
 for(i=0;i<4;i++)
  {
   for(j=0;j<4;j++)
    {
     printf(" %d ",arr[i][j]);
    }
   printf("\n");
  }
 for(i=0;i<4;i++)
  {
   for(j=0;j<4;j++)
    {
     arr_new[j][4-i-1]=arr[i][j];
    }
  }
 for(i=0;i<4;i++)
  {
   for(j=0;j<4;j++)
    {
     printf(" %d ",arr_new[i][j]);
    }
   printf("\n");
  }
}
output

Sunday, April 24, 2016

c program to count number of alphabets, digits and spaces in a given file

#include<stdio.h>
//included process.h for using the function exit(0)
#include<process.h>
void main()
{
 FILE *fp;
 char c;
//a will stand for number of alphabets
//d for digits
//s for spaces
 int a=0,d=0,s=0;
//this is the name of file to read
 fp=fopen("emp1.txt","r");
 if(fp==NULL)
  {
   printf("Not open");
//it will exit the program
   exit(0);
  }
 while((c=fgetc(fp))!=EOF)
  {
//we are recognizing alphabets based on thier ascii codes
   if((c>=65&&c<=90)||(c>=97&&c<=122))
    a++;
   else if(c>=48&&c<=58)
    d++;
   else
    s++;
  }
 printf("alphabets=%d,digits= %d ,spaces=%d",a,d,s);
}

output
alphabets=2, digits =5, spaces=8

Monday, April 11, 2016

C program to find fabonacci series upto n using recursive technique

#include<stdio.h>
int fab(int i)
 {
  if(i==0)
   {
    return 0;
   }
  else if(i==1)
   {
    return 1;
   }
  else
   return fab(i-1)+fab(i-2);
 }
void main()
 {
  int n;
  printf("Enter no of terms");
  scanf("%d",&n);
  int i;
  for(i=0;i<n;i++)
   {
    printf(" %d ",fab(i));
   }
 
 }
output
Enter no of terms 6
0 1 1 2 3 5