Pages

Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Write a program that will determine the prime factors of a specified factorial

Tuesday, 1 July 2014


#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{

int number,div, num1;
int arr[20], i=0; 
int result=0, large_no;
cout<<"Enter a number to know its prime factor: "; 
cin>>number;
num1= number;
cout<<"\nThe prime factors of "<<number<<" are: \n\n";

div = 2;

while(number!=0)
{
if(number%div!=0)
div = div + 1;

else {
number = number / div;
cout<<div<<" ";
arr[i]= div;
i++;

if(number==1)
break;
}
}
large_no= arr[i-1];


cout<<endl;
cout<<"In this prime factorss.. "<<endl;
for(int j=2;j<=large_no;j++)
{
for(int k=0;k<=large_no;k++)
{

if(j==arr[k])
{
result=result+1;
}


cout<<j<<" comes "<< result<<" time "<<endl;
result=0;
}


system("pause");
}

Write a program in C language to find nCr and nPr

Sunday, 26 January 2014

#include <iostream>
#include <conio.h>
using namespace std;

int fict(int n)
{
    int i, sum=1;
    for(i=1; i<=n; i++)
    {
         sum = sum*i;
    }
    return sum;
}

main()
{
      char status, find;
      int n, r, ans;
   
      while(status != 'n')
      {
             cout << "\n1: nCr\n2: nPr\n";
             find = getch();
             cout << "Enter value of n: ";
             cin >> n;
             cout << "Enter value of r: ";
             cin >> r;
           
             if(find == '1')
             {
                     ans = fict(n) /( fict(r) * fict(n-r));
                     cout << "\nAnswer of "<<n<<"C"<<r<<" is: "<<ans<<endl;
             }
             else if(find == '2')
             {
                     ans = fict(n) / fict(n-r);
                     cout << "\nAnswer of "<<n<<"P"<<r<<" is: "<<ans<<endl;
             }
             else
             {
                     cout << "Enter valid input."<<endl;
             }
           
           
             cout << "You like to calculate again? n/y.";
             status = getch();
      }
       
      system("pause");  
}

Write a program in c language to concatenate two strings.

Monday, 15 July 2013

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

void stringconcatenation(char[],char[]);

 main()
{

    char mrr1[100],mrr2[100];
    printf("Enter the first string: ");
   gets(mrr1);
    printf("Enter the second string: ");
    gets(mrr2);
    stringconcatenation(mrr1,mrr2);
   printf("The String after concatenation: %s",mrr1);
    getch();
}
void stringconcatenation(char mrr1[],char mrr2[])
{
    int l=0,k=0;
    while(mrr1[l]!='\0')
    {
         l++;
    }
  while(mrr2[k]!='\0')
    {
         mrr1[l] = mrr2[k];
         l++;
         k++;
    }
 mrr1[l] ='\0';
}

Write a program in c language to get numbers from user and print average.

Wednesday, 10 July 2013

# include<stdio.h>
#include<conio.h>
#define LIMIT 130
int main(){
float marks[LIMIT], average, sum = 0.0;
int i=0;
do {
printf("\nEnter the mark of the student(-1 to finish)");
scanf("%f", &marks[i]);
if (marks[i] >= 0)
sum = sum + marks[i];
}
while(marks[i++] >= 0);
average = sum/(i-1);
printf ("\nThe average is %.2f\n", average);
getch();
}

Write a program in c language to find first and second largest number from array.

Monday, 27 May 2013

#include <stdio.h>
#include <conio.h>
int main()
{
    int i, a[10], loc1=0, loc2=0, max1=0, max2=0;
    for(i=1; i<=5; i++)
    {
             printf("Enter number: ");
             scanf("%d", &a[i]);
    }
    for(i=1; i<=5; i++)
    {
             if(max1<a[i])
             {
                    max2=max1;
                    max1=a[i];
                    loc2=loc1;
                    loc1=i;
             }
             if(max2<a[i] && a[i]<max1)
             {
                     max2=a[i];
                     loc2=i;
             }
    }
    printf("First largest is %d  at %d and Second largest is %d at %d",max1, loc1, max2, loc2);
    getch();
}


