Posts

Showing posts with the label C

C Program to Insert an Element into Array

Previously we learned to insert elements into empty arrays in the order. But how can we insert a particular element into a desired position of an array? For that we need to initialize a variable 'key' which we may use to indicate the position of PC in program. Let's have a look into the program. //Program #include <stdio.h> void main() { int array[10]; int i, j, n, m, temp, key, pos; printf( "Enter how many elements \n "); scanf( "%d" , &n); printf( "Enter the elements \n "); for (i = 0; i < n; i++) { scanf( "%d" , &array[i]); } printf( "Input array elements are \n "); for (i = 0; i < n; i++) { printf( "%d\n ", array[i]); } for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } printf( "Sorted list is \n "); for (i = 0; i ...

C Program to Delete the Specified Integer from an Array

This C Program deletes specified integer from an array. It accepts an array and the number to delete. #include <stdio.h>   void main() { int vectorx[10]; int i, n, pos, element, found = 0;   printf( "Enter how many elements\n "); scanf( "%d" , &n); printf( "Enter the elements\n "); for (i = 0; i < n; i++) { scanf( "%d" , &vectorx[i]); } printf( "Input array elements are\n "); for (i = 0; i < n; i++) { printf( "%d\n ", vectorx[i]); } printf( "Enter the element to be deleted\n "); scanf( "%d" , &element); for (i = 0; i < n; i++) { if (vectorx[i] == element) { found = 1; pos = i; break; } } if (found == 1) { for (i = pos; i < n - 1; i++) { vectorx[i] = vectorx[i + 1]; } printf( "The resultant vector is \n "); for (i = 0; i ...

C Program to Seperate Even & Odd Elements of an Array

In this program we find the odd and even elements of an array and insert them into two different arrays. Here the logic of the program to find whether a number is odd or even is used. //Program #include <stdio.h> void main() { long int ARR[10], OAR[10], EAR[10]; int i, j = 0, k = 0, n;   printf( "Enter the size of array AR \n "); scanf( "%d" , &n); printf( "Enter the elements of the array \n "); for (i = 0; i < n; i++) { scanf( "% ld " , &ARR[i]); fflush(stdin); } /* Copy odd and even elements into their respective arrays */ for (i = 0; i < n; i++) { if (ARR[i] % 2 == 0) { EAR[j] = ARR[i]; j++; } else { OAR[k] = ARR[i]; k++; } } printf( "The elements of OAR are \n "); for (i = 0; i < j; i++) { printf( "% ld \n ", OAR[i]); } printf( "The elements of EAR are \n "...

C Program to Find The Largest Number in an Array

This program makes the concept of array more clear. As arrays are continuous memory locations, we can access them easily by incrementing the number (variable) in square bracket. This method is used in this program. Here using loop each and every element is comparing it with the other elements. //program #include <stdio .h>   int main ( ) { int array [ 50 ] , size , i , largest ; printf ( " \n Enter the size of the array: " ) ; scanf ( "%d" , & size ) ; printf ( " \n Enter %d elements of the array: " , size ) ; for ( i = 0 ; i < size ; i ++ ) scanf ( "%d" , & array [ i ] ) ; largest = array [ 0 ] ; for ( i = 1 ; i < size ; i ++ ) { if ( largest < array [ i ] ) largest = array [ i ] ; } printf ( " \n largest element present in the given array is : %d" , largest ) ; return 0 ; }

C Program to Calculate The Average of Array Elements

It is as simple as the previous program in which we calculated the sum of array elements. //Program #include <stdio.h> void main() {     int n, i;        float num[100], sum = 0.0, average;     printf("Enter the numbers of elements: ");     scanf("%d", &n);     while (n > 100 || n <= 0)     {         printf("Error! number should in range of (1 to 100).\n");         printf("Enter the number again: ");         scanf("%d", &n);     }     for(i = 0; i < n; ++i)     {         printf("%d. Enter number: ", i+1);         scanf("%f", &num[i]);         sum += num[i];     } ...

C Program to Calculate Sum of Array Elements

Array is a mile stone of C Program which we may use almost everywhere. Here is a basic program to understand array, memory locations and structure of array etc. What is an array? An array is a group of continuous memory locations which we can access by the address. An array of a particular datatype can only store values of that data type(often it convert and use the given data). Array initialization The syntax of array initialization in C is : datatype array_name[size]; Algorithm Here the array named 'array' of type 'int' is used to store different numbers and adding them by separately accessing the array elements using for loop. //Program //Program #include <stdio.h< voide main() {     int n, i;        float num[100], sum = 0.0, average;     printf("Enter the numbers of elements: ");     scanf("%d", &n);     while (n > 100 || n <= 0)     { ...

