Showing posts with label Fundamental. Show all posts
Showing posts with label Fundamental. Show all posts
 C PROGRAMS : FUNDAMENTALS

C PROGRAMS : FUNDAMENTALS

C PROGRAMS : FUNDAMENTALS
11. Program to input a number and find whether it is even or odd
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variale "n" to store input number
 int n,r;
 
 // Inputting number
 printf("Enter a number : ");
 scanf("%d", &n);
 
 // Checking if input number is ever or odd
 if(n%2==0)
 printf("Entered number is even");
 else
 printf("Entered number is odd");
 
 getch(); // Linux user - Remove this

 return 0;
}

12. Program to find biggest of four no by using ternary numbers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable a, b, c, d to store 4 numbers input by user
 int a,b,c,d;
 
 // Declaring variable big to store the biggest value
 int big;

 // Inputting number
 printf("Enter first number : ");
 scanf("%d",&a);
 printf("Enter second number : ");
 scanf("%d",&b);
 printf("Enter thirda number : ");
 scanf("%d",&c);
 printf("Enter fourth number : ");
 scanf("%d",&d);
 
 // Determining biggest number
 big=(a>b)?(a>c)?(a>d)?a:d:(c>d)?c:d:(b>c)?(b>d)?b:d:(c>d)?c:d;

 
 // Printing smallest number
 printf("Biggest of four number = %d",big);
 
 getch(); // Linux user - Remove this

 return 0;
}

13. Program to print smallest of four no by using ternary operators
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable a, b, c, d to store 4 numbers input by user
 int a,b,c,d;
 
 // Declaring variable small to store the smallest value
 int small;

 // Inputting number
 printf("Enter first number : ");
 scanf("%d",&a);
 printf("Enter second number : ");
 scanf("%d",&b);
 printf("Enter thirda number : ");
 scanf("%d",&c);
 printf("Enter fourth number : ");
 scanf("%d",&d);
 
 // Determining smallest number
 small=(a<b)?(a<c)?(a<d)?a:d:(c<d)?c:d:(b<c)?(b<d)?b:d:(c<d)?c:d;
 
 // Printing smallest number
 printf("Smallest of four number = %d",small);
 
 getch(); // Linux user - Remove this

 return 0;
}

14. Program to accept a year and check the given year is leap or not by using ternary
#include<stdio.h>
#include<conio.h> // Linux user - Remove this


int main()
{
 // Declaring variable "y" to input year and "leap" to determine leap year
 int y,leap;
 
 // Inputting year
 printf("Enter any year : ");
 scanf("%d",&y);
 
 // Checking if leaf year or not
 leap=(y%400==0)?:(y%100!=0)?(y%4==0)?1:0:0;

 if(leap==1)
 printf("The given year is leap year");
 else
 printf("The given year is not leap year");

 getch(); // Linux user - Remove this

 return 0;
}
15. Program to find area of a triangle when there sides are given
# include <stdio.h>
# include <conio.h> // Linux user - Remove this


int main( )
 {
 // Declaring variable a, b, c = to store three sides of triangle
 int a,b,c;
 
 // Declaring variable s = mid value
 float s, area;

 // Inputing sides of triangle
 printf("Enter there sides of the triangle : ");
 scanf("%d%d%d",&a,&b,&c);
 
 // Checking if finding area is possible or not
 if((a+b)<c||(b+c)<a||(a+c)<b)
 printf("Finding area is not possible");
 
 // Calculating area
 else
 {
 s=(a+b+c)/2;
 area=sqrt(s*(s-a)*(s-b)*(s-c));
 }
 
 // Printing area
 printf("Area=%.2f",area);
 
 getch( ); // Linux user - Remove this

 return 0;
}
C PROGRAMS : FUNDAMENTALS

C PROGRAMS : FUNDAMENTALS

C PROGRAMS : FUNDAMENTALS
6. Program to find the average of three numbers with input values as integer and output value (avg) as float
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
    int a, b, c;
    float avg;
    
    printf("Enter any three numbers ");
    scanf("%d %d %d", &a, &b, &c);
    avg = ( (float)a + (float)b + (float)c ) / 3; 
    /* we have type casted integer to float as a, b, c, 3 all are integer type and
     integer / integer gives the result integer. */
    printf("Average=%f", avg);
    getch();// Linux user - Remove this

    return 0;
}

