#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);
}
Output 1:
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
Output 1:
Enter no of elements : 5
Enter the values : 10 20 30 40 50
Enter the element to be searched : 60
Number not found.
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 » Check Whether a Number is Palindrome or Not Program : [php]#include <stdio.h> int main() { int n, reversedN = 0, remainder, originalN; printf("nEnter an integer : "); scanf("%d", &n); originalN = n; // reversed integer is stored in reversedN while (n != 0) { remainder = n % 10; reversedN = reversedN * 10 + remainder; n /= 10; } // palindrome if orignalN and reversedN are equal if (originalN == reversedN) printf("n%d is a palindrome.", originalN); else printf("n%d is not a palindrome.", originalN); return 0; }[/php] Output 1: [php]Enter an integer: 101 101 is a palindrome.[/php] Output 2: [php]Enter an integer: 102 102 is not a palindrome.[/php]
Computer Science » C Program » Find Average of Two Numbers Program : [php]#include <stdio.h> int main() { int num1, num2; float avg; printf("nEnter first number : "); scanf("%d",&num1); printf("nEnter second number : "); scanf("%d",&num2); avg= (float)(num1+num2)/2; printf("nAverage of %d and %d is : %f",num1,num2,avg); return 0; }[/php] Output : [php]Enter first number : 5 Enter second number : 3 Average of 5 and 3 is : 4.00[/php]
Computer Science » C Program » Check Whether a Number is Prime or Not Program : [php]#include<stdio.h> int main() { int num, i, count = 0; printf("nEnter a number :"); scanf("%d", &num); for (i = 2; i <= num / 2; i++) { if (num % i == 0) { count++; break; } } if (count == 0) printf("n%d is a prime number.", num); else printf("n%d is not a prime number.", num); return 0; }[/php] Output 1: [php]Enter a number : 5 5 is a prime number.[/php] Output 2: [php]Enter a number : 4 4 is not a prime number.[/php]