Wednesday, February 19, 2014

WORKING WITH IF CONDITIONS

C Programming - If Statement

Prepared By C.Saravana Kumar

Click the following Links to go Solutions of Programs :
Example : 1.1 To Check Eligible to Vote
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("\nEnter Your Age : ");
scanf("%d",&age);
if(age>=18)
   printf("\nAge is greater than or equal to 18");
getch();
}
Output : 1
Enter Your Age : 30
Age is greater than or equal to 18
Output : 2 (No Result)
Enter Your Age : 10

Example : 1.2 To Check Eligible to Vote
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("\nEnter Your Age : ");
scanf("%d",&age);
if(age>=18)
{
   printf("\nAge is greater than or equal to 18");
   printf("\nThis Age Eligible to Vote");
}
getch();
}
Output : 1
Enter Your Age : 30
Age is greater than or equal to 18
This Age Eligible to Vote
Output : 2 (No Result)
Enter Your Age : 10

Example : 1.3 To Check Eligible to Vote
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("\nEnter Your Age : ");
scanf("%d",&age);
if(age>=18)
   printf("\nEligible to Vote");
else
   printf("\nNot Eligible to Vote");
getch();
}
Output : 1
Enter Your Age : 30
Age is greater than or equal to 18
Eligible to Vote
Output : 2 (No Result)
Enter Your Age : 10
Not Eligible to vote

Example : 2.1 Biggest of Two Numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\nEnter Value of A ");
scanf("%d",&a);

printf("\nEnter Value of B ");
scanf("%d",&b);
if(a>b)
   printf("\n A is Big");
else
   printf("\n B is Big");
getch();
}
Output : 1
Enter value of A 10
Enter value of B 20
A is big
Output : 2 (Result is Wrong - To Fix This Problem - See Next Example 2.2)
Enter value of A 10
Enter value of B 10
B is big

