Powered By Blogger

Tuesday 16 April 2013

Some Basic Examples

Q. WAP to Enter 5 Numbers and display the Sum ?

 #include<stdio.h>
 main()
{
int val1,sum=0,i;
printf("Please Enter 5 Numbers:\n");
for(i=0;i<5;i++)
{
scanf("%d",&val1);
sum+=val1;
}
printf("Sum is: %d",sum);
}

Q. WAP to Reverse a three digit Number using simple Logic ?


#include<stdio.h>
main()
{
            int a;
            printf("Enter a three digit number\n");
            scanf("%d",&a);
            int p = a % 10;
            int y=  a / 10;
            int q = y % 10;
            int o = y / 10;
            int r = o % 10;
            int c =  p * 100 + q * 10 + r * 1;
            printf("%d",c);
}

Q. WAP to Find Sum of N Numbers ?

#include <stdio.h>
main()
{
    int N, sum = 0;
    printf("Program for sum of all numbers from 0 - N\n");
    printf("Enter N: ");
    scanf("%d", &N);
    sum = N * (N+1) / 2;
    printf("The sum of all numbers between 0 and %d is: %d", N, sum);
}

Q. WAP to Display the Table of any Number Entered by user ?

#include<stdio.h>
main()  
{
    int i,num;
    printf("Please Enter the Number:\n");
    scanf("%d",&num);
    printf("Table of  %d  is: \n",num);
    for(i=1;i<=10;i++)
    {
    printf("%d * %d = %d\n",num,i,num*i);    
    } 
}

Q. WAP to Reverse any digit Number ?

#include<stdio.h>
main ()
{
int no,r,res;
printf("Enter any value: ");
scanf("%d",&no);
r=res=0;
while(no>0)
{
r=no%10;
no=no/10;
res=(res*10)+r;
}
printf("\nReverse is %d",res);
}

Thursday 17 January 2013

Constructs Examples (Contd..)

------------
do...while
------------

#include<stdio.h>
using namespace std;
main()
{
 int a;
 do
 {
  printf("Enter a:\n");
        scanf("%d",&a);

 }
 while(a!=2);
}



----------
for loop
----------

#include<stdio.h>
using namespace std;
main()
{

 for(int a=1;a<=5;a++)
 {
  printf("\t \n%d\t\t",a*5);
 }
}



























Constructs Examples

--------
if..else
--------

#include<stdio.h>
using namespace std;
main()
{
 int a,b;
 a=2;
 b=3;
 if(a>b)
 {
  printf("a is greater than b\n");
 
 }
 else
 {
  printf("b is greater than a\n");
 
 }
}
 


---------------
switch..case
---------------

#include<stdio.h>
using namespace std;
main()
{
    int op;
    float a;
    float b;
    float result;
    while(1)
    {
    printf("Select the Arithmetic Operation: \n 1 - Addition\n 2 - Subtraction\n 3 - Multiplication\n 4 - Division\n \n \n");
    scanf("%d", &op);
    printf("Enter first number: ");
    scanf("%f", &a);
    printf("Enter second number: ");
    scanf("%f", &b);
   
    switch (op)
    {
        case 1:
            result = a + b;
            break;
        case 2:
            result = a - b;
            break;
        case 3:
            result = a * b;
            break;
        case 4:
            if (b == 0)
            {
                printf("Cannot divide by zero.");
            }
            result = a / b;
            break;
      
            default:
                {
                 printf("invalid choice");
                 break;
                }   
}
printf("The result is: %f\n",result);
printf("\n");
}
}


-------------
while loop
-------------

#include<stdio.h>
using namespace std;
main()
{
 char ch;
 printf("Enter your choice:\n");
 scanf("%c",&ch);
 while(ch!='y')
 {
 
  printf("No\n");
  printf("No\n");
  printf("No\n");
  printf("No\n");
  break;
 }
}

Tuesday 15 January 2013

Constructs

Constrcuts are used for specific purpose ,like if you want to include statemtnts like ,the number is greater or number is b greater or you can say ,if you want to check this i good or that is good or this is bad and that is good like statements .

Constructs are of two main types :
Conditional Constructs
Loop Constructs

Conditional Constructs are used when you specify conditions in you C Program .
They are :

      if...else
     switch...case

Syntax for if...else :-

if(condition)
{
statement to be executed;
}
else
{
statement to be executed;
}

Syntax for switch...case :-

switch(variable/condition)
{
case 1:
statement to be executed;
break;
case 2:
statement to be executed;
break;
.
.
.
.
.
default:
statement to be executed;
break;
}

*break statement is used to terminate the case statement and move the compiler to the outer area of the construct or to the given statement.
*default is used for an additional case to be executed ,in case of any unexpected condition occurs .

Loop Constructs are used if you want to repeat/ Iterate the program or a specified condition for several times .
They are :-

     while loop
     do..while loop
     for loop

 Syntax for while loop :-

while(Condition)
{
statement to be executed;
}

Syntax for do...while loop :-

do
{
statement to be executed;
}
while(Condition)

Difference between while loop & do...while loop :