7. Program to find the area of a right angled triangle based on base and height values
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
    float h, b, area;
    
    printf("Enter base ");
    scanf("%f",&b);
    printf("Enter height ");
    scanf("%f", &h);
    area=(0.5)*b*h;
    printf("Area of right angled triangle= %f", area);
    getch(); // Linux user - Remove this

    return 0;
}

8. Program to find or obtain the sum of square of two floating point numbers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
    float a, b, sum;
    
    printf("Enter first number ");
    scanf("%f", &a);
    printf("Enter second number ");
    scanf("%f", &b);
    printf("Square of first number = %f", a*a);
    printf("\n Square of second number = %f", b*b);
    sum = (a*a + b*b); 
    printf("\n Sum of square = %f", sum);
    getch(); // Linux user - Remove this

    return 0;
}

9. Program to find the simple interest and total amount based on interest
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 /* Variables for principal amount, rate of interest, time duration,
  simple interest and total amount */
 int pa, ri, t, si,ta;
 
 // Inputting values
 printf("Enter principal amount:");
 scanf("%d", &pa);
 printf("Enter rate of interest:");
 scanf("%d", &ri);
 printf("Enter time duration:");
 scanf("%d", &t);
 
 // calculating simple interest and total amount
 si = (pa*ri*t)/100;
 ta = si + pa;
 
 // displaying simple interest and total amount
 printf("Simple interest: %d and total amount: %d", si,ta);
 
 getch(); // Linux user - Remove this

 return 0;
}

10. Program to enter 10 digit number and displaying it in the given format where first 3 digit refer to area code, next three digit to exchange code and remaining to number.Program to enter 10 digit number and displaying it in the given format where first 3 digit refer to area code, next three digit to exchange code and remaining to number.
/*
Input/output:
Enter 10 digit number: 9876543210
You entered: 9876543210
Area code: 987
Exchange code: 654
Number: 3210
The complete telephone number: (987)654-3210

*/
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 int a,b,c;
 
 // Inputting Telephone number
 printf("Enter ten digit telephone no.");
 scanf("%3d %3d %4d",&a,&b,&c);
 
 // Displaying telephone number in given format
 printf("You entered:%d%d%d\n",a,b,c);
 printf("Area code:%d\n",a);
 printf("Exchange code:%d\n",b);
 printf("Number:%d\n",c);
 printf("The complete telephone number:(%d)%d-%d",a,b,c);
 
 getch(); // Linux user - Remove this

 return 0;
}
C PROGRAMS : FUNDAMENTALS

C PROGRAMS : FUNDAMENTALS

C PROGRAMS : FUNDAMENTALS
1. A simple program for printing a line of text
#include<stdio.h>
#include<conio.h> // Linux user - Remove this
int main()
{
 printf("Hello this is my first C program");
 getch(); // Linux user - Remove this

 return 0;
}

2. Program to input integer number and display it
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
    // variable to take input
    int n;
    
    printf("Enter any number ");
    scanf("%d",&n);
    printf("Input number=%d",n);
    
    // holds the output screen defined in header file conio.h
    getch(); // Linux user - Remove this

    return 0;
    
}

3. Program to add, subtract, multiply and divide two numbers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
    int a, b;
    
    printf("Enter any two number ");
    scanf("%d",&a);
    scanf("%d",&b);
    printf("Sum=%d",a+b);
    printf("\nSubtracted value=%d",a-b);
    printf("\nMultiplied value=%d",a*b);
    printf("\n Division value=%d",a/b);
    getch(); // Linux user - Remove this

    return 0;
}

4. Program to subtract two numbers without using arithmetic operators ( - )
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
    int a, b, r;
    
    printf("Enter any two number ");
    scanf("%d",&a);
    scanf("%d",&b);
    r=a+(~b)+1;
    printf("Subtracted value=%d", r);
    getch(); // Linux user - Remove this
    return 0;
}


