Skip to main content

Posts

Showing posts from April, 2022

Loops in C programming with example

Loops in C programming with example A loop is used for executing a block of statements repeatedly until a given condition returns false. Example of For loop #include <stdio.h> int main () { int i ; for ( i = 1 ; i <= 3 ; i ++) { printf ( "%d\n" , i ); } return 0 ; } Output: 1 2 3 Nested For Loop in C Nesting of loop is also possible. Lets take an example to understand this: #include <stdio.h> int main () { for ( int i = 0 ; i < 2 ; i ++) { for ( int j = 0 ; j < 4 ; j ++) { printf ( "%d, %d\n" , i , j ); } } return 0 ; } Output: 0 , 0 0 , 1 0 , 2 0 , 3 1 , 0 1 , 1 1 , 2 1 , 3 Example of while loop #include <stdio.h> int main () { int count = 1 ; while ( count <= 4 ) { printf ( "%d " , count ); count ++; } return 0 ; } Output: 1 2 3 4 Example of while loop using logical operator In this example

Loop In C Language

Loop In C Language  A loop statement allows us to execute a statement or group of statements multiple times. Given below is the general form of a loop statement in most of the programming languages − C programming language provides the following types of loops to handle looping requirements. Sr.No. Loop Type & Description 1 while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. 2 for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. 3 do...while loop It is more like a while statement, except that it tests the condition at the end of the loop body. 4 nested loops You can use one or more loops inside any other while, for, or do..while loop. While Loop in C A while loop is the most straightforward looping structure. While loop syntax in C programming language is as follows: Syntax of While Loop in C: while (condition) { statem

Basic Example Of C

C Program to check whether the given integer is positive or negative  /* Description : A program to check whether the input * integer number is positive or negative. * Written by : Santosh Asole */ #include <stdio.h> void main () { int num ; printf ( "Enter a number: \n" ); scanf ( "%d" , & num ); if ( num > 0 ) printf ( "%d is a positive number \n" , num ); else if ( num < 0 ) printf ( "%d is a negative number \n" , num ); else printf ( "0 is neither positive nor negative" ); } Output 1: Enter a number : 0 0 is neither positive nor negative Output 2: Enter a number : - 3 - 3 is a negative number Output 3: Enter a number : 100 100 is a positive number Program to find largest of three input numbers #include <stdio.h> int main () { int num1 , num2 , num3 ; //Ask user to input any three integer numbers p