while loop & do..while loop are almost same ,but the only major difference is that do..while loop checks the condition after the execution of body .

 Syntax for for loop :-

for(Initializing; condition; Increment /Decrements )
{
statement to be executed;
}

the first stage in for loop is initializing the variable ,then condition to be checked ,the what will happen if the specify given condition is true ,increment or Decrements .

Some Operators for Constructs :

Arithmetic Operators 
(+ ,- ,* ,/ ,%)

+ is used for addition
- is used for difference
* is used for multiplication
/ is used for division
% is used to find out remainder

Logical Operators
(&&, ||, !)

&& is AND Operator ,used to find out the whole condition is true or false
|| is OR Operator ,used to find out any condition is true or false
! is NOT Operator ,used to find out or you can say define Negative Result

Unary Operators
(++, --)

++ used to increment the value of any variable or an operand by 1
-- used to decrements the value of any variable or an operand by 1
    also applied on single operands .

Comparison Operators
(>, <, >= ,<= ,==, !=)

> is greater than
< is less than
>= is greater than equal to
<= is less than equal to
== equal equal to
!= not equal to

and many more ...

Monday 14 January 2013

I-P-O

I-P-O stands for Input-Process-Output ,each and every program in any programming language performs I-P-O operation ,I/O (Input-Output) is all we need in our C Program .
 
In C Language :
stdin is used for Input
stdout is used for Output
 
Input & Output takes place as a stream of character .
we can perform or use two types of ,mainly two types of Input & Output ,
 
1.Character-Based I/O
2.String-Based I/O
 
 
Character-based I/O includes :-
 
#getc()
#putc()
#getchar()
#putchar()
 
getc() : getc() is also a function/inbuilt function ,or you can say a keyword .getc() is used to accept a single character .example : getc(a); ,so this statement will accept a single character 'a' .
 
putc() : putc() is also a function/ inbuilt function ,putc() is used to generate an output or to display the accepted character on console window (Console window is the responsible window ,where you actually visualize you output of you C Program) .
 
Illustration :
 
#include<stdio.h>
main()
{
 char c;
 c=getc(stdin);
 putc(c,stdout);

 
so here we are declaring a character type variable c ,c=getc(stdin); statement is storing and accepting variable's value ,putc(c,stdout); is used to display the stored variable's value onto the console :
 

 
 
getchar() : is also used to accept a single character ,the only difference between getc() and getchar() is about parameters, parameters are the additional information we pass between (...) .
 
putchar() : is also used to display a single character ,parameters are bit different as compare to
 putc() .
 
Illustration :
 
#include<stdio.h>
main()
{
 char c;
 c=getchar();
 putchar(c);
 printf("\n");
}


 
 
you can see here that there is no need to specify stdin or stdout .so a different way to implement character-based I/O operations .

String-Based I/O includes :-

#gets()
#puts()

gets() : is used to accept a string (sequence of characters ) ,example : "Programming" .
puts() : is used to display the string

Illustration :
#include<stdio.h>
main()
{
 char c[20];
 puts("Enter Something :\n");
 gets(c);
 puts("You Entered: ");
 puts(c);
 printf("\n");
}


so here we are declaring a character type variable c which holds sequence of  20 character .
you can see here gets and puts used to accept and display strings .



check it out!

#include<stdio.h>
main()
{
printf("Hi, ");
disp_msg();
printf(" Enjoy Learning ");
}
disp_msg()
{
printf("Welcome to C Programming,");
}

What will be the output?
-Hi, Enjoy Learning Welcome to C Programming,

No, the above generated output is not correct ,when you execute this ,you'll see :
-Hi, Welcome to C Programming, Enjoy Learning

because compiler executes the statements step-by-step ,so it goes the printf("Hi, "), then disp_msg(),
disp_msg() having some information which is in disp_msg(){...} block, so compiler executes it first ,
disp_msg()
{
printf("Welcome to C Programming,");
}
then printf(" Enjoy Learning "), so the output will be :
Hi, Welcome to C Programming, Enjoy Learning

Functions

Functions as u have seen earlier ,used to transfer messages and data in our C Program. Functions contain all the information/ data. it can be single level function as well as multi-level function.
Single Level Function
Consider the following single-level function :

main()
{
printf("single-level function\n");
}

So, in the above illustration main is the only function available .

Multi-level Function
you can create multiple functions in your C Program ,or you can say whenever a user create a function by itself (user-created function) .
Consider this :

main()
{
-
-
-
}
cprog()
{
-
-
-
}
...

so, here you can see cprog() ,is a user0defined function ,added to the C code ,that's multi-level function's example .

There are some predefined Functions available in library :-

strlen() is used to find length of a given string
strcpy() is used to copy strings
strcmp() is used to compare strings
strcat() is used to concantinate strings

strlen() example: 

#include <stdio.h>
#include <string.h>

int main ()
{
  char szInput[256];
  printf ("Enter a sentence: ");
  gets (szInput);
  printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput));
  return 0;
}

 strcpy() example


#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  strcpy (str2,str1);
  strcpy (str3,"copy successful");
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;
}

strcat() example


#include <stdio.h>
#include <string.h>

int main ()
{
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;
}