Example : 2.2 Biggest of Two Numbers (Change below Code in Previous Code ( Ex 2.1 )
if(a>b)
   printf("\nA is Big");
if(b>a)
   printf("\nB is Big");
[or]
if(a>b)
   printf("\nA is Big");
else if(b>a)
   printf("\nB is Big");
Output : 1
Enter value of A 10
Enter value of b 20
B is big
Output : 2 (To print message A & B are equal - See the below Example 2.3
Enter value of A 10
Enter value of B 10
 

Example : 2.3 Biggest of Two Numbers (Change below Code in Previous Code (Ex 2.1 )
if(a>b)
   printf("\nA is Big");
if(b>a)
   printf("\nB is Big");
else if(a==b)
   printf("\nA & B are Equal");
Output : 1
Enter value of A 10
Enter value of b 20
B is big
Output : 2
Enter value of A 10
Enter value of B 10
A & B are Equal

Example : 3.1 Biggest of Three Numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nEnter Value of A ");
scanf("%d",&a);
printf("\nEnter Value of B ");
scanf("%d",&b);
printf("\nEnter Value of C ");
scanf("%d",&c);
if(a>b && a>c)
   printf("\n%d is Big",a);
else if (b>a && b>c)
   printf("\n%d is Big",b);
else if (c>a && c>b)
   printf("\n%d is Big",c);
getch();
}
Output : 1
Enter Value of A 2
Enter Value of B 3
Enter Value of C 4
4 is Big
Output : 2 (A & C Values are less than B.See the Next Output )
Enter Value of A 3
Enter Value of B 4
Enter Value of C 3
4 is Big
Output : 3 (A & C Values are greater than B. To print message A & C is big - See [Ex 3.2])
Enter Value of A 3
Enter Value of B 2
Enter Value of C 3
 
Output : 4 (To print message 3 values are Equal - See [Ex 3.2])
Enter Value of A 3
Enter Value of B 3
Enter Value of C 3
 

Example : 3.2 Biggest of Three Numbers (Change below code in Previous Code[(Ex 3.1 ] )
if(a>b && a>c)
   printf("\n%d is Big",a);
else if (b>a && b>c)
   printf("\n%d is Big",b);
else if (c>a && c>b)
   printf("\n%d is Big",c);
else
   printf("\n3 Values are Equal");
[or]
if(a>b && a>c)
   printf("\n%d is Big",a);
else if (b>a && b>c)
   printf("\n%d is Big",b);
else if (c>a && c>b)
   printf("\n%d is Big",c);
else if(a==b && b==c)
   printf("\n3 Values are Equal");
Output : 1
Enter Value of A 3
Enter Value of B 3
Enter Value of C 3
3 Values are Equal
Output : 2 (A & C Values are greater than B. To print message A & C is big - See [Ex 3.3])
Enter Value of A 3
Enter Value of B 2
Enter Value of C 3
 

Example : 3.3 Biggest of Three Numbers (Finally)
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nEnter Value of A ");
scanf("%d",&a);
printf("\nEnter Value of B ");
scanf("%d",&b);
printf("\nEnter Value of C ");
scanf("%d",&c);
if(a==c && b<a)
    printf("\nA & C Values are Big ");
else if(a==b && c<a)
   printf("\nA & B Values are Big");
else if(b==c && a<b)
   printf("\nB & C Values are Big");
else if(a==b && b==c)
   printf("\n3 Values are Equal.");
else if(a>b && a>c)
   printf("\nA (%d) is Big",a);
else if (b>a && b>c)
   printf("\nB (%d) is Big",b);
else if (c>a && c>b)
   printf("\nC (%d) is Big",c);
getch();
}
Output : 1
Enter Value of A 2
Enter Value of B 3
Enter Value of C 2
B (3) is Big
Output : 2
Enter Value of A 3
Enter Value of B 2
Enter Value of C 3
A & C values are Big

Example : 3.1 Biggest of Three Numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nEnter Value of A ");
scanf("%d",&a);
printf("\nEnter Value of B ");
scanf("%d",&b);
printf("\nEnter Value of C ");
scanf("%d",&c);
if(a>b && a>c)
   printf("\n%d is Big",a);
else if (b>a && b>c)
   printf("\n%d is Big",b);
else if (c>a && c>b)
   printf("\n%d is Big",c);
getch();
}
Output : 1
Enter Value of A 2
Enter Value of B 3
Enter Value of C 4
4 is Big
Output : 2 (A & C Values are less than B.See the Next Output )
Enter Value of A 3
Enter Value of B 4
Enter Value of C 3
4 is Big
Output : 3 (A & C Values are greater than B. To print message A & C is big - See [Ex 3.2])
Enter Value of A 3
Enter Value of B 2
Enter Value of C 3
 
Output : 4 (To print message 3 values are Equal - See [Ex 3.2])
Enter Value of A 3
Enter Value of B 3
Enter Value of C 3
 

Example : 3.2 Biggest of Three Numbers (Change below code in Previous Code[(Ex 3.1 ] )
if(a>b && a>c)
   printf("\n%d is Big",a);
else if (b>a && b>c)
   printf("\n%d is Big",b);
else if (c>a && c>b)
   printf("\n%d is Big",c);
else
   printf("\n3 Values are Equal");
[or]
if(a>b && a>c)
   printf("\n%d is Big",a);
else if (b>a && b>c)
   printf("\n%d is Big",b);
else if (c>a && c>b)
   printf("\n%d is Big",c);
else if(a==b && b==c)
   printf("\n3 Values are Equal");
Output : 1
Enter Value of A 3
Enter Value of B 3
Enter Value of C 3
3 Values are Equal
Output : 2 (A & C Values are greater than B. To print message A & C is big - See [Ex 3.3])
Enter Value of A 3
Enter Value of B 2
Enter Value of C 3
 

5.1 To check given number is Even or Odd number
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\nEnter any Value ");
scanf("%d",&n);
if(n%2==0)
   printf("\n%d is Even Number ",n);
else if (n%2==1)
   printf("\n%d is Odd Number",n);
getch();
}
Output : 1
Enter any Value 3
3 is Odd Number

6.1 To Calculate the absolute value of an integer
#include<stdio.h>
#include<conio.h>
void main()
{
int number;
clrscr();
printf ("Enter any Number ");
scanf ("%d", &number);
if ( number < 0 )    number= - number;

printf ("\n The absolute value is %d",number);
getch();
}
Output : 1
Enter any Number 2
The absolute value is 2
Output : 2
Enter any Number -4
The absolute value is 4

Ex:7.1 Leap Year or Not
#include<stdio.h>
#include<conio.h>
void main()
{
int year,reminder;
clrscr();
printf ("Enter the Year ");
scanf ("%d",&year);
//if(year%4==0 && year%4!=0 || year % 400==0)
//if((year%4==0 && year%4!=0) || year % 400==0)
if(reminder==0)
   printf("\nIt's a Leap Year");
else
   printf("\nIt's not a Leap Year");
getch();
}
Output : 1
Enter The year 2010
It's a Leap Year.

Ex:8.1 to Categorizes a Single Character that is entered at the keyboard.
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr()
printf("\nEnter a Single Character > ");
scanf("%c",&c);
if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <='Z') )    printf ("It's an alphabetic character.\n"); else if (c >= '0' && c <= '9' )    printf ("It's a digit.\n"); else    printf ("It's a special character.\n");