5. Program to find the greatest between 3 integers( numbers ) using ternary or conditional operator
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Variable for 3 integers and for storing greatest integer
 int a, b, c, g;
 
 // Inputting integers
 printf("Enter three integers:");
 scanf("%d %d %d",&a,&b,&c);
 
 // Determining the greatest integer using ternary or conditional operator
 g=(a>b && b>c) ? a : (b>c && b>a) ? b : (c>a && c>b) ? c : 0;
 
 // displaying greatest value
 if(g!=0)
 printf("Greagest integer: %d",g);
 else
 printf("None is greatest: Two integers have same value" );
 
 getch();// Linux user - Remove this

 return 0;
}
BASIC COMPONENT OF A C PROGRAM

BASIC COMPONENT OF A C PROGRAM

Now lets begin exploring the C programming language. Before we start executing programs in C Turbo C/C++ Compiler we must know some basic component of a C program.

#include - The #include is known as a preprocessor directive and is used to tell the C preprocessor to find the stdio file with extension .h. <stdio.h> stand for standard input output stream header file and contains information for printf, scanf etc.

main() - Exectuion of a a program starts from a main() function. It defines the point from where the execution of the program starts. Anything written between opening curly brace and ending curly brace of main is executed.

printf() - This is the standard way of producing output. The functionality of printf() is referenced in stdio.h by the C compiler, thus it always work in the same way.

scanf() - This is the standard way of taking input from user. The functionality of scanf() is also referenced in stdio.h by the C compiler.

comments: Comments are information given by the program to make a program readable and easy to understand. It reduces the complexity of a program. Anything written as comments is ignored by the compiler.

There are two ways of writing comments:

1. Single line comment

Syntax:
//Your comments here

example:
 // Hello this is my first C program

2. Multiline comment  

Syntax:
/* Your comment
Your comment*/

example: 
/* This is my first C program
and i am very excited about it */


SYNTAX:

Note : It should be noted that some compiler does not include header file "conio.h". It is also not included in compiler of Linux or any other Unix based operating system. So in case using it displays an error remove this and the function associated with it i.e getch().

#include<stdio.h> //This tells the compiler about the input/output functions such as printf(), scanf() 
#include<conio.h> //It is used for getch() function
int  main() // It is the entry point of a program
{ // Progam begins with this curly braces
printf("Congratulation you successfully run your  first program"); // to print in the output screen
getch(); // to hold the output screen
return 0; // tell the OS that the program exited without error
} // Program end with this curly braces

OUTPUT:

Congratulation you successfully run your  first program

EXPLANATION:

When we execute this program (press Ctrl+F9 to execute in Turbo C or F9 in Dev C++).

NOTE: If you want to execute this program in Dev C++ then there is a minor change. Replace void main() with int main() as Dev C++ compiler does not support void as return type for main(). Also replace getch() with return 0.

return 0 means that the program is terminated successfully and the compiler is returning back the control to the computer

Now on execution the program will display "Congratulation you successfully run your  first program" (without double quotes) as output on monitor. Any string you pass within double quotes through printf is sent to console output (monitor) i.e it is displayed as it is on the monitor.

PROGRAM EXAMPLE:

#include<stdio.h>
int main()
{
printf("%d", 5+6); // %d is conversion character for integer
return 0;
}

OUTPUT:

11

EXPLANATION:

The above program contains only one statement to be executed within main() function. As this statement contains 5+6 without quotes, it will add these two numbers and pass the result in integer ( as indicated by conversion character %d ) to console window i.e 11 is sent to the monitor. So in this case the output is 11.

So whenever we use double quotes, compiler just prints the message ignoring what has been include there but when no quotes are used, actual values are processed according to instructions and executed accordingly.
LOGIC BOOSTER AND BRAINSTORMING QUESTION

LOGIC BOOSTER AND BRAINSTORMING QUESTION

LOGIC BOOSTER AND BRAINSTORMING QUESTION

