Posts

Showing posts from September, 2018

C program to Reverse a Sentence Using Recursion #43

C program to Reverse a Sentence Using Recursion This program takes a sentence from user and reverses that sentence using recursion. This program does not use string to reverse the sentence or store the sentence. To understand this example, you should have the knowledge of following C programming topics: C Programming Functions C Programming User-defined functions C Programming Recursion Example: Reverse a sentence using recursion /* Example to reverse a sentence entered by user without using strings. */ #include <stdio.h> void reverseSentence (); int main () { printf ( "Enter a sentence: " ); reverseSentence (); return 0 ; } void reverseSentence () { char c ; scanf ( "%c" , & c ); if ( c != '\n' ) { reverseSentence (); printf ( "%c" , c ); } } Output Enter a sentence: margorp emosewa awesome program This program first p

C Program to Convert Binary Number to Octal and vice-versa #42

C Program to Convert Binary Number to Octal and vice-versa In this example, you will learn to convert binary number to octal and octal number to binary manually by creating a user-defined function. To understand this example, you should have the knowledge of following C programming topics: C Programming Functions C Programming User-defined functions Example 1: Program to Convert Binary to Octal In this program, we will first convert binary number to decimal. Then, the decimal number is converted to octal. #include <stdio.h> #include <math.h> int convertBinarytoOctal ( long long binaryNumber ); int main () { long long binaryNumber ; printf ( "Enter a binary number: " ); scanf ( "%lld" , & binaryNumber ); printf ( "%lld in binary = %d in octal" , binaryNumber , convertBinarytoOctal ( binaryNumber )); return 0 ; } int convertBinarytoOctal ( long long binaryNumber ) {

Control Flow Examples

Image
C Programming Decision Making and Loops Examples You will find necessary examples to create programs containing loops and decision making statements in this article. To understand examples on this page, you should have the knowledge of following topics: if...else Statement for Loop while Loop break and Continue Statement switch...case C Control Flow Examples go on to the label section and select C PROGRAMMING EXAMPLES  

C switch...case Statement

Image
C switch...case Statement In this tutorial, you will learn to write a switch statement in C programming (with an example). The  if..else..if  ladder allows you to execute a block code among many alternatives. If you are checking on the value of a single variable in  if...else...if , it is better to use  switch statement. The switch statement is often faster than nested  if...else  (not always). Also, the syntax of switch statement is cleaner and easy to understand. Syntax of switch...case switch (n) ​{ case constant1: // code to be executed if n is equal to constant1; break; case constant2: // code to be executed if n is equal to constant2; break; . . . default: // code to be executed if n doesn't match any constant } When a case constant is found that matches the switch expression, control of the program passes to the block of code associated with that case. In the above pseudoco

C break and continue Statement

Image
C Programming break and continue Statement In this tutorial, you will learn how to use break and continue statements to alter the program flow of loops. It is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression. In such cases,  break  and  continue  statements are used. break Statement The break statement terminates the loop ( for ,  while and do...while loop ) immediately when it is encountered. The break statement is used with decision making statement such as  if...else . Syntax of break statement break; The simple code above is the syntax for break statement. Flowchart of break statement How break statement works? Example #1: break statement // Program to calculate the sum of maximum of 10 numbers // Calculates sum until user enters positive number # include <stdio.h> int main () { int i ; double number , sum = 0.0 ; for ( i = 1 ;

While and Do...While Loop

Image
C Programming while and do...while Loop Loops are used in programming to repeat a specific block of code. After reading this tutorial, you will learn how to create a while and do...while loop in C programming. Loops are used in programming to repeat a specific block until some end condition is met. There are three loops in C programming: for loop while loop do...while loop while loop The syntax of a while loop is: while (testExpression) { //codes } where,  testExpression  checks the condition is true or false before each loop. How while loop works? The while loop evaluates the test expression. If the test expression is true (nonzero), codes inside the body of while loop are exectued. The test expression is evaluated again. The process goes on until the test expression is false. When the test expression is false, the while loop is terminated. Flowchart of while loop Example #1: while loop // Program to find factorial of a number // Fo