getch();
}
Output : 1
Enter a Single Character > a
It's an alphabetic character.
Output : 2
Enter a Single Character > 2
It's a digit.
Output : 3
Enter a Single Character > &
It's a special character.

Ex:9.1 To Evaluate simple expression of the following form - I : Number operator Number
#include<stdio.h>
#include<conio.h>
void main()
{
float number1,number2;
char operator;
printf ("\nType in your expression : ");
scanf("%f %c %f",&number1,operator,&number2);
if ( operator == '+' )
   printf ("\n%.2f ", number1+number2);
else if ( operator == '-' )
   printf ("\n%.2f ", number1-number2);
else if ( operator == '*' )
   printf ("\n%.2f ", number1*number2);
else if ( operator == '/' )
   printf ("\n%.2f ", number1/number2);
getch();
}
Output : 1
Type in your expression : 10+20
30.00
Output : 2
Type in your expression : 123.45*121
14937.45
Output : 3 (To Fix following Output See Next Example )
Type in your expression : 5/0
(Program terminated.Because Division by Zero Error)

Ex:9.2 To Evaluate simple expression of the following form -II : Number operator Number
#include<stdio.h>
#include<conio.h>
void main()
{
float number1,number2;
char operator;
printf ("\nType in your expression : ");
scanf("%f %c %f",&number1,operator,&number2);
if ( operator == '+' )
   printf ("\n%.2f ", number1+number2);
else if ( operator == '-' )
   printf ("\n%.2f ", number1-number2);
else if ( operator == '*' )
   printf ("\n%.2f ", number1*number2);
else if ( operator == '/' )
else if ( operator == '/' )
{
  if(number2==0)
     printf("\nCannot divided by zero ");
  else
     printf ("\n%.2f ", number1/number2);
}
else
   printf("\nUnknown Operator.);
getch();
}
Output : 1
Type in your expression : 5/0
Cannot divided by zero
Output : 2
Type in your expression : 5$0
Unknown Operator

Thank You...




C OPERATORS



C Programming - Operators - Part 1


Prepared By C.SaravanaKumar


Example : 1 Modulo (%) Operator;

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
a=7;
b=2;
c=a%b;
printf("Ans=%d",c);
c=a/b;
printf("Ans=%d",c);
getch();
}

Output :

Ans=1
Ans=3




Example : 2 Arithmetic Operations (Integer Values)

#include<stdio.h>
#include<conio.h>
void main()
{
int a=7,b=2;
clrscr();
printf("\nSum of A,B=%d",a+b);
printf("\nSubtraction of A,B=%d",a-b);
printf("\nMultiplication of A,B=%d",a*b);
printf("\nDivision of A,B=%d",a/b);
printf("\nModulo Division of A,B=%d",a%b);
printf("\nAll Arithmetic Operations of A,B =%d",((a+b)*(a-b)/5)%2);
getch();
}


