Posts

Showing posts from 2016

C Program - Prime number or not using function

Image
#include<stdio.h> // Prototype of function prime returning integer and having int parameter user_num. int prime(int user_num); int main() { int num,ans; printf("Enter the Number = "); //Ask the user to insert the number. scanf("%d", &num); ans=prime(num);     // Call the prime function to check that number is //prime. if (ans == 1) // if prime function returns 1, number is prime printf("%d is prime.\n", num); else printf("%d is not prime.\n", num); // Otherwise not prime. getch(); } //Definition of function prime. int prime(int user_num) { int div; for (div=2; div <= user_num-1; div++)      // for loop to check that number //is prime. { if (user_num%div == 0) // returns 0 if statement is true. return 0; } if (user_num == div) return 1; // return 1 if statement is true. }

C Program - Elimination of duplicate numbers using an arrays

Image
#include<stdio.h> int main() { // 2 arrays with 10 elements // counter is initialized to 0 int  array1[10], array2[10], counter = 0; int j; // Input 10 numbers printf("Enter 10 Numbers = "); for (int i = 0;i<10; i++) scanf("%d", &array1[i]); // outer loop runs 10 times for (j = 0; j<10;j++) { int k; for (k = 0; k<counter; k++) { // If the array 1 element is equal to array 2 element it exits the loop if (array1[j] == array2[k]) break; } //else it will add that new element to another array 2 and increment the counter if (k == counter) { array2[counter] = array1[j]; counter++; } } // now counter is the number of elements in array 2 // prints the elements in array 2 printf("Elements after removing duplicate numbers \n"); for (j = 0; j<counter; j++) printf("%d ", array2[j]); getch(); }

C Program - Count the occurrence of numbers using array

Image
#include<stdio.h> #define RESPONSE_SIZE 100    // Actual number of times. #define FREQUENCY_SIZE 101   // Integers value from 0 to 100 int main() { size_t answer; // counter the loop through 100 times size_t rating; // counter the loop through frequency 1 to 100. int i = 1;  // i is for loop to check that if 0 comes stop inputing //Initializing the arrays to 0. int frequency[FREQUENCY_SIZE] = { 0 }; int responses[RESPONSE_SIZE] = { 0 }; printf("Enter the Integer between 1 and 100 = "); scanf("%d", &responses[0]); while (responses[i - 1] != 0 && i<100) { scanf("%d", &responses[i]); i++; } // for each answer, use the value as a array subscript //to determine the element to increment for (answer = 0; answer < RESPONSE_SIZE; ++answer) { ++frequency[responses[answer]]; } // to print the output of the frequencies // if the number of rating for certain integer is 0 // it ignores...

C Program - Find smallest in an Array using function

Image
#include<stdio.h> // function min with parameter as array and array size(function prototype) int min(int array[], int size); int main() { //initialize array to 0 int arr[10] = { 0 }; int size = 10; printf("Enter any 10 numbers = "); // for loop to scan 10 elements for (int i = 0; i < 10; i++) { scanf("%d", &arr[i]); } // passing the array and size and print smallest number on screen printf("The smallest number is %d", min(arr, size)); getchar(); } //Min function header int min(int array[], int size) { //initailizing one element to compare the number int element = array[0]; // for loop to compare array with smallest number for (int i = 0; i < size; i++) { if (array[i]<element) { element = array[i]; } } // returning smallest number. return element; }

C Program - Reverse of int array of random numbers

Image
#include<stdio.h> #include<stdlib.h> #include<time.h> void main() { //Array named arr with 10 elements int arr[10] = { 0 }; srand(time(NULL)); printf("The 10 random numbers are = \n"); //For loop to enter 10 random numbers as array elements. for (int counter = 0; counter < 10; counter++) { //inserting random numbers as array elements arr[counter] = (rand() % 100) + 1; printf("%d\n", arr[counter]); } //Printing the reverse order of numbers. printf("\nThe reverse order of numbers is = \n"); for (int counter = 9; counter >= 0; counter--) { printf("%d\n", arr[counter]); } getch(); }

C program - Finding today date according to user input

Image
Pic. reference - http://41.media.tumblr.com/ec1e2865d914521ded388fbcc447791c/tumblr_inline_nu6vqczp8y1tbt8rv_400.png #include<stdio.h> //Start Program void main() { int date,elapsed_days,future_day; //Declare the variables. printf("Enter an integer for today date of week = "); //Ask the user to enter integer for today. scanf("%d", &date); if (date < 7) //There are 7 days in week, if user inputs wrong, it should show an error message. { printf("Enter the number of days since elapsed today = "); //Input the days elasped since today. scanf("%d", &elapsed_days); future_day = (date + elapsed_days) % 7; //Find Future day. switch (date) //Print the message according to input. { case 0: // Two ways of writing program are switch and with if else. printf("Today is Sunday"); break; case 1: printf("Today is Monday"); break; case 2: printf(...