Skip to main content

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
   printf("\nEnter value of num1, num2 and num3:");
   //Store input values in variables for comparsion
   scanf("%d %d %d",&num1,&num2,&num3);

   if((num1>num2)&&(num1>num3))
      printf("\n Number1 is greatest");
   else if((num2>num3)&&(num2>num1))
      printf("\n Number2 is greatest");
   else
      printf("\n Number3 is greatest");
   return 0;
}

Output:

Enter value of num1, num2 and num3: 15 200 101
 Number2 is greatest

Comments

Popular posts from this blog

Syntax/Format of C program

C Programs A C program can vary from 3 lines to millions of lines and it should be written into one or more text files with extension ".c"; for example, hello.c. You can use "vi", "vim" or any other text editor to write your C program into a file. Before we study the basic building blocks of the C programming language, let us look at a bare minimum C program structure so that we can take it as a reference in the upcoming chapters.     Syntax/Format of simple C program main ()  <--------------------- Function name  {  <------------------------------ Start of program         ........         ........  <------------------- Program statement         ........  }  <------------------------------ End of program      Hello World Example A C program basically consists of the following parts − • Preprocessor Commands • Functions • Variables • Sta...

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...

IMPORTANCE OF C

IMPORTANCE OF C The increasing popularity of C is probably due to its many desirable qualities. It is a robust language whose rich set of built-in functions and operators can be used to write any complex program. The C compiler combines the capabilities of an assembly language with the features of a high-level language and therefore it is well suited for writing both system software and business packages. In fact, many of the C compilers available in the market are written in C.     Programs written in C are eficient and fast. This is due to its variety of data types and powerful operators. It is many times faster than BASIC. For example, a program to increment a variable from O to 15000 takes about one second in C while it takes more than 50 seconds in an interpreter BASIC.     There are only 32 keywords in ANSI C and its strength lies in its built-in functions. Several standard functions are available which can be used for developing program    C...