Output :
Sum of A,B=9
Subtraction of A,B=5
Multiplication of A,B=14
Division of A,B=3
Modulo Division of A,B=1
All Arithmatic Operations of A,B =1



Example : 3 Arithmetic Operations (Float Values)


#include<stdio.h>
#include<conio.h>
void main()
{
float a=7,b=2;
clrscr();
printf("\nSum of A,B=%f",a+b);
printf("\nSum of A,B=%.2f",a+b);
printf("\nSubtraction of A,B=%.2f",a-b);
printf("\nMultiplication of A,B=%.2f",a*b);
printf("\nDivision of A,B=%.2f",a/b);

/* This line not worked --------------

printf("\nModulo Division of A,B=%.2f",a%b);

-----------------------------------*/

printf("\nAll Arithmatic Operations of A,B =%.2f",(a+b)*(a-b)/5);
getch();
}

Output :
Sum of A,B=9.000000
Sum of A,B=9.00
Subtraction of A,B=5.00
Multiplication of A,B=14.00
Division of A,B=3.50
All Arithmatic Operations of A,B =9.00



Example : 4 Arithmetic Operation (With Using Variable)

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum,sub,mult,div,remind;
clrscr();
a=10;
b=3;
sum=a+b;
sub=a-b;
mult=a*b;
div=a/b;
remind=a%b;
printf("\nSum = %d",sum);
printf("\nSubtraction = %d",sub);
printf("\nMultiplication = %d",mult);
printf("\nDivision=%d",div);
printf("\nModulo Division = %d",remind);
getch();
}
Output :
Sum = 13
Subtraction = 7
Multiplication = 30
Division=3
Modulo Division = 1







Example : 5 Addition Operator (Character Variable)

#include<stdio.h>
#include<conio.h>
void main()
{
char c1,c2;
clrscr();
c1='a';
c2='b';
printf("\n%d",c1); /* It Prints ascii value of a */
printf("\n%d",c2);
printf("\n%d",c1+c2);
getch();
}
Output :
97
98
195




Example : 6 To get student mark with out scanf function-I


#include<stdio.h>
#include<conio.h>
void main()
{
float m1,m2,m3;
clrscr();
m1=80;
m2=90;
m3=100;
printf("\nSum of Marks = %.2f",m1+m2+m3);
printf("\nAverage of Marks = %.2f",(m1+m2+m3)/3);
getch();
}


Output :
Sum of Marks = 270.00
Average of Marks = 90.00





Example : 7 To get student mark with out scanf function-II


#include<stdio.h>
#include<conio.h>
void main()
{
float m1,m2,m3,tot,avrg;
clrscr();
m1=80;
m2=90;
m3=100;
tot=m1+m2+m3;
avrg=tot/3;
printf("\nSum of Marks = %.2f",tot);
printf("\nAverage of Marks = %.2f",avrg);
getch();
}


Output :
Sum of Marks = 270.00
Average of Marks = 90.00





Example : 8 To get student mark with scanf function-III


#include<stdio.h>
#include<conio.h>
void main()
{
float m1,m2,m3,tot,avrg;
clrscr();
printf("\nEnter Mark 1=");
scanf("%f",&m1);
printf("\nEnter Mark 2=");
scanf("%f",&m2);
printf("\nEnter Mark 3=");
scanf("%f",&m3);
tot=m1+m2+m3;
avrg=tot/3;
printf("\nSum of Marks = %.2f",tot);
printf("\nAverage of Marks = %.2f",avrg);
getch();
}


Output :
Enter Mark 1=100
Enter Mark 2=90
Enter Mark 3=100

Sum of Marks = 290.00
Average of Marks = 96.67




Example : 9 Area of Circle :