C Program to Disply Armstrong Number Series

What is an Armstrong number? An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. This definition depends on the base b of the number system used, e.g., b  = 10 for the decimal system or b  = 2 for the binary system. (Read more about Armstrong number) For generating Armstrong number series we use the following algorithm. Algorithm A positive integer is called an Armstrong number of order n if abcd... = a n + b n + c n + d n + ...   In case of 3 digit Armstrong numbers the sum of cubes of the digits of the number will be equal to the actual number. Thus we can use a conditional statement to split a number, multiply, add and check with original number.   #include < stdio .h > #include < math .h > int main() { int low, high, i, temp1, temp2, remainder, n = 0, result = 0; printf("Enter two numbers(intervals): "); scanf("%d %d", &low, &high); pri...

C Program to Display Prime Number series

This is a simple program. Here the technique used is to generate prime numbers from one lower limit to one upper limit using loop. For more details refer our previous program : C Program to Check Whether a Number is Prime or Not. //Program #include < stdio.h > int main() {     int low, high, i, flag;     printf("Enter two numbers(intervals): ");     scanf("%d %d", &low, &high);     printf("Prime numbers between %d and %d are: ", low, high);     while (low < high)     {         flag = 0;         for(i = 2; i <= low/2; ++i)         {             if(low % i == 0)             {             ...

C Program to Check Whether a Number is Prime or Not

What is an Armstrong number? An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. This definition depends on the base b of the number system used, e.g., b  = 10 for the decimal system or b  = 2 for the binary system. (Read more about Armstrong number) For generating Armstrong number series we use the following algorithm. Algorithm A positive integer is called an Armstrong number of order n if abcd... = a n + b n + c n + d n + ...   In case of 3 digit Armstrong numbers the sum of cubes of the digits of the number will be equal to the actual number. Thus we can use a conditional statement to split a number, multiply, add and check with original number.   #include < stdio .h > #include < math .h > int main() { int low, high, i, temp1, temp2, remainder, n = 0, result = 0; printf("Enter two numbers(intervals): "); scanf("%d %d", &low, &high); pri...

C Program to check whether a number is palindrome or not

In this program we use if else if ladder mechanism. If an integer is equal to the reverse of it, then it is a palindrome.  Algorithm   In this program we divide the given number by 10 and take the reminder(This could be done by using % operator).  Arrange the reminders in order to get the reverse of original number. Check whether original number is equal to  the reverse of it.   //Program   #include int main () { int n , reversedInteger = 0 , remainder , originalInteger ; printf ( "Enter an integer: " ); scanf ( "%d" , & n ); originalInteger = n ; // reversed integer is stored in variable while ( n != 0 ) { remainder = n % 10 ; reversedInteger = reversedInteger * 10 + remainder ; n /= 10 ; } // palindrome if orignalInteger and reversedInteger are equal if ( originalInteger == reversedInteger ) printf ( "%d is a palindrome." , ...

C Program to find the power of a number

It is easy to find the power of any number in calculator. For doing the same in a C program needs  an algorithm.    Algorithm   Identify the base number -> Identify the exponent -> find power. This can be illustrated with an example of 2 3 .  Here Base is '2'; exponent is '3' and the power is 2*2*2 = 8.   #include int main () { int base , exponent ; long result = 1 ; printf ( "Enter a base number: " ); scanf ( "%d" , & base ); printf ( "Enter an exponent: " ); scanf ( "%d" , & exponent ); while ( exponent != 0 ) { result *= base ; -- exponent ; } printf ( "Answer = %ld" , result ); return 0 ; }

Number Magic2 Using For loop #c

The Output look like this 1 2 3 4 5 6 7 8 9 10 //Lets start the program #include<stdio.h> #include<conio.h> void main() { int j,k,b=31,c; clrscr(); for(c=9;c>=1;c=c-1,b=b+1)    {printf("\n");     for(j=1;j<=b;j++)     printf(" ");     for(k=1;k<=c;k++)     printf("%d ",c);     }     getch();     } Lets Start Next Program. The Out Will look like this.                                                    1                            ...

Number Magics using For loop #c

The program to get output like this 1 2 2 3 3 3 4 4 4 4 //It's a simple program.Here we print only 9 rows. #include<stdio.h> #include<conio.h> void main() { int i,n=9,k; //Now we use this for loop to check the number of rows. for(i=1;i<=n,i++) //Here we use this for loop to print like 1 or 2 2 or 3 3 3.      { for (k=1;k<=i;k++)               printf("%d",i);       } getch(); }

