Tuesday, October 21, 2025

Input 2 values. and perform the Arithmetic Operations (Addition, Subtraction, Multiplication, Division and Reminder Division)

Input 2 values. and perform the Arithmetic Operations (Addition, Subtraction, Multiplication, Division and Reminder Division)


#include<stdio.h>

main()

{

      int a,b,c,d,e,f,g;

      c=d=e=f=g=0;

      scanf("%d%d",&a,&b);

      c=a+b;

      d=a-b;

      e=a*b;

      f=a/b;

      g=a%b;

      printf("\nC=%d\nD=%d\nE=%d",c,d,e);

      printf("\nF=%d\nG=%d",f,g);

}

Output






Formatted Input. scan function in C

 Formatted Input. scan function in C



Formatted Input: Formatted input refers to an input data that has been arranged in a particular format.

scanf(): Scanf() function can be used input  the data into the memory from the standard input device.  This function can be used to enter any combination of numerical Values, single characters and strings.  The function returns number of data items.

Syntax:- 

      scanf (“conversion strings”, &arg1,&arg2,--------&arg-n);

Where conversion string reefers to a string containing certain required formatting information and arg1, arg2----arg-n are arguments that represent the individual input data items.

Example:                   

Where %s , %d, %f, %c with % are conversion characters.  The conversion characters indicate the type of the to corresponding data. Commonly used conversion characters for data input.

 int a;  float b; char n,sn[20];

        scanf(“%d”,&a);

        scanf(“%f”,&b;

        scanf(“%c”,&n);

        scanf(“%s”,sn);

        scanf(“%d%f%c%s”,&a,&b,&n,sn);

Storage Class in C

 Storage Class in C


            

The scope and lifetime of a variable or / and function within a C program is defined by storage class. There are four storage classes in C


auto -      It is the default storage class for all variables and / or   functions.


register - Allows for defining variables to store in CPU register  instead of RAM. Unary operator is not applied for register    variable.


static –   The static storage class allows the updated variable values  available  for the next time when the function, in which the  variable is defined,  is invoked for the next time.


extern -   It allows a global variable to be visible to all the program  files /  external files ( C programs).

     

Calculate Simple Interest

 Calculate Simple Interest with following detail. p=1000, n=5 and  r=2.5


#include<stdio.h>

main()

{

      int n=5;

      float p=1000,r=2.5,si=0.0;

      si=p*n*r/100;

      printf("\nSimple Interest %.2f",si);

} 

      Output





Perform Simple Arithmetic Operation Program

 

Perform Simple Arithmetic Operation Program

Where a is 5 and b is 2.    

 +(Add)      -(Subtraction)   *(Multiplication

 / (Division)   %(Remainder)


#include<stdio.h>

main()

{

      int a=5,b=2,c,d,e,f,g;

      c=d=e=f=g=0;

      c=a+b;

      d=a-b;

      e=a*b;

      f=a/b;

      g=a%b;

      printf("\nC=%d\nD=%d\nE=%d",c,d,e);

      printf("\nF=%d\nG=%d",f,g);

}


Output 



Note : if Both are Integer then result is 

       integer other wise Float.

   Ex. (5/2.0  or  5.0/2   or   5.0/2.0 -----          2.5)


Monday, October 20, 2025

Formatted Output

Formatted Output

printf():  The printf() function is used to print the data from the computers memory onto a standard output device.  This function can be used to output any combination of numerical values, single character and strings.

Syntax: 

printf(“Control String Control  Message Conversion String”, List of arg1 );


Where control string is a string that contains backslash characters, Control message that conations the user defined information and conversion string that contains formatted information, and list of arg.  are arguments that represent the output data items.


A simple format specification has the following form:

%w.p type-specifier

Where w is an integer number that specifies the total number of columns for the output value and p is another integer number that specifies the number of digits to the right of the decimal point (of a real number) or the number of characters to be printed from a string. Both wad p are optional. Some examples of formatted printf statement are:

Example :  

int  a=6; float b=2.75; char d=’A’, city[10]=”KHAMMAM”;


Statement                 Output

printf(“Welcome To C Program”);         Welcome  To C Program

printf(“\nWelcome  To C Program”);  (skip line and Print next line)

                                                                         Welcome  To C Program

printf(“%d”,a);                        5

printf(“%f”,b);                 2.75

printf(“%c”,d);                         A

printf(“%s”,city);                 KHAMMAM

printf(“%d%f%c%s”,a,b,d,city);         52.75AKHAMMAM

printf(“%d\t%f\t%c\t%s”,a,b,d,city); 5   2.75   A    KHAMMAM

printf(“A=%d\tB=%f\tD=%c”,a,b,d); A=5    B=2.75    D=A


Where  %c %s %d %f are conversion characters.

 Output of Integer Numbers: The format specification for printing an integer number is

%wd

                Format   Output

printf("%d", 9876)                              9876

printf("%6d", 9876)                            _ _ 9876

                     printf("%2d", 9876)                            9876

          printf("%-6d", 9876)                           9876_ _

          printf("%06d", 9876)                          009876


Output of Real Numbers

The output of a real number may be displayed in decimal notation using the following format specification.

%w.pf

 The integer w indicates the minimum number of positions that are to be used for the display of the value and the integer p indicates the number of digits to be displayed after the decimal point (precision). The value, when displayed, is rounded to p decimal places and printed right-justified in the field of w columns. Leading blanks and trailing zeros will appear as necessary. The default precision is 6 decimal places. The negative numbers will be printed with the minus sign. The number will be displayed in the form -.

            Ex. y=98.7654

Format                                                 Output

printf("% 7.4f",y)                                98.7654

printf("%7.2f",y)                                 _ _98.77

printf("%-7.2f",y)                                98.77_ _

printf("%f",y)                                      98.7654

printf("%10.2e",y)                               _ _9.88e+01

printf("%11.4e",-y)                             -9.8765e+01

printf("%-l0.2e",y)                              9.88e+01_ _

printf("%e",y)                                      9.876540e+01

Printing of a Single Character

A single character can be displayed in a desired position using the format

%wc

The character will be displayed right-justified the field of w columns. We can make the display left-Justified by placing a minus sign before the integer w. The default value for w is 1.

KHAMMAM 507001

Specification                   Output 

                        |1 |2 |3 | 4 |5 |6 |7 |8 |9 |0 |1 |2  |3 |4 |5 |6 |7 |8 |9 |0  |

%s                   |K|H|A|M|M|A|   |5 |0 |7 |0  |0 |1 |   |   |   |   |   |   |    |

%20s               |   |   |   |   |    |   |   |K|H|A|M|M|A|   |5 |0 |7 |0 |0 |1  |

%20.10s          |   |   |   |   |    |   |   |   |   |   |K|H|A|M|M|A|M|   |5 | 0 |

%.5s                |K|H|A|M|M||  |   |   |   |   |   |   |    |   |   |   |   |   |   |    |

%-20.10s         |K|H|A|M|M|A|   |5 |0 |   |   |   |    |   |   |   |   |   |   |    |

%5s                 |K|H|A|M|M|A|   |5 |0 |7 |0 |0 |1  |   |   |   |   |   |   |    |


Backslash Character (Only Output)

 Backslash Character  (Only Output)


C supports some special backslash character constants that are  used in output function.


"\a"     Alert or bell

"\b"     Back space      

"\f"     Form Feed

"\n"     New line

"\r"     Return

"\t"     Tab

"\v"     Vertical tab

"\\"     Print \ (back slash)

"\?"     Print ?

"\'"     Print ' (Single Quote)

"\""     Print " (Double Quote)

"\0"     Null

"%%"    Print %