#include<stdio.h>
#include<conio.h>
void main()
{
float radius,area;
clrscr();
printf("\nEnter Radius of Circle : ");
scanf("%f",&radius);
area=3.14*radius*radius;
printf("\nArea of Circle = %.2f",area);
getch();
}


Output :
EEnter Radius of Circle : 2
Area of Circle = 12.56





Example : 10 Salary Calculation :

#include<stdio.h>
#include<conio.h>
void main()
{
float salary,allowance,deduction;
clrscr();
printf("\nEnter Ganesh's Basic Salary = ");
scanf("%f",&salary);
allowance=salary*(10.0/100); /* 10% of Basic Salary */
deduction=salary*(20.0/100); /* 3% of Basic Salary */
printf("\nAllowance of Salary Rs.=%.2f",allowance);
printf("\nDeduction of Salary Rs.=%.2f",deduction);
printf("\nNet Salary = %.2f",salary+allowance-deduction);
getch();
}
Output : 1
Enter Ganesh's Basic Salary = 2000

Allowance of Salary Rs.=200.00
Deduction of Salary Rs.=400.00
Net Salary = 1800.00




Example : 11 Conversion kilometer to Centimeter,feet,inches,meter.


#include<stdio.h>
#include<conio.h>
void main()
{
float km;
clrscr();
printf("Enter Kilometer Value = ");
scanf("%f",&km);
printf("\n%.2f Kilo Meter = %.2f Centimeter",km,100000*km);
printf("\n%.2f Kilo Meter = %.2f Feet",km,km*3280.839895);
printf("\n%.2f Kilo Meter = %.2f Inches",km,km*39370.07874);
printf("\n%.2f Kilo Meter = %.2f Meter",km,km*1000);
getch();
}
/*
Note :
1 Km = 1000 meter
1 Km = 3280.839 895 feet
1 kilometer = 39370.078 74 inch
1 kilometer = 100000 centimeter
*/
Output :
Enter Kilometer Value = 3

3.00 Kilo Meter = 300000.00 Centimeter
3.00 Kilo Meter = 9842.52 Feet
3.00 Kilo Meter = 118110.24 Inches
3.00 Kilo Meter = 3000.00 Meter



Example : 12 Fahrenheit and Celsius temperature conversion :


#include<stdio.h>
#include<conio.h>
void main()
{
float fh,cel;
clrscr();
printf("\nEnter Faherenheit Value = ");
scanf("%f",&fh);
cel= ((fh-32)/9.0 )*5;
printf("\nCelsius Value = %.2f",cel);
getch();
}

/* Note :

To convert Fahrenheit temperatures into Celsius:

* Begin by subtracting 32 from the Fahrenheit number.
* Divide the answer by 9.
* Then multiply that answer by 5.
*/


Output :
Enter Faherenheit Value = 50

Celsius Value = 10.00




Example : 13 Write a program to interchange values


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,t;
clrscr();
printf("\nEnter First Value = ");
scanf("%d",&a);
printf("\nEnter Second Value = ");
scanf("%d",&b);
printf("\nBefore Swap a = %d,b=%d",a,b);
t=b;
b=a;
a=t;
printf("\nAfter Swap a=%d,b=%d",a,b);
getch();
}


Output :
Enter First Value = 2
Enter Second Value = 3

Before Swap a = 2,b=3
After Swap a=3,b=2





Example : 14 To Sum of Digits (3 Digit Number)


#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=0;
clrscr();
printf("\nEnter Three Digit Value");
scanf("%d",&n);
s=s+(n%10);
n=n/10;
s=s+(n%10);
n=n/10;
s=s+(n%10);
printf("\nSum of Digits = %d",s);
getch();
}


Output :
Enter Three Digit Value 123
Sum of Digits = 6



C LANGUAGE - PART 1 EXAMPLES




C Programming - Fundamentals

Prepared By C.SaravanaKumar




Example : 1 Write a Program to print "Hello"

#include<stdio.h>
#include<conio.h>
void main()
{
/*My First Program - Comment Line */
clrscr();
printf("Hello");   
getch();
}

Output :

Hello




