int main()
{
int array[9];
int i;
printf("Enter the element of the array : ");
for (i = 0; i < 9; i++)
scanf("%d", &array[i]);
printf("nAlternate elements of the entered array : n");
for (i = 0; i < 9; i += 2)
printf( "%dn", array[i]) ;
}
Output :
Enter the element of the array :
1
2
3
4
5
6
7
8
9
Alternate elements of the entered array :
1
3
5
7
9
Computer Science » C Program » Fibonacci Series Starting from Any Two Numbers Program : [php]#include<stdio.h> int main() { int first, second, sum, num, counter = 0; printf("Enter the term : "); scanf("%d", &num); printf("nEnter First Number : "); scanf("%d", &first); printf("nEnter Second Number : "); scanf("%d", &second); printf("nFibonacci Series : %d %d ", first, second); while (counter < num) { sum = first + second; printf("%d ", sum); first = second; second = sum; counter++; } return (0); }[/php] Output : [php]Enter the term : 5 Enter First Number : 1 Enter Second Number : 3 Fibonacci Series : 1 2 3 5 8 13 21[/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 » 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 » Print an Integer Entered by the User Program : [php]#include <stdio.h> int main() { int number; printf("Enter an integer : "); scanf("%d", &number); printf("nInteger entered by you : %d", number); return 0; }[/php] Output : [php]Enter an integer : 121 Integer entered by you : 121[/php]