C Program to Insert an Element in an Array

Here is the simple C program to insert an element in a one dimensional array. The array should be in ascending order. #include<stdio.h> int main() { int a[20],n,x,i,pos=0; printf(“Enter size of array:”); scanf(“%d”,&n); printf(“Enter the array in ascending order:\n”); for(i=0;i<n;++i) scanf(“%d”,&a[i]); printf(“\nEnter element to insert:”); scanf(“%d”,&x); for(i=0;i<n;++i) if(a[i]<=x&&x<a[i+1]) { pos=i+1; break; } for(i=n+1;i>pos;–i) a[i]=a[i-1]; a[pos]=x; …

C Program to Insert an Element in an Array Read More »

C Program to Find Sum of Elements Above and Below Main Diagonal of Matrix

Here is the simple C program to find sum of elements above and below main diagonal of matrix. The matrix should be square matrix. #include<stdio.h> int main() { int i,j,m,n,d1=0,d2=0,a[5][5]; printf(“How many rows and columns:”); scanf(“%d%d”,&m,&n); printf(“Enter matrix elements:\n”); for(i=0;i<m;++i) for(j=0;j<n;++j) { scanf(“%d”,&a[i][j]); if(j>i) d1+=a[i][j]; else if(i>j) d2+=a[i][j]; } printf(\n”Sum of elements above the diagonal=%d\n”,d1); …

C Program to Find Sum of Elements Above and Below Main Diagonal of Matrix Read More »

C++ program to swap two numbers using macros

#include<iostream.h> #include<conio.h> #define SWAP(a,b) {int temp; temp=a; a=b; b=temp;} void main() { clrscr(); int x,y; cout<<“Enter two numbers:”; cin>>x>>y; cout<<“x=”<<x<<” y=”<<y; SWAP(x,y); cout<<“nx=”<<x<<” y=”<<y; getch(); }

C++ program to enter a number and print it into words

#include<iostream.h> #include<conio.h> void once(int a) { switch(a) { case 1: cout<<“One”; break; case 2: cout<<“Two”; break; case 3: cout<<“Three”; break; case 4: cout<<“Four”; break; case 5: cout<<“Five”; break; case 6: cout<<“Six”; break; case 7: cout<<“Seven”; break; case 8: cout<<“Eight”; break; case 9: cout<<“Nine”; break; } } int tens(int a,int b) { int flag=0; switch(a) { …

C++ program to enter a number and print it into words Read More »

C program to find the sum of series 1/2+4/5+7/8+……

#include<stdio.h> #include<conio.h> void main() { int i,n; clrscr(); float sum=0,x,a=1; printf(“1/2+4/5+7/8+……”); printf(“nnHow many terms(ex: 1,2,3…n)?”); scanf(“%d”,&n); for(i=0;i<n;++i) { x=a/(a+1); sum+=x; a+=3; } printf(“nSum=%f”,sum); getch(); }

C program to swap two numbers without using temporary variable

#include<stdio.h> #include<conio.h> void main() { int x,y; clrscr(); printf(“Enter value of X: “); scanf(“%d”,&x); printf(“Enter value of y: “); scanf(“%d”,&y); if(x>y) { y=x-y; x=x-y; y=x+y; } else if(y>x) { x=y-x; y=y-x; x=y+x; } printf(“nx=%d”,x); printf(“ny=%d”,y); getch(); }