Posts

Showing posts with the label C

C Program to Guess a Number

Image
From  Deitel C how to program (Making a difference) This is a carefully sort out source of in c program that plays "guess the number" as follows: The program chooses the number to be guessed by selecting a random integer in the range 1 to 1000.. The player types a first guess and presses the Enter key. If the player guess is incorrect, the program displays 'Too high.Try again or Too Low. Try again. When the user enters the correct answer, displays 'Congratulations. You guessed the number! We will basically be using the IF Statements to compare user input to determine if it matches the random generated number. So we  use the if statement to determine if the number input by the user is correct else the program either prints out too high or too low.  #include <stdio.h> #include <stdlib.h> #include <time.h>  int  main(void) {     int a=0, b=0;     int choice; srand(time(NULL));     a = 1 ...

Odd and Even

Image
Welcome in C programming, in this tutorial we will be looking at how to w rite a program that asks a user to input an integer and the program determines if the integer is odd or even. An  even number  is a  number  that can be divided into two equal groups. While an  odd number  is a  number  that cannot be divided into two equal groups.  We will be using the IF/ELSE statement to compare the  variable imputed by the user, so if "num" which is the variable name that holds the user input divides two (2) with no reminder, then its an even number, else its an odd number.  The program below, shows the step on how these is achieved. #include<stdio.h> #include<stdlib.h> int main(void) {     int num;      printf("Enter a number:");     scanf("%d", &num);      if(num%2==1){       ...

Simple Interest calculation

Image
In this C programming tutorial, i will be showing you how to write a program in C to calculate simple interest; and we are going to need an illustrated example to enable us get variable to use for the calculation.  So lets assume a person invests $4016.25 in a savings account yielding 9% interest; Assuming that all interest is left on deposit in the account, we are to write a program in C that calculates and prints the amount of money in the account at the end of each year for 5 years. Formula for calculating simple interest amount is:  a= p(1+r) n a= Amount  p= original amount(Principal),  r = Rate ,  n= Number of years. This problem involves a loop that performs the indicated calculation for each of the 5 years the money remains on deposit. We would also be using the  [pow]-Standard Library Function. The function pow(x,y) calculates the values of x raised to the yth power. The header <math.h> should always be included when...

Rolling a Six-Sided Die

Image
Welcome to C programming tutorial, we will be writing a program illustration that Rolls a Six-Sided. A fair  die  means that each of the faces has the same probability of landing facing up;  A standard  six - sided die , for example, can be considered "fair" if each of the faces has a probability of 1/6. The Program will be required to loop 20 times and on each loop the rand() function for number generation will be called to be picking numbers randomly between 1 to 6. #include<stdio.h> #include<stdlib.h> int main( void ) {     int i;     for (i=1; i<=20; i++){         printf("%5d", 1+(rand()%6));         if (i%5==0){             printf("\n");          }     }     return 0; ...

Using Do-While Repetition Statement

Image
A  do while loop  is a control flow  statement  that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block. this tutorial shows us how to use a do while repetition statement to break out loop. Now lets assume you are printing loads of data using this do while loop and you want it to stop at a particular point of mark, that is where the break statements comes in. The break statement are used to alter the flow of control, the break statement when executed in a do-while statement causes an immediate exit from that statement. The program below illustrates how to use the do-while statement. #include<stdio.h> int main(void) {     int x = 1;     do{ printf("%d", x);   if(x==1000){       break; } } while(x <=10000);             ...

A Simple Questionier Program

Image
Write a program that asks a user to input his name, age, and marital status and displays the result. /* A simple  questioner  program*/ #include<stdio.h> #include<stdlib.h> /*function main begins program execution */ int main ( void ) { /*Initialization phase */     char name [ 100 ];     char mari [50];     char yes;     int age; /*Processing phase */     printf("\t This Program helps catalog all your personal data.\n\n \tPlease complete the following question \n" );     printf ( "Whats your name:" );      scanf ( "%s" , name);     printf ( "How old are you:" );     scanf ( "%d" , & age);     printf ( "Are you Married:" );     scanf ( "%s" , mari);   /*Terminating phase */     printf ( "Tha...

Calculating Radius, Diameter, Circumference , Area and Length of a Circle

Image
Write a program that asks a user to input the radius and degree of a circle, the program reads it in and calculates: The radius, Diameter, Circumference, Area and Length of a circle. SOLUTION #include<stdio.h> #include<stdlib.h> /*function main begins program execution */ int main( void ) { /*Initialization phase */     int radius ;    // radius to be entered by the user     int degree ;  // degree to be entered by the user   /*Processing phase * /     printf ( "Insert radius and degree of the circle:" );  /*Prompting for input */     scanf ( "%d%d" , & radius , & degree );   /*reading radius and degree from user */     printf ("Radius is:%f\n" , radius*1.0);  /*displaying result       printf( "Diameter is:%f\n" , radius*2.0);  /*displaying result      ...

Function Maximum and Minimum

Image
Write a program that asks a user to input 3 separate integer, the program reads it and determines the maximum and minimum integer. Understanding Functions Modules in C are called functions . C programs are typically written by combining new functions you write with prepackaged functions available in the C standard library . The C standard library provides a rich collection of functions for performing common mathematical calculations , string manipulations , character manipulations , input/output , and many other useful operations. This makes your job easier, because these functions provide many of the capabilities you need. Functions are invoked by a function call , which specifies the function name and provides information (as arguments) that the function needs to perform its designated task. for more on functions click here Solution / *Finding the maximum/minimum of three integers */ #include<stdio.h> int maximum ( int x , int y , int z ); ...

Using the For Statement to Add

Image
Understanding the For Repetition statement Before we use the For Repetition Statement to add numbers, we need to understand what the For Statement really mean and does. The for statement is part of the control statement in C functions. The for statement is required for counter-controlled repetitions. C provides the For statement which specifies the counter-controlled-repetition. Counter-Controlled repetition is often called definite repetition, because the number of repetition terminates when the counter imputed is complete. SOLUTION  // How to Add with the For Selection statement  #include<stdio.h> /*function main begins program execution */ int main ( void ) { /*Initialization phase */     int sum = 0 ;    /*Initializing total */     int x ;   /*no of grade to be entered */ /*Processing phase */     for ( x = 3 ; x <= 1000 ; x += 3 ){    ...

Separating Integers

Image
Write  a program that inputs one five-digits number, separates the number into its individual digits and prints the digits from one another by three spaces each. solution #include<stdio.h> int main( void ) {     /*Starting numbers */     int n ;     int one , two , thr,fov,fiv ;     printf ( "Enter a five digits:" );     scanf ( "%d" , & n );     one = n / 10000 ;     two =( n % 10000 )/ 1000 ;     thr =(( n % 10000 )% 1000 )/ 100 ;     fov =((( n % 10000 )% 1000 )% 100 )/ 10 ;     fiv =(((( n % 10000 )% 1000 )% 100 )% 10 )/ 1 ;     printf ( "%d   %d   %d   %d   %d\n", one, two, thr, fov, fiv );     return 0 ; }