#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,k,a[10],c[10],n,l; cout<<"Enter the no. of elements\t"; cin>>n; cout<<"\nEnter the sorted elments for optimal merge pattern"; for(i=0;i<n;i++) { cout<<"\t"; cin>>a[i]; } i=0;k=0; c[k]=a[i]+a[i+1]; i=2; while(i<n) { k++; if((c[k-1]+a[i])<=(a[i]+a[i+1])) { c[k]=c[k-1]+a[i]; } else { c[k]=a[i]+a[i+1]; i=i+2; while(i<n) { k++; if((c[k-1]+a[i])<=(c[k-2]+a[i])) { c[k]=c[k-1]+a[i]; } else { c[k]=c[k-2]+a[i]; }i++; } }i++; } k++; c[k]=c[k-1]+c[k-2]; cout<<"\n\nThe optimal sum are as follows......\n\n"; for(k=0;k<n-1;k++) { cout<<c[k]<<"\t"; } l=0; for(k=0;k<n-1;k++) { l=l+c[k]; } cout<<"\n\n The external path length is ......"<<l; getch(); }
#include<stdio.h> main() { int i,j,k,n; char c; printf("enter the # of rows of graphical output\n"); scanf("%d",&n); printf("enter the character you want to print\n"); scanf("%c",&c); for(i=1;i<=n;i++) { for (k=1;k<=(n-i);k++) { printf(" "); } for(j=0;j<i;j++) { printf("%c",c); printf(" "); } for(k=1;k<=(n-i-1);k++) { printf(" "); } printf("\n"); } getch(); }
Here is the code to do it: #include<stdio.h> main() { int n, c, k, space = 1; //Here we ask for the number of rows would be : printf("Enter number of rows\n"); scanf("%d",&n); space = n - 1; //This is the first half of the diamond for ( k = 1 ; k <= n ; k++ ) { for ( c = 1 ; c <= space ; c++ ) printf(" "); space--; for ( c = 1 ; c <= 2*k-1 ; c++) printf("*"); printf("\n"); } space = 1; //Here is the second half of the diamond for ( k = 1 ; k <= n - 1 ; k++ ) { for ( c = 1 ; c <= space; c++) printf(" "); space++; for ( c = 1 ; c <= 2*(n-k)-1 ; c++ ) printf("*"); printf("\n"); } return 0; } Hope that helped :)
The following code for example is a solution (you could do it with less variables, but this is more readable):int GCD(int a, int b){int n, k, c;n = (a>b)?a:b;k = (a>b)?b:a;while (k){c = n%k;n=k;k=c;}return n;}
#include<stdio.h> void main() { int a[5],b[5]; int c[10]; int i,j,temp,k; printf("\n enter the elements of array A:=\n"); for(i=0;i<5;i++) scanf("%d",&a[i]); printf("\n enter the elements of array B:=\n"); for(i=0;i<5;i++) scanf("%d",&b[i]); for(i=1;i<5;i++) { for(j=0;j<5-i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } for(j=0;j<5-i;j++) { if(b[j]>b[j+1]) { temp=b[j]; b[j]=b[j+1]; b[j+1]=temp; } } } printf("\n the elements of the array A:=\n"); for(i=0;i<5;i++) printf("%d\t",a[i]); printf("\n the elements of the array B:=\n"); for(i=0;i<5;i++) printf("%d\t",b[i]); i=0,j=0,k=0; while(i<5&&j<5) { if(a[i]>b[j]) c[k++]=b[j++]; if(a[i]<b[j]) c[k++]=a[i++]; } if(i<5&&j==5) while(i<5) c[k++]=a[i++]; if(i==5&&j<5) while(j<5) c[k++]=b[j++]; printf("\n the elements of the sorted merged array C:=\n"); for(i=0;i<10;i++) printf("%d\t",c[i]); }
8.998 X 10^9 N*m^2/C^2
for (n=1; n<1000; ++n) { for (sum=0, k=1; k<=n/2; ++k) if (n%k==0) sum += k; if (sum==n) printf ("%d\n", n); }
knockout
#include<stdio.h> #include<conio.h> void main() { int i,j,k,a[10],n; clrscr(); scanf("%d",&n); for(i=0;i<3;i++) { n=n%10; j=0; a[j]=n; j++; } for(j=0;j>3;j++) { a[k]=0; a[k]=a[k]+a[j]; avg=(a[k]/3); printf("the average value of digit will be %d",avg); } getch(); } }
#include <stdio.h> #include <conio.h> void main(){ int a[10],b[10],c[10],i,j,m,n,tmp,k; printf("\nEnter the size of the 1st array:"); scanf("%d",&m); printf("\nEnter the size of the 2nd array:"); scanf("%d",&n); printf("\nEnter the 1st array values:"); for(i=0;i<m;i++){ scanf("%d",&a[i]); } printf("\nEnter the 2nd array values:"); for(i=0;i<n;i++){ scanf("%d",&b[i]); } for(i=0;i<m;i++) { for(j=0;j<m-i;j++) { if(a[j]>a[j+1]) { tmp=a[j]; a[j]=a[j+1]; a[j+1]=tmp; } } } printf("\n\n Array in the ascending order is - \n"); for(i=0;i<m;i++) { printf("\t %d",a[i]); } for(i=0;i<n;i++) { for(j=0;j<n-i;j++) { if(b[j]>b[j+1]) { tmp=b[j]; b[j]=b[j+1]; b[j+1]=tmp; } } } printf("\n\n Array in the ascending order is - \n"); for(i=0;i<n;i++) { printf("\t %d",b[i]); } i=j=k=0; while(i<n&&j<m) { if(a[i]<b[j]) c[k++]=a[i++]; else if(a[i]>b[j]) c[k++]=b[j++]; else { c[k++]=b[j++]; i++; j++; } } if(i<n) { int t; for(t=0;t<n;t++) c[k++]=a[i++]; } if(j<m) { int t; for(t=0;t<m;t++) { c[k++]=b[j++]; } } printf("\nFinally sorted join array is:"); for(k=0;k<(m+n);k++) printf("\t\n %d ",c[k]); getch(); }
K. N. Raj died on 2010-02-10.
K. N. Rao was born on 1931-10-12.