Posts

Showing posts with the label While Loop

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 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 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 ; }

Largest value of set of given numbers using while loop #c

//The Program Written by Tittu Varghese //To find largest of set of given numbers using while loop. #include<stdio.h> #include<math.h> #include<conio.h> void main() { int x,l=-32768,i=1,n; clrscr(); printf("Enter the limit\n"); scanf("%d",&n); x=l; printf("Then enter %d numbers\n",n); while(i<=n)  {   scanf("%d",&x);   if (x>l)     l=x;     i=i++;  } printf("The largest of given %d numbers is %d",n,l); getch(); } //Here we assign value for "l" as -32768 because it is the smallest value can assign to int (intiger). //*We can use if,else etc. with while,for etc. loops.   //The Program Written by Tittu Varghese //To find largest and smallest of set of given numbers using while loop. #include<stdio.h> #include<math.h> #include<conio.h> void main() { int x,l=-32768,s=32767,i=1,n; clrscr(); printf("Enter the limit\n"); scanf(...

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); ...