Write a program in c language to insert a new element in array.

#include <stdio.h>
#include <conio.h>
int main()
{
    int i, a[10], loc, item;
    for(i=0; i<=9; i++)
    {
             printf("Enter number %d: ", i);
             scanf("%d", &a[i]);
    }
   
    printf("Enter location where you want to enter number: ");
    scanf("%d", &loc);
    printf("Enter number: ");
    scanf("%d", &item);
    for(i=10; i>loc; i--)
    {
             a[i]=a[i-1];
    }
    a[loc]=item;
    for(i=0; i<=10; i++)
    {
             printf("%d ", a[i]);
            // scanf("%d", &a[i]);
    }
    getch();
}

Write A Program To Find A Maximum Number In Array

Wednesday, 8 May 2013


#include <stdio.h>
#include <conio.h>
int main()
{
  int array[100], maximum, s, d, l = 1;

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

  printf("Enter %d integers\n", s);

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

  maximum = array[0];

  for (d = 1; d < s; d++)
  {
    if (array[d] > maximum)
    {
       maximum  = array[d];
       l = d+1;
    }
  }

  printf("Maximum element is present at location %d and it's value is %d.\n", l, maximum);
 getch();
}

Write A Program Search In Given Number In Array Using Binary Search

#include <stdio.h>
#include <conio.h>
int main()
{
   int c, f, l, midl, n, s, array[100];

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

   printf("Enter %d integers: ", n);

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

   printf("Enter value to find: ");
   scanf("%d",&s);

   f = 0;
   l = n - 1;
   midl = (f+l)/2;

   while( f <= l )
   {
      if ( array[midl] < s )
         f = midl + 1;
      else if ( array[midl] == s )
      {
         printf("%d found at location %d.", s, midl+1);
         break;
      }
      else
         l = midl - 1;

      midl = (f + l)/2;
   }
   if ( f > l )
      printf("Not found! %d is not present in the list.", s);

   getch();
}

Write A Program Search A Given Number In Array Using Linear Search

#include <stdio.h>
#include <conio.h>
int main()
{
   int array[100], s, d, number;

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

   printf("Enter %d numbers: ", number);

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

   printf("Enter the number to search: ");
   scanf("%d",&s);

   for ( d = 0 ; d < number ; d++ )
   {
      if ( array[d] == s )  
      {
         printf("%d is present at location %d.", s, d+1);
break;
      }
   }
   if ( d == number )
      printf("%d is not present in array.", s);  

   getch();
}

Write a program in C language for bubble sorting in ascending order.

Tuesday, 7 May 2013

#include <stdio.h>
#include <conio.h>
int main()
{
  int s,temp,i,j,array[20];

  printf("Enter total numbers of elements: ");
  scanf("%d",&s);

  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
  {
      scanf("%d",&array[i]);
  }
     
  for(i=s-2;i>=0;i--)
  {
      for(j=0;j<=i;j++)
      {
           if(array[j]>array[j+1])
           {
              temp=array[j];
              array[j]=array[j+1];
              array[j+1]=temp;
           }
      }
  }

  printf("After sorting: ");
 
  for(i=0;i<s;i++)
  {
      printf(" %d",array[i]);
  }

  getch();
}

Write a program to get 9 numbers from user in two dimensional array and print it as a matrix.

Saturday, 4 May 2013


#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][3];
int i,j;
printf("value in a case\n");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
      printf("Enter Value of row %d and column %d: ", i, j);          
      scanf("%d", &a[i][j]);
}
}
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
getch();
}

Write a program to get numbers from user and sort is print on screen