Example : 2
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hello\nWelcome");
printf("\nGoldenjrsinfo");
printf("-My Web");
getch();
}
Output :
Hello
Welcome
Goldenjrsinfo-MyWeb






Example : 3
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\nHello 12");    
printf("\n%d");       
printf("\n%d",100);
printf("\nA=%d",100);
printf("\nA=%d+10",100);
printf("\nA=%d",100+10);
getch();
}
Output :
Hello 12
0
100
A=100
A=100+10
A=110





Example : 4 Integer & Float Conversions (5/2)
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\n1-> %d",5/2);
printf("\n2-> 5/2 = %d",5/2);           

/* printf("\n5/2 = %f",5/2); This Line is Error */

printf("\n3-> 5.0/2 = %d",5.0/2);   
printf("\n4-> 5/0/2 = %f",5.0/2);     
printf("\n5-> 5.0/2 = %.2f",5.0/2);   
printf("\n6-> 5/2.0 = %.2f",5/2.0);    
printf("\n7-> 5.0/2.0=%.2f",5.0/2.0);
getch();
}
Output :
1-> 2
2-> 5/2 = 2
3-> 5.0/2 = 0
4-> 5/0/2 = 2.500000
5-> 5.0/2 = 2.50
6-> 5/2.0 = 2.50
7-> 5.0/2.0=2.50





Example : 5 Floating Point Error....
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\n5/2 = %f",5/2);
/*if Error found this program replace %f into %d"*/
getch();
}
Output :(Press ALT+F9 to compile In Turboc & Press ALT+F5 to see below Message)
printf : floating point formats not linked
Abnormal program termination





Example : 6 Variables.....


#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("\n%d",10);
printf("\n%d (Garbage Value)",a);
getch();
}

Output :
10
-28711  (Garbage Value)





Example : 7 Variables.....


#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
a=10;
printf("\n%d ",a);
printf("\nValue = %d",a);
printf("\nGiven Value = %d",a);
getch();
}

Output :
10
Value = 10
Given Value=10




Example : 8 Integer Variable with scanf


#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
/* a=10;*/
scanf("%d",a);
printf("\n%d ",a);
printf("\nValue = %d",a);
getch();
}

Output :if You Type 20 from Keyboard
20
Press Enter...
20
Value = 20
Finally...You can see Output window..
20
20
Value = 20




Example : 9 Integer Variable with scanf


#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("\nEnter Any Value = ");
scanf("%d",a);
printf("\nEntered Value = %d",a);
getch();
}

Output :
Enter Any Value =
if You Type 20 from Keyboard
Enter Any Value = 20
Press Enter...
Entered Value = 20
Finally...You can see Output window..
Enter Any Value =20
Entered Value = 20




Example : 10 Working With scanf Function

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
scanf("%d %d",&a,&b);
printf("\n%d",a);
printf("\n%d",b);
getch();
}
 Output : 1
if You type 10 from Keyboard
10
Press Enter > again please type 20 from Keyboard
10
20
Finally...You can see Output window..
10
20
10
20


Output : 2
press 10 from Keyboard > press spacebar (Don't Press Enter) > Press 20 from Spacebar
10 20
Press Enter >
10 20
Finally...You can see Output window..
10 20
10
20




Example : 11 Working With Scanf ..


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\nEnter  a Value = ");
scanf("%d",&a);
printf("\nEnter  b Value = ");
scanf("%d",&b);
printf("\nA Value = %d",a);
printf("\nB Value = %d",b);
printf("\nA+B Value = %d",a+b);
printf("\nA+10 Value = %d",a+10);
getch();
}

Output :
Enter a Value =
if You Type 20 from Keyboard
Enter a Value = 10
Press Enter...
Enter a Value = 10
Enter b Value = 10
Press Enter...
Finally...You can see Output window..
Enter a Value = 10
Enter b Value = 20
A Value =10
B Value= 20
A+B Value = 30
A+10 Value = 20




Example : 12 Character Variable Declaration


