Computer Science » C Program » Insert Element in An Array Program : [php]#include<stdio.h> int main() { int arr[30], element, num, i, location; printf("nEnter no of elements : "); scanf("%d", &num); for (i = 0; i < num; i++) { scanf("%d", &arr[i]); } printf("nEnter the element to be inserted : "); scanf("%d", &element); printf("nEnter the location : "); scanf("%d", &location); //Create space at the specified location for (i = num; i >= location; i--) { arr[i] = arr[i - 1]; } num++; arr[location - 1] = element; //Print out the result of insertion for (i = 0; i < num; i++) printf("n %d", arr[i]); return (0); }[/php] Output : [php]Enter no of elements : 5 10 20 30 40 50 Enter the element to be inserted : 60 Enter the location : 3 10 20 60 30 40 50[/php]
Computer Science » C Program » Reverse a Given Number Program : [php]#include<stdio.h> int main() { int num, rem, rev = 0; printf("nEnter any number to be reversed : "); scanf("%d", &num); while (num >= 1) { rem = num % 10; rev = rev * 10 + rem; num = num / 10; } printf("nReversed Number : %d", rev); return (0); }[/php] Output : [php]Enter any number to be reversed : 123 Reversed Number : 321[/php]
Computer Science » C Program » Even Number Pyramid Program : [php]#include<stdio.h> int main() { int i, j, num = 2; for (i = 0; i < 4; i++) { num = 2; for (j = 0; j <= i; j++) { printf("%dt", num); num = num + 2; } printf("n"); } return (0); }[/php] Output : [php]2 2 4 2 4 6 2 4 6 8 [/php]
Computer Science » C Program » Searching Element in an Array Program : [php]#include<stdio.h> int main() { int a[30], element, num, i; printf("nEnter no of elements : "); scanf("%d", &num); printf("nEnter the values : "); for (i = 0; i < num; i++) { scanf("%d", &a[i]); } //Read the element to be searched printf("nEnter the element to be searched : "); scanf("%d", &element); //Search starts from the zeroth location i = 0; while (i < num && element != a[i]) { i++; } //If i < num then Match found if (i < num) { printf("nNumber found at the location : %d", i + 1); } else { printf("nNumber not found."); } return (0); }[/php] Output 1: [php]Enter no of elements : 5 Enter the values : 10 20 30 40 50 Enter the element to be searched : 30 Number found at the location : 3[/php] Output 1: [php]Enter no of elements : 5 Enter the values : 10 20 30 40 50 Enter the element to be searched : 60 Number not found.[/php]
Computer Science » C Program » Inverted Pyramid Program : [php]#include<stdio.h> int main() { int i, j; char ch = '*'; for (i = 4; i >= 0; i--) { printf("n"); for (j = 0; j < i; j++) printf("%c", ch); } return(0); }[/php] Output : [php]**** *** ** *[/php]