LOGIC BOOSTER

  1. When an operation is to be performed on two values ( may be of different types ), the type of smaller value will automatically be converted into larger one and the result will be in large one's type.
  2. char or short values will get converted into integer ( int ) type if involved in calculation part.
  3. float value is converted into double value:
  • If either operand is double, the other is converted to double and the result is double.
  • Otherwise, if either operand is long, the other is converted to long, and the result is long.
  • Otherwise, if either operand is unsigned, the other is converted to unsigned, and the result is unsigned.
  • Otherwise, the operand must be int.
For example: Suppose
c: char
d: double
i: int
u: unsigned
s: short int
l: long
f: float

 Expressions   Final data type                                               Explanation
       i + d
         double
 One value is of type int and second is of type double, so smaller one  
 (int) will be converted into double first ( without loss of information ) 
 and then the result type of addition of two double is double.
   20 * s +3             int First short will be converted into int. All values are of type int, so
 result is of type int.
    12.0 + c         double First "c" is converted into int then to double. Result will be of
 type double.    
    3 * i / 4            int As 3 and "i" are of typ int, so result of 3*i is of type int, then int
 divided by int is again int.
    3 * i / 4.0         double The result of 3 * i is of type int, then int divided by double is
 double.


  • You must use "&" ( ampersand ) with scanf to enter integer or character value ( while using format specifier "%d" or "%c" or "%f" ). Omitting it will make the program not work correctly.
  • You must not use "&" with printf while printing the values of any variable.
  • If you use comma between format specifiers in scanf, user must have to provide comma between two values to differentiate them.
  • All statements must be terminated with semicolon.
  • C is case sensitive - all keywords and standard library functions are in lowercase.


BRAINSTORMING QUESTIONS

1. How can we display the float number in exponential notation.
  • We can use the format specifier %e to print float value in exponential notation.
2. Is int a; a declaration or definition.
  • The declaration of a variable means how it works but it does not allocate memory to an identifier. Since int a; statement introduces type of "a" and also allocates memory, it becomes definition of variable "a".
3. The following code finds the average of three numbers:

#include<stdio.h>
#include<conio.h> // Linux user - Remove this
int main()
{
int a=10, b=20, c=30, avg;
avg= a+b+c/3;
printf("Average is: %d", avg);
getch(); // Linux user - Remove this
return 0;
}
Does the user get the correct answer?

  • No, because of following reasons:
  • the division operator have higher precedence than the addition operator. Hence expression is evaluated as "avg = a+b+(c/3);"
  • The division operator operates on integer operand. So the result will be in integer. When "c" is divided by 3, only integer part is returned and fraction part is removed.
4. Every statement in C is terminated by a semicolon. Can we write the following codes which the first statement inside main is terminated by comma instead of a semicolon. Will it work?

#include<stdio.h>
#include<conio.h> // Linux user - Remove this
int main()
{
printf("This is C "),
printf("Programming."); 
getch(); // Linux user - Remove this
return 0;
}

  • Yes, the code will work. The code has only one statement containing two expression separated by comma, i.e printf ("This is C ") and printf ("Programming."); As the expression separated by comma are evaluated in left to right order, printf("This is C ") is evaluated followed by printf("Programming."). Therefore, the output is: This is C Programming.
5. Will the following code work. The two expressions are separated by two commas.

#include<stdio.h>
#include<conio.h> 
// Linux user - Remove this
int main()
{
int x=10, , y=32;
printf("%d %d"), x, y);
getch();// Linux user - Remove this
return 0;
}

  • No. The compiler gives compile time error. This is because two comma operators cannot appear consecutively.
COMMON PROGRAMMING ERRORS - FUNDAMENTALS

COMMON PROGRAMMING ERRORS - FUNDAMENTALS

COMMON PROGRAMMING ERRORS - FUNDAMENTALS
  1. Not putting semicolon after C statements like printf() or scanf().
  2. Not including required header files.
  3. Parenthesis not matching in printf() or scanf().
  4. Not including double quotes in printf().
  5. Not putting comma between identifiers.
  6. Including space in identifiers.
  7. using wrong conversion character in scanf() or printf().
  8. Not including address of operator ( & ) in scanf().
  9. Using address of operator ( & ) in printf().
  10. Not giving precedence to expression wherever required like a + b / 2 is different from ( a + b ) / 2.