Fibonacci Series using For loop #c

The Fibonacci Series is like  0,1,1,2,3,5,8,13------------ or 0 ,  0+1, 1+1 ,2+1, 3+2, 5+3, 8+5 // A program to display Fibonacci Series using For loop in c. #include<stdio.h> #include<conio.h> void main() { int n,i,x=1,y=0,z=0; clrscr(); printf("Enter the limit\n"); scanf("%d",&n); for (i=1;i<=n;i++)     { printf("%d,",z);       z=x+y;       x=y;       y=z;       }       getch();       }

Factorial Of a Number Using For loop

//Factorial of a Number Using For loop in C. #include<stdio.h> #include<conio.h> void main() { int n,i,f; clrscr(); printf("Enter a Number\n"); scanf("%d",&n); for(i=1,f=1;i<=n;i++)    {f=f*i;}    printf("The factorial is %d",f);    getch();    } //Multiplication table of a user defined number using for loop in C. #include<stdio.h> #include<conio.h> void main() { int i,n,p; clrscr(); printf("Enter a number\n"); scanf("%d",&n); for (i=1;i<=10;i++)     {p=i*n;      printf("%d X %d = %d\n",n,i,p);      }      getch();      } //Multiplication table of All numbers up to 10 using for loop in C. #include<stdio.h> #include<conio.h> void main() { int n,i,p,j; clrscr(); for (i=1;i<=10;i++)     {printf("The multiplication table of %d\n",i);     p...

For Loop

Image
 Now i am talking about "for loop". For loop is similar to while loop. You can use while loop instead of for loop and also while loop instead of while loop. The Synatax of for loop is  for(initiation;condition;statement)

To print a given number is odd or even. #c

//The Program by Tittu Varghese #include<stdio.h> #include<conio.h> void main() { int x,a; clrscr(); printf("Enter a number\n"); scanf("%d%",&x); a = x%2; if (a==0)     printf("The given number %d is even.",x); else     printf("The given number %d is odd.",x); getch(); }

Sum and average of set of given numbers using while loop #c

//The Program By Tittu Varghese // To display the sum and average of set of given numbers. #include<stdio.h> #include<math.h> #include<conio.h> void main() { int x,i=1,s=0,n; float a; clrscr(); printf("Enter the limit\n"); scanf("%d",&n); printf("Enter %d numbers\n",n); while(i<=n)  {   scanf("%d",&x);   s = s+x;   i=i++;  } printf("sum of given %d given number is %d and\n",n,s); a =s/n; printf("The average is %f\n",a); getch(); } //getch(); is used for hold the out put screen. //clrscr(); is used for clear the screen. //***it must use after variable declaration.

To display sum of first n numbers using while loop #c

//The Program By Tittu Varghese // To display the sum of first n numbers. #include<stdio.h> #include<math.h> #include<conio.h> void main() {int i,n,s; i = 0; s = 0; printf("Enter the value of n to find sum of n numbers\n"); scanf("%d",&n); while (i <= n) {s = s+i; i = i+1; } printf("The sum of first %d numbers is %d\n",n,s); getch(); } //The Program By Tittu Varghese // To display the sum of first n even numbers. #include<stdio.h> #include<math.h> #include<conio.h> void main() {int i,n,s; i = 2; s = 0; printf("Enter the value of n to find sum of n even numbers\n"); scanf("%d",&n); while (i <= n+n) {s = s+i; i = i+2; } printf("The sum of first %d even numbers is %3d\n",n,s); getch(); } //The Program By Tittu Varghese // To display the sum of first n odd numbers. #include<stdio.h> #include<math.h> #include<conio.h...

To display n numbers using while loop. #c

//The Program By Tittu Varghese //To display n numbers. #include<stdio.h> #include<conio.h> void main() { int a,b,i,n; i = 1; printf("How many numbers you need to display ?"); scanf("%d",&n); while (i <= n) {b = i;  printf("%d,",b);  i = i+1;   }  getch(); } //The Program By Tittu Varghese //To display n even numbers.     #include<stdio.h> #include<conio.h> void main() { int a,b,i,n; i = 2; printf("How many even numbers you need to display ?"); scanf("%d",&n); while (i <= (n+n)) {b = i;  printf("%d,",b);  i = i+2;   }  getch(); } //The Program By Tittu Varghese //To display n odd numbers.     #include<stdio.h> #include<conio.h> void main() { int a,b,i,n; i = 1; printf("How many even numbers you need to display ?"); scanf("%d",&n); while (i <= ((n+n)-1)) {b = i;  printf("%d,",b); ...