#include<stdio.h>
#include<conio.h>
void main()
{
char ans;
clrscr();
printf("\n ans = %c",ans);
ans=' ';
printf("\n ans= %c",ans);
printf("\n ans= %d",ans); /*Returns ascii value of Space */
ans='Y';
printf("\n ans = %c",ans);
getch();
}

Output :
ans = Å
ans=
ans= 32
ans = Y




Example : 13 String Variable With Scanf Function


#include<stdio.h>
#include<conio.h>
void main()
{
char ans;
char myname[15];
clrscr();
printf("\nEnter Any Character => ");
scanf("%c",&ans);
printf("\nEnter Any Name(String) => ");
scanf("%s",&myname);
printf("\nGiven Character => %c",ans);
printf("\nGiven Name => %s",myname);
getch();
}

Output :
Enter Any Character => M

Enter Any Name(String) => Manoj Kumar

Given Character => M
Given Name => Manoj




Example : 14 String Variable With gets Function


#include<stdio.h>
#include<conio.h>
void main()
{
char ans;
char myname[15];
clrscr();

printf("\nEnter Any Name => ");
scanf("%s",&myname);
printf("\nGiven Name Using Scanf => %s",myname);

/*
Please add fflush(stdin) before gets function
If Omitted this line,You will get wrong output
If any doubt,remove the fflush(stdid); and run again
*/

printf("\nEnter Any Name => ");
fflush(stdin);
gets(myname);

printf("\nGiven Name Using gets => %s",myname);
getch();
}

Output :
Enter Any Name => Vijay Antony

Given Name Using Scanf => Vijay
Enter Any Name => Vijaya Raj

Given Name Using gets => Vijaya Raj




Example : 15 Combine all variables (Emp Info)

#include<stdio.h>
#include<conio.h>
void main()
{

int empno;
char empname[20];
float salary;
clrscr();
printf("\nGive the Employee Details");

printf("\nEmployee ID = ");
scanf("%d",&empno);

printf("\nEmployee Name =");
fflush(stdin);
gets(empname);

printf("\nSalary = ");
scanf("%f",&salary);

printf("\nGiven Emp Details ");
printf("\nEmp ID=%d , Empname= %s,Salary = %.2f",empno,empname,salary);

getch();
}

Output :
Give the Employee Details
Employee ID = 101

Employee Name =Rakesh Sharma

Salary = 24000

Given Emp Details
Emp ID=101 , Empname= Rakesh Sharma,Salary = 24000.00






Example : 16 print employee information with gotoxy function (Read Carefully)
/*Author : C.Saravana Kumar */
#include<stdio.h>
#include<conio.h>
void main()
{

int empno;
char empname[20];
float salary;
clrscr();

/*Syntax of gotoxy function is > gotoxy(Xposition,Yposition) */
/*
-------------------------------------- (x) |
|            .(10,1)
|
|
|             .(10,40)          .(60,40)
(y)

(10,1) Means > gotoxy(10,1);
*/
gotoxy(2,2);printf("Employee ID ");
gotoxy(25,2);scanf("%d",&empno);

gotoxy(2,4);printf("Employee Name ");
gotoxy(25,4);fflush(stdin);gets(empname);

gotoxy(2,6);printf("Salary ");
gotoxy(25,6);scanf("%f",&salary);

gotoxy(2,8);printf("\nGiven Emp Details ");
gotoxy(2,11);printf("Emp No");
gotoxy(9,11);printf("Emp Name");
gotoxy(32,11);printf("Salary");

gotoxy(2,13);printf("%d",empno);
gotoxy(9,13);printf("%s",empname);
gotoxy(32,13);printf("%.2f",salary);

getch();
}

Output :
Employee ID                101

Employee Name           Tamil Selvan

Salary                         45000

Given Emp Details

Emp No   Emp Name                             Salary

101        Tamil Selvan                           45000.00




WORKING WITH IF CONDITIONS

C Programming - If Statement Prepared By C.Saravana Kumar Click the following Links to go Solutions of Programs : 1.1 To Check Eligible ...