Computer Science » C Program » Print First 10 Natural Numbers Program : [php]#include<stdio.h> int main() { printf("1"); printf("2"); printf("3"); printf("4"); printf("5"); printf("6"); printf("7"); printf("8"); printf("9"); printf("10"); }[/php] Output : [php]12345678910[/php] Program : [php]#include<stdio.h> int main() { int i = 1; for (i = 1; i <= 10; i++) { printf("%d", i); } return (0); }[/php] Output : [php]12345678910[/php] Program : [php]#include<stdio.h> int main() { int i = 1; while (i <= 10) { printf("%d", i); i++; } return (0); }[/php] Output : [php]12345678910[/php] Program : [php]#include<stdio.h> int main() { int i = 1; do { printf("%d", i); i++; } while (i <= 10); return (0); }[/php] Output : [php]12345678910[/php] Program : [php]#include<stdio.h> int main() { int i = 0; Start: i = i + 1; printf("%d", i); if (i < 10) goto Start; }[/php] Output : [php]12345678910[/php]
Computer Science » C Program » Find greatest in 3 numbers Program : [php]#include<stdio.h> int main() { int a, b, c; printf("nEnter value of a, b & c : "); scanf("%d %d %d", &a, &b, &c); if ((a > b) && (a > c)) printf("na is greatest"); if ((b > c) && (b > a)) printf("nb is greatest"); if ((c > a) && (c > b)) printf("nc is greatest"); return(0); }[/php] Output : [php]Enter value for a,b & c : 12 16 20 c is greatest[/php]
Computer Science » C Program » Check if a Number is Divisible by 3 Program : [php]#include<stdio.h> int main() { int number; printf("Enter a number : "); scanf("%d",&number); if ((number % 3) == 0) printf("n%d is a multiple of 3.",number); else printf("n%d is not a multiple of 3.",number); return 0; }[/php] Output 1: [php]Enter a number : 9 9 is a multiple of 3.[/php] Output 2: [php]Enter a number : 8 8 is not a multiple of 3.[/php]
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 » Reversing an Array Elements Program : [php]#include<stdio.h> int main() { int arr[30], i, j, num, temp; printf("nEnter no of elements : "); scanf("%d", &num); //Read elements in an array for (i = 0; i < num; i++) { scanf("%d", &arr[i]); } j = i - 1; // j will Point to last Element i = 0; // i will be pointing to first element while (i < j) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; // increment i j--; // decrement j } //Print out the Result of Insertion printf("nArray after reversal : "); for (i = 0; i < num; i++) { printf("%d t", arr[i]); } return (0); }[/php] Output : [php]Enter no of elements : 5 10 20 30 40 50 Array after reversal : 50 40 30 20 10 [/php]