Last active
March 15, 2017 14:10
-
-
Save sazid/58492ab9ee79cd27b6a94afdb88a4275 to your computer and use it in GitHub Desktop.
C programs for practise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Print all the prime numbers from 1...n | |
* and also show the sum of all the prime numbers within this range. | |
*/ | |
#include <stdio.h> | |
int main() | |
{ | |
// loop controllers | |
int i, outer_loop_counter; | |
// count of prime numbers | |
int count = 0; | |
// sum of the prime numbers | |
int sum = 0; | |
// user input | |
int user_input_number; | |
// this variable will indicate whether a number is prime or not | |
// 1 - TRUE; 0 - FALSE | |
int is_prime; | |
// take user input | |
printf("Input a number: "); | |
scanf("%d", &user_input_number); | |
for (outer_loop_counter = 1; outer_loop_counter <= user_input_number; outer_loop_counter++) { | |
// reset the variable after every loop | |
is_prime = 1; | |
for (i = 2; i <= outer_loop_counter / 2; i++) { | |
if (outer_loop_counter % i == 0) { | |
is_prime = 0; | |
} | |
} | |
if (is_prime == 1) { | |
sum = sum + outer_loop_counter; | |
count++; | |
printf("%d is prime.\n", outer_loop_counter); | |
} else { | |
printf("%d is not prime.\n", outer_loop_counter); | |
} | |
} | |
printf("Sum of all the prime numbers within 1...%d is: %d\n", user_input_number, sum); | |
printf("Count of prime numbers: %d\n", count); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/** | |
* Array basics | |
*/ | |
void main() { | |
/* | |
* An array holding 5 integer values | |
* this is equivalent to declaring 5 separate variables like | |
* int a, b, c, d, e; | |
*/ | |
int my_array[ 5 ]; | |
/* | |
* Arrays have 0 based index (position). So, first item is at | |
* index (position) 0, second item is at 1 and so forth | |
* | |
* SYNTAX: my_array[ index/position ] | |
*/ | |
my_array[0] = 5; // we're setting the value of first position's value to 5 | |
my_array[1] = 3; | |
my_array[2] = 6; | |
my_array[3] = 10; | |
my_array[4] = 20; | |
printf( "Value at position 3 is: %d\n", my_array[3] ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/** | |
* Accessing array using a loop | |
*/ | |
void main() { | |
// This is another way of declaring an array | |
// AND also initializing the values at the same time | |
int x[10] = { 20, -40, 30, 60, 80, 43, 85, 81, 99, -102 }; | |
int i; | |
// we can take advantage of a loop to access a large array! | |
// let us assume i will hold the index of the array | |
for (i = 0; i < 10; i++) { | |
// print the index | |
printf("i: %d\n", i); | |
// print the value at that index | |
printf("x[%d]: %d\n\n", i, x[i]); | |
// OUTPUT will be similar to: | |
// x[3]: 60 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/** | |
* Find largest number in an array | |
*/ | |
void main() { | |
// this time, let's take the value of 5 items from the user | |
int x[5]; | |
int i; | |
int largest_number; | |
// take 5 inputs from the user by looping 5 times and | |
// set the values | |
for (i = 0; i < 5; i++) { | |
printf("Input value x[%d]: ", i); | |
scanf( "%d", &x[i] ); | |
} | |
// let us assume, the largest number is at position 0 | |
largest_number = x[0]; | |
for (i = 0; i < 5; i++) { | |
// if the largest number is less than the value of x[i] | |
// then x[i] is the NEW largest number | |
if (largest_number < x[i]) { | |
largest_number = x[i]; | |
} | |
} | |
printf("Largest number is: %d\n", largest_number); | |
/** | |
* Sample OUTPUT: | |
Input value x[0]: 20 | |
Input value x[1]: -23 | |
Input value x[2]: 450 | |
Input value x[3]: 23 | |
Input value x[4]: 89 | |
Largest number is: 450 | |
*/ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/** | |
* Find second largest number in an array | |
*/ | |
void main() { | |
// Here, 54 is the SECOND LARGEST and 823 is the LARGEST | |
int x[5] = { 823, 12, 53, 23, 54 }; | |
int i; | |
int largest_number; | |
int second_largest_number; | |
// let us assume, largest number is at position 0 | |
// and second largest number is at position 1 | |
// WARNING: DO NOT SET THE VALUE OF BOTH TO THE SAME POSITION | |
largest_number = x[0]; | |
second_largest_number = x[1]; | |
// find the largest number | |
for (i = 0; i < 5; i++) { | |
if (largest_number < x[i]) { | |
// change the value of largest_number to the new largest number | |
largest_number = x[i]; | |
} | |
} | |
// find the second largest number | |
for (i = 0; i < 5; i++) { | |
// check whether the value of x[i] is NOT EQUALS to the previously | |
// calculated largest number | |
if ( (second_largest_number < x[i]) && (x[i] != largest_number) ) { | |
second_largest_number = x[i]; | |
} | |
} | |
printf("Largest number is: %d\n", largest_number); | |
printf("Second largest number is: %d\n", second_largest_number); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
// change this value to the size of the array you're working with | |
#define ARRAY_SIZE 11 | |
/** | |
* Reverse an array | |
* @return 0 | |
*/ | |
void main() { | |
int x[ARRAY_SIZE] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; | |
// The swapping will continue till half of the array is reached | |
int exchange_upto = ARRAY_SIZE / 2; | |
for (int i = 0; i < exchange_upto; i++) { | |
// as x[i] will soon be used to hold another value in the next line of code, | |
// let's store the current value in a temporary variable | |
int temp = x[i]; | |
// if i = 4, then | |
// = ((ARRAY_SIZE - 1) - i) | |
// = (( 11 - 1) - 4) | |
// = (10 - 4) | |
// = 6 | |
// Store the value of position 6 in 4 | |
x[i] = x[ (ARRAY_SIZE - 1) - i ]; | |
// Store the value of position 4 in 6 | |
// here, 'temp' contains the original value of x[i] before | |
// the swapping happened | |
x[ (ARRAY_SIZE - 1) - i ] = temp; | |
// The following line shows how the swapping works, you can remove it | |
printf("x[%d] <-> x[%d]\n", i, (ARRAY_SIZE - 1) - i); | |
} | |
// show the result | |
for (int i = 0; i < ARRAY_SIZE; ++i) { | |
printf("%d\n", x[i]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void main() { | |
int x[7]; | |
int i; | |
// ----- START OF 1 ----- // | |
printf("\n\n1. TAKE INPUT AND SHOW THE RESULT ------------------\n\n"); | |
// TAKE INPUT FROM THE USER | |
for (i = 0; i < 7; i++) { | |
printf("Input %d: ", i); | |
scanf("%d", &x[i]); | |
} | |
printf("\nOutput:\n"); | |
// DISPLAY THE RESULT | |
for (i = 0; i < 7; i++) { | |
printf("x[%d]: %d\n", i, x[i]); | |
} | |
// ----- START OF 2 ----- // | |
printf("\n\n2. FIND MAX ------------------\n\n"); | |
int max = x[0]; | |
for (i = 0; i < 7; i++) { | |
if (max < x[i]) | |
max = x[i]; | |
} | |
printf("MAX: %d\n", max); | |
// ----- START OF 3 ----- // | |
printf("\n\n3. FIND MIN ------------------\n\n"); | |
int min = x[0]; | |
for (i = 0; i < 7; i++) { | |
if (min > x[i]) | |
min = x[i]; | |
} | |
printf("MIN: %d\n", min); | |
// ----- START OF 4 ----- // | |
printf("\n\n4. FIND AVERAGE ------------------\n\n"); | |
int average; | |
int sum = 0; | |
for (i = 0; i < 7; i++) { | |
sum = sum + x[i]; | |
} | |
average = sum / 7; | |
printf("Average: %d\n", average); | |
// ----- START OF 5 ----- // | |
printf("\n\n5. FIND MAX INDEX ------------------\n\n"); | |
int max_index; | |
for (i = 0; i < 7; i++) { | |
if (max == x[i]) | |
max_index = i; | |
} | |
printf("Max index: %d\n", max_index); | |
// ----- START OF 16----- // | |
printf("\n\n6. FIND MIN INDEX ------------------\n\n"); | |
int min_index; | |
for (i = 0; i < 7; i++) { | |
if (min == x[i]) | |
min_index = i; | |
} | |
printf("Min index: %d\n", min_index); | |
// ----- START OF 7 ----- // | |
printf("\n\n7. SEARCH INPUT NUMBER ------------------\n\n"); | |
int input_num; | |
printf("Input a number: "); | |
scanf("%d", &input_num); | |
for (i = 0; i < 7; i++) { | |
if (input_num == x[i]) { | |
printf("Found at index: %d\n", i); | |
break; | |
} else if (i == 6) { | |
printf("The given number is not present in the array.\n"); | |
} | |
} | |
// ----- START OF 8 ----- // | |
printf("\n\n8. FIND SECOND MINIMUM ------------------\n\n"); | |
int second_min = x[max_index]; | |
for (i = 0; i < 7; i++) { | |
if (second_min > x[i] && x[i] != min) { | |
second_min = x[i]; | |
} | |
} | |
printf("Second minimum: %d\n", second_min); | |
// ----- START OF 9 ----- // | |
printf("\n\n9. FIND SECOND LARGEST ------------------\n\n"); | |
int second_largest = x[min_index]; | |
printf("%d\n", max); | |
for (i = 0; i < 7; i++) { | |
if (second_largest < x[i] && x[i] != max) { | |
second_largest = x[i]; | |
} | |
} | |
printf("Second largest: %d\n", second_largest); | |
// ----- START OF 10 ----- // | |
printf("\n\n10. FIND MINIMUM LENGTH ------------------\n\n"); | |
int length = max - min; | |
int j; | |
int comp; | |
for (i = 0; i < 7; i++) { | |
for (j = 0; j < 7; j++) { | |
if (i == j) continue; | |
if (x[i] > x[j]) | |
comp = x[i] - x[j]; | |
else | |
comp = x[j] - x[i]; | |
if (length > comp) { | |
length = comp; | |
} | |
} | |
} | |
printf("Minimum length: %d\n", length); | |
} | |
max = x[i]; | |
} | |
printf("MAX: %d\n", max); | |
// ----- START OF 3 ----- // | |
printf("3. FIND MIN ------------------\n\n"); | |
int min = x[0]; | |
for (i = 0; i < 7; i++) { | |
if (min > x[i]) | |
min = x[i]; | |
} | |
printf("MIN: %d\n", min); | |
// ----- START OF 4 ----- // | |
printf("4. FIND AVERAGE ------------------\n\n"); | |
int average; | |
int sum = 0; | |
for (i = 0; i < 7; i++) { | |
sum = sum + x[i]; | |
} | |
average = sum / 7; | |
printf("Average: %d\n", average); | |
// ----- START OF 5 ----- // | |
printf("5. FIND MAX INDEX ------------------\n\n"); | |
int max_index; | |
for (i = 0; i < 7; i++) { | |
if (max == x[i]) | |
max_index = i; | |
} | |
printf("Max index: %d\n", max_index); | |
// ----- START OF 16----- // | |
printf("6. FIND MIN INDEX ------------------\n\n"); | |
int min_index; | |
for (i = 0; i < 7; i++) { | |
if (min == x[i]) | |
min_index = i; | |
} | |
printf("Min index: %d\n", min_index); | |
// ----- START OF 7 ----- // | |
printf("7. SEARCH INPUT NUMBER ------------------\n\n"); | |
int input_num; | |
printf("Input a number: "); | |
scanf("%d", &input_num); | |
for (i = 0; i < 7; i++) { | |
if (input_num == x[i]) { | |
printf("Found at index: %d\n", i); | |
break; | |
} else if (i == 6) { | |
printf("The given number is not present in the array.\n"); | |
} | |
} | |
// ----- START OF 8 ----- // | |
printf("8. FIND SECOND MINIMUM ------------------\n\n"); | |
int second_min = x[max_index]; | |
for (i = 0; i < 7; i++) { | |
if (second_min > x[i] && x[i] != min) { | |
second_min = x[i]; | |
} | |
} | |
printf("Second minimum: %d\n", second_min); | |
// ----- START OF 9 ----- // | |
printf("9. FIND SECOND LARGEST ------------------\n\n"); | |
int second_largest = x[min_index]; | |
printf("%d\n", max); | |
for (i = 0; i < 7; i++) { | |
if (second_largest < x[i] && x[i] != max) { | |
second_largest = x[i]; | |
} | |
} | |
printf("Second largest: %d\n", second_largest); | |
// ----- START OF 10 ----- // | |
printf("10. FIND MINIMUM LENGTH ------------------\n\n"); | |
int length = max - min; | |
int j; | |
int comp; | |
for (i = 0; i < 7; i++) { | |
for (j = 0; j < 7; j++) { | |
if (i == j) continue; | |
if (x[i] > x[j]) | |
comp = x[i] - x[j]; | |
else | |
comp = x[j] - x[i]; | |
if (length > comp) { | |
length = comp; | |
} | |
} | |
} | |
printf("Minimum length: %d\n", length); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Find if a number is divisible by both 3 AND 7 | |
*/ | |
#include <stdio.h> | |
int main() | |
{ | |
int input_number; | |
int count = 0; | |
printf("Input a number: "); | |
scanf("%d", &input_number); | |
// Replace '&&' with '||' in the line below, | |
// to check for OR condition instead of AND | |
if (input_number % 3 == 0 && input_number % 7 == 0) { | |
printf("%d is divisible by both 3 and 7.\n", input_number); | |
count++; | |
} else { | |
printf("%d is divisible NOT by both 3 and 7.\n", input_number); | |
} | |
printf("Count is: %d\n", count); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() | |
{ | |
// ask the user for how many input numbers to be taken | |
int number_of_inputs; | |
// loop counter | |
int i; | |
// this variable will hold user input in each looping step | |
int n; | |
// counter - for positive numbers | |
int positive_count = 0; | |
// counter - for negative numbers | |
int negative_count = 0; | |
printf("Number of inputs: "); | |
scanf("%d", &number_of_inputs); | |
for (i = 0; i < number_of_inputs; i++) | |
{ | |
printf("Input #%d: ", i); | |
scanf("%d", &n); | |
if (n >= 0) | |
positive_count++; | |
else | |
negative_count++; | |
} | |
printf("Positive count: %d\n", positive_count); | |
printf("Negative count: %d\n", negative_count); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Find if a year is leap year or not | |
*/ | |
#include <stdio.h> | |
int main() | |
{ | |
int year; | |
printf("Input a year: "); | |
scanf("%d", &year); | |
if (year % 400 == 0) | |
printf("%d is a leap year.\n", year); | |
else if (year % 4 == 0 && year % 100 != 0) | |
printf("%d is a leap year.\n", year); | |
else | |
printf("%d is NOT a leap year.\n", year); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Find if a number is prime or not | |
*/ | |
#include <stdio.h> | |
int main() | |
{ | |
int input_number; | |
int i; | |
// is_prime = 1 ----- The number is prime | |
// is_prime = 0 ----- The number is NOT prime | |
int is_prime = 1; | |
printf("Input a number: "); | |
scanf("%d", &input_number); | |
// i++ ---> i = i + 1 | |
for (i = 2; i < (input_number / 2); i++) | |
{ | |
if (input_number % i == 0) | |
{ | |
is_prime = 0; | |
break; | |
} | |
} | |
if (is_prime == 1) | |
printf("The number is prime.\n"); | |
else | |
printf("The number is not prime.\n"); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Find the reverse of a number | |
* Also, find the sum of the digits of a number | |
*/ | |
#include <stdio.h> | |
int main() | |
{ | |
int n; | |
int reverse_number = 0; | |
int sum = 0; | |
int temp; | |
int reminder; | |
printf("Input a number: "); | |
scanf("%d", &n); | |
temp = n; | |
while (n > 0) | |
{ | |
reminder = n % 10; | |
reverse_number = (reverse_number * 10) + reminder; | |
sum += reminder; | |
n = n / 10; | |
} | |
printf("The reverse of %d is: %d\n", temp, reverse_number); | |
printf("The sum of the digits of %d is: %d\n", temp, sum); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Sum of all the odd numbers from 1 to n | |
*/ | |
#include <stdio.h> | |
int main() | |
{ | |
int sum = 0; | |
int n; | |
int i; | |
printf("Input a number: "); | |
scanf("%d", &n); | |
// i += 2 ---> i = i + 2 | |
for (i = 1; i <= n; i += 2) | |
{ | |
// sum = sum + i | |
sum += i; | |
} | |
printf("Sum of all the odd numbers is: %d\n", sum); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Find if the input character is a vowel/consonant | |
*/ | |
#include <stdio.h> | |
int main() | |
{ | |
char input_character; | |
printf("Input a character: "); | |
scanf("%c", &input_character); | |
switch (input_character) | |
{ | |
case 'a': | |
case 'e': | |
case 'i': | |
case 'o': | |
case 'u': | |
printf("%c is a vowel.\n", input_character); | |
break; | |
default: | |
printf("%c is a consonant.\n", input_character); | |
break; | |
} | |
return 0; | |
} |
Great Samples!
Useful for all of us.....!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Find the roots of a quadratic equation in the form of: ax^2 + bx + c = 0

https://gist.github.com/sazid/b9f94b56674c69682df7f7bad8ad7165