#include <stdio.h>
#include <conio.h>
int main()
{
    int a[10], i, temp;
    printf("Enter Numbers to sort: ");
    for(i=0; i<=9; i++)
    {
         scanf("%d", &a[i]);   
    }
    for(i=0; i<=9; i++)
    {
             if(a[i+1]<a[i])
             {
                    temp=a[i];
                    a[i]=a[i+1];
                    a[i+1]=temp;
                    i=-1;
             }
    }
   

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

Get Element in 2 D Array and print orignal metrix and its transpose

Friday, 19 April 2013

#include<stdio.h>
#include<conio.h>
int main()
{
int a[4][4],c,r;
for(r=1;r<4;r++)
{
for(c=1;c<4;c++)
{
printf("Enter %d Row adn %d column: ",r,c);
scanf("%d",&a[r][c]);
}// inner loop clossed
} // outer loop clossed
printf("Before Transpose\n\n");
for(r=1;r<4;r++)
{
for(c=1;c<4;c++)
{
printf("%d",a[r][c]);
printf("\t");
} // inner loop clossed
printf("\n");
} // outer loop clossed
printf("\n\nAfter Transpose\n\n");
for(r=1;r<4;r++)
{
for(c=1;c<4;c++)
{
printf("%d",a[c][r]);
printf("\t");
} // inner loop clossed
printf("\n");
} // outer loop clossed

getch();
}

Output
Before transpose: 
2            3            4
1            8            0
3            6            9

After transpose:
2            1            3
3            8            6
4            0            9


Find Size of (unsized) dynamic Array and print element of Array



#include <stdio.h>
#include <conio.h>
main()
{
      int i, j;
     char str1[][2]={
     '1', 'a',
     '2', 'b',
     '3', 'c',
     '4', 'd',
     '5', 'e',
     '6', 'f'};
     printf("Size of Array is: %d \n\n", sizeof(str1));
     printf("Element of Array is: \n");
     for(i=0; i<=5; i++)
     {
     for(j=0; j<=1; j++)
     {
         printf("%c \t", str1[i][j]);  
     }
     printf("\n");
     }
 
     getch();
}

Copy Data/String of one array to another array




#include <stdio.h>
#include <conio.h>
int main()
{
    int i;
      char str1[]="This is Exersize 1.";
      char str2[20], ch, ch2;
      i=0;
      while(ch!='.')
      {
                    str2[i]=ch=str1[i];
                    i++;
      }
      i=0;
      while(ch2!='.')
      {
                     ch2=str2[i];
                     printf("%c", ch2);
                     i++  ;
      }
 
      getch();
}

Copy one string to another using strcpy() function



#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
      char str1[]="This is Exersize 1.";
      char str2[20];
      strcpy(str2, str1);
      printf("%s", str2);
      getch();
}

Get order of matrix and element from user and print matrix of given numbers and given order



    # include <stdio.h>
    # include <conio.h>
    int main()
    {
      int mat[10][10] ;
      int i, j, row, col ;

      printf("Enter the order of the matrix : ") ;
      scanf("%d %d", &row, &col) ;
      printf("\nEnter the elements of the matrix : \n\n") ;
      for(i = 0 ; i < row ; i++)
        for(j = 0 ; j < col ; j++)
          scanf("%d", &mat[i][j]) ;
          printf("\nThe transpose matrix is : \n\n") ;
      for(i = 0 ; i < col ; i++)
      {
        for(j = 0 ; j < row ; j++)
        {
          printf("%d \t", mat[j][i]) ;
        }
        printf("\n") ;
      }
      getch() ;
    }

Write a program to print histograme based on number using Array


#include<stdio.h>
#include<conio.h>
int main()
{
    int i,j,sum=0;
    int a[10]={1,22,3,4,5,6,7,8,9,1};
    printf("%s %13s %17s\n","element","value","histrogram");
    for(i=0;i<10;i++){
                          printf("%d %19d ",i,a[i]);  
                      
    for(j=1;j<=a[i];j++){
                      printf("*");
                      }
                      printf("\n");
                      }
                              getch();
                      }

Write a program to print histograme based on number using Array


#include<stdio.h>
#include<conio.h>
int main()
{
    int i,j,sum=0;
    int a[10]={1,22,3,4,5,6,7,8,9,1};
    printf("%s %13s %17s\n","element","value","histrogram");
    for(i=0;i<10;i++){
                          printf("%d %19d ",i,a[i]);  
                      
    for(j=1;j<=a[i];j++){
                      printf("*");
                      }
                      printf("\n");
                      }
                              getch();
                      }


 

Search Box

Most Reading

Contact Form

Name

Email *

Message *