Friday, 10 January 2014

C Program to Add digits of the number using single statement

Add Digits of the Number Using Single Statement :
#include<stdio.h>
void main()
{
int number=12354;
int sum=0;
for(;number > 0;sum+=number%10,number/=10);
printf("nSum of the Digits : %d",sum);
}
Output :
15
How ?
for(initialization ; conditional ; increment)
{
  //body
}
  • In ‘For Loop‘ Condition is first tested and then body is executed.
  • Carefully Look at Semicolon at the end of ‘For Loop’ , which tells us two Things -
    • For Loop is Bodyless.
    • Only Condition and Increment Statements will be executed.

C Program to Reverse the digits of a number in 3 Steps

C program to reverse the digits of a number ? [ In 3 Steps ]

Problem Statement : Reversing the digits of number without using mod (%) Operator ?

Prerequisite :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

void main()
{
 int num1, num2;
 char str[10];
 clrscr();

    printf("nEnte the Number : ");
    scanf("%d",&num1);

    sprintf(str,"%d",num1);

    strrev(str);

    num2 = atoi(str);

    printf("nReversed Number : ");
    printf("%dn",num2);
    getch();
}
Output :
Enter the Number : 123
Reversed Number : 321

Explain Logic :
Step 1 : Store Number in the Form of String
  • Sprintf function sends formatted output to string variable specified
  • Number will be stored in String variable “str”
sprintf(str,"%d",num1);
Step 2 : Reverse the String Using Strrev Function
  • Strrev will reverse String
  • eg “1234″ will be reversed as “4321″
strrev(str);
Step 3 : Convert String to Number
  • [A to I ] =  [ Alphabet to Integer ] = atoi
  • Atoi function Converts String to Integer
num2 = atoi(str);

C Program to Add reversed number with Original Number

C program to add reversed number with Original Number ?


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

void main()
{
int num1, num2;
char str[10];
clrscr();

printf("nEnte the Number : ");
scanf("%d",&num1);

sprintf(str,"%d",num1);

strrev(str);

num2 = atoi(str);

printf("nReversed Number + Original Number = %d ",num1 + num2 );
getch();
}
Output:
Ente the Number : 123Reversed Number + Original Number = 444

C Program to Demonstrate Printf inside Another Printf Statement

Printf inside printf in C : Example 1

#include<stdio.h>
#include<conio.h>
void main()
{
int num=1342;
clrscr();
printf("%d",printf("%d",printf("%d",num)));
getch();
}

Output :
134241

How ?
  1. Firstly Inner printf is executed which results in printing 1324 
  2. This Printf Returns total number of Digits i.e  4 and second inner printf will looks like
  3. printf("%d",printf("%d",4));
  4. It prints 4 and Returns the total number of digits i.e 1 (4 is single digit number )
  5. printf("%d",1);
  6. It prints simply 1 and output will looks like 132441

Rule :
Inner printf returns Length of string printed on screen to the outer printf

C Program to Demonstrate Nested Printf Statements

nested Printf statements : Example 1

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("%d",printf("abcdefghijklmnopqrstuvwxyz"));
getch();
}

Output :
abcdefghijklmnopqrstuvwxyz26

How ?
  1. “abcdefghijklmnopqrstuvwxyz” will be first Printed while executing inner printf
  2. Total Length of the “abcdefghijklmnopqrstuvwxyz” is 26
  3. So printf will return total length of string
  4. It returns 26 to outer printf
  5. This outer printf will print 26

C Program to Print Hello word without using semicolon

Part 1 : Printf Hello word in C without using semicolon [only ones ]

#include<stdio.h>
void main()
{
   if(printf("Hello"))
   {
   }
}

Output :
Hello

Part 2 : Printf Hello word in C without using semicolon [infinite times]

#include<stdio.h>
void main()
{
   while(printf("Hello"))
   {
   }
}

Part 3 : Printf Hello [Using Switch]

#include<stdio.h>
void main()
{
   switch(printf("Hello"))
   {
   }
}

Part 4 : Using Else-if Ladder

#include<stdio.h>
void main()
{
   if(printf(""))
      {
      }
   else if (printf("Hello"))
      {
      }
   else
      {
      }
}

Part 5 : Printf Hello [Using While and Not]

#include<stdio.h>
void main()
{
    while(!printf("Hello"))
    {
    }
}

Part 6 : Using #define

#include<stdio.h>
#define PRINT printf("Hello")
void main()
{
    if(PRINT)
    {
    }
}

C Program to Accept Paragraph using scanf

Accept Paragraph using scanf in C
#include<stdio.h>
void main()
{
char para[100];
printf("Enter Paragraph : ");
scanf("%[^t]",para);
printf("%s",para);
}

Output :[Press Tab to Stop Accepting Characters ]
Enter Paragraph : C Programming is very easy to understand
C
Language
is backbone of
C++
Language

How ?
scanf("%[^t]",para);
  1. Here scanf will accept Characters entered with spaces.
  2. It also accepts the Words , new line characters .
  3. [^t]  represent that all characters are accepted except tab(t) , whenever t will encountered then the process of accepting characters will be terminated.
Drawbacks :
  1. Paragraph Size cannot be estimated at Compile Time
  2. It’s vulnerable to buffer overflows.
How to Specify Maximum Size to Avoid Overflow ?
//------------------------------------
// Accepts only 100 Characters
//------------------------------------
scanf("%100[^t]",para);