You are on page 1of 11

/*five digit number enter by keyboard calculate the sum of its digits

Let us C----Chap=1----Page=35*/
#include<conio.h>
#include<stdio.h>
main()
{
int num,one,two,three,four,five,total;
int d1,d2,d3,d4,d5;
clrscr();
printf("Enter a five digit number=");
scanf("%d",&num);
d1=num/10000;
one=num%10000;
d2=one/1000;
two=one%1000;
d3=two/100;
three=two%100;
d4=three/10;
four=three%10;
d5=four/1;
total=d1+d2+d3+d4+d5;
printf("Total=%d",total);
getch();
}
Control Flow
Decisions if then else
More decisions switch
Loops while, do while, for
Keyword break
Keyword continue
Decisions if then else
Parentheses surround the test: -surround the condition with parentheses. These are mandatory.
One statement becomes the then part
If more are required, braces must be used:- if many statements are to be executed ,group the statements into a block.

if(if>0)
printf(a is positive);

if(if>0){
printf(a is positive);
i=-i;
}
A semicolon after the condition forms a do nothing statement.

If then else
An optional else may be added
One statement by default, if more are required, braces must be used
if(if>0)
printf(a is positive);
else
printf(a is negative);
Optionally an else statement, which is executed if the condition is false

Nesting ifs
Else associates with the nearest if:-Potential ambiguity with nested if then else statements. This arises in trying to
determine where an else clause belongs

int i=100;
if(i>0)
if(i>1000)
printf(i is big);
else
printf(i is reasonable);

int i=-20;
if(i>0){
if(i>1000)
printf(i is big);
}else
printf(i is reasonable);

if ((time>=0.)&&(time<12.)) printf(good morning);


else if ((time>-12.)&&(time<18.)) printf(good afternoon);
else if ((time>-18.)&&(time<24.)) printf(good evening);
else printf(time is out of range);

*Assume that the USA uses the following income tax code formula for their
annual income: //////////
5000 of income 0% tax // AJAY //
10000 of income 10% tax //////////
20000 of income 15% tax
above 35000 of income 20% tax
for example earning of 38,000 annually would owe
5000*0.0+10000*0.10+20000*0.15+3000*0.20=4600*/
#include<conio.h> next=income-20000;
#include<stdio.h> tax=(5000*0.00)+(10000*0.10)+(next*0.15);
main() printf("\nTax=%f",tax);
{ }
float income,next,tax; else if(income>=10000)
float limit=35000; {
clrscr(); next=income-10000;
printf("Enter your Annual Salary="); tax=(5000*0.00)+(next*0.10);
scanf("%f",&income); printf("\nTax=%f",tax);
//next=income-limit; }
if(income>limit) else if(income>=5000)
{ {
next=income-limit; next=income-5000;
tax=(5000*0.00)+(10000*0.10)+(20000*0.15)+(next*0.20); tax=5000*0.00;
printf("\nTax=%f",tax); printf("\nTax=%f",tax);
} }
else if(income>=20000) getch();
{ }

/* Generate the divisors of a given integer */


#include<conio.h> {
#include<stdio.h> if((num%i)==0)
main() {
{ printf("\nDivisor of num %d is %d",num,i);
int div,num,i; //break;
clrscr(); }
printf("Enter any number="); }
scanf("%d",&num); getch();
for(i=2;i<=num;++i) }
/*Find all the armstrong number in the range of 0 to 999
An Armstrong number of three digit in an integer such that the sum
of the cubes of its digits is equal to the number itself*/
#include<stdio.h> {
#include<conio.h> printf("\nArmstrong number is=%d",ans);
main() }
{ }
int num,num1,num2,num4,num3,ans; else
clrscr(); {
for(num=0;num<=999;num++) num1=num/10;
{ num2=num%10;
if((num>100)&&(num<999)) ans=(num1*num1*num1)+(num2*num2*num2);
{ if(ans==num)
num1=num/100; {
num2=num%100; printf("\nArmstrong number is=%d",num);
num3=num2/10; }
num4=num2%10; }
ans=(num1*num1*num1)+(num3*num3*num3)+ }
(num4*num4*num4); getch();
if(ans==num) }

/*Check whether a given number is perfect number of not


Perfect number is equal to the sum of all of its positive divisors.
For example 6 is perfect number, because 1,2 and 3 are its proper positive divisors and 1+2+3=6*/
#include<stdio.h> }
#include<conio.h> }
void main() if(sum==num)
{ {
int num,count,sum=0,ans; printf("\nNumber is Perfect Number");
clrscr(); }
printf("Enter a number="); else
scanf("%d",&num); {
for(count=1;count<num;count++) printf("Number is not Perfect Number");
{ }
if((num%count)==0) getch();
{ }
sum+=count;
/*Check whether given two number is Amicable numbers or not
Amicable number are two numbers so related that the sum of the proper
divisors of the one is equal to the other
example (220,284)*/
#include<stdio.h> for(count=1;count<num2;count++)
#include<conio.h> {
main() if((num2%count)==0)
{ {
int num1,num2,count,sum1=0,sum2=0; sum2+=count;
clrscr(); }
printf("Enter 1st number="); }
scanf("%d",&num1); if((sum1==num2)&&(sum2==num1))
printf("Enter 2nd number="); {
scanf("%d",&num2); printf("Pair of number is AMICABLE NUMBERS");
for(count=1;count<num1;count++) }
{ else
if((num1%count)==0) {
{ printf("Numbers is not Amicable numbers");
sum1+=count; }
} getch();
} }

/*Write a program to find the roots of a quadratic equation*/


#include<conio.h> #include<stdio.h>
#include<math.h> root=sqrt(temp);
main() x1=(-b+root)/(2*a);
{ x2=(-b-root)/(2*a);
int temp,a,b,c,root,x1,x2,j; printf("x1 = %d x2=%d",x1,x2);
clrscr(); }
printf("a = "); else
scanf("%d",&a); {
printf("b = "); j=sqrt(-temp);
scanf("%d",&b); x1=(-b+j)/(2*a);
printf("c = "); x2=(-b-j)/(2*a);
scanf("%d",&c); printf("x1 = %d x2=%d",x1,x2);
temp=b*b-4*a*c; }
if(temp>=0) getch();
{ }

Switch
C supports a switch for multi way decision making.
Placing default first does not alter the behavior in any way
Only integral constants may be tested.
If no condition matches the default is executed.
If no default nothing is done
The break is important

switch(c){
case a:case a:
printf(area=%d,r*r*pi);
break;
case c:casec:
printf(circumference=%d,2*r*pi);
break;
caseq;
printf(quit option chosen);
break;
default:
printf(unknown option chosen);
break;
}

while loop
Parentheses must surround the condition.
One statement forms the body of the loop.
Braces must be added if more statements are to be executed.
A semicolon placed after the condition forms a body that does nothing.

Int j=5;
While (j>0){
Printf(j=%I\n,j);
}

The while statement is a top-driven loop: first the loop condition (i.e., the controlling expression) is evaluated. If it yields true, the
loop body is executed, and then the controlling expression is evaluated again. If the condition is false, program execution continues
with the statement following the loop body.

/* Read in numbers from the keyboard and


* print out their average.
* -------------------------------------- */
#include <stdio.h> double x = 0.0, sum = 0.0;
int main( ) int count = 0;
{
printf( "\t--- Calculate Averages ---\n" ); if ( count == 0 )
printf( "\nEnter some numbers:\n" printf( "No input data!\n" );
"(Type a letter to end your input)\n" ); else
while ( scanf( "%lf", &x ) == 1 ) printf( "The average of your numbers is %.2f\n", sum/count
{ );
sum += x; return 0;
++count; }
}
scanf( "%lf", &x ) == 1

is true as long as the user enters a decimal number. As soon as the function scanf( ) is unable to convert the string input into a
floating-point numberwhen the user types the letter q,

Do while
Do while guarantees execution at least once. Whereas while has the condition followed by the body, do while has the body
followed by the condition.

The do ... while statement is a bottom-driven loop:

int j=-10;
printf(start\n);
do{
printf(j=%i\n,j);
j--;
}while(j>0);
printf(stop\n);

/*convert a line of lowercase text to uppercase*/


#include<ctype.h>
#define EOL \n

char letter[80];
int tag,count=-1;
do ++count;
while((letter[count]=getchar())!=EOL);
tag=count;
count=0;
do{
putchar(toupper(letter[count]));
++count;
}while(count<tag);
}

/*Reads in integers until a 0 is entered.if 0 as input then it should display


the total number of even and odd integer
average value of even integer
average value of odd integer */
#include<stdio.h> {
#include<conio.h> evenc++;
main() event+=num;
{ }
int num=1,oddc=0,evenc=0,oddt=0,event=0; else
int evenavg,oddavg,ans; {
clrscr(); oddc++;
while(num) oddt+=num;
{ }
printf("Enter a number(enter 0 to end)="); }
scanf("%d",&num); evenavg=event/evenc;
if((num%2)==0) oddavg=oddt/oddc;
printf("\n1.\tTotal number of even and odd integer"); case 2:
printf("\n2.\tAverage value of even integer"); printf("\nAverage value of even integer=%d",evenavg);
printf("\n3.\tAverage value of odd integer"); break;
printf("\n Enter your choice(1,2,3)="); case 3:
scanf("%d",&ans); printf("\nAverage value of odd number=%d",oddavg);
switch(ans) break;
{ default:
case 1: break;
printf("\nTotal odd number=%d\tTotal even number= }
%d\n",oddc,evenc); getch();
break; }
for loop
for encapsulates the essential elements of a loop into one statement

for(initial-par;while-condition;upate-part)
body;

expression1 : Initialization:- Evaluated only once, before the first evaluation of the controlling expression, to perform any necessary
initialization.

expression2 : Controlling expression:-Tested before each iteration. Loop execution ends when this expression evaluates to false.

expression3 : Adjustment:-An adjustment, such as the incrementation of a counter, performed after each loop iteration, and before
expression2 is tested again.

int j
for(j=5;j>0;j--)
printf(j=%I\n,j);

for(j=5;j>0;j--){
printf(j=%i,j);
printf(%s\n,((j%2)==0)?even:odd);
}

/*calculate the average of n numbers*/


int n, count;
float x, average, sum=0;
printf(how many numbers=);
scanf(%d,&n);
for(count=1;count<=n;++count)
{
printf(x=);
scanf(%f,&x);
sum+=x;
}
average=sum/n;
printf(average is %f,average);
}

stepping with for


unlike some languages the for loop is not restricted to stepping up or down by 1:- it is possible to step up or down in whole or
fractional steps.
Math.h
double angle;
for(angle=0.0;angle<3.14159;angle+=0.2)
printf(sine of %f is %f\n,angle,sin(angle));

Extending the for loop


The initial and update parts may contain multiple comma separated statements
int i,j,k
for(i=0,j=5,k=-1,i<10;i++,j++,k--)
Using the special comma operator, several statements may be executed in the initial and or update parts of the loop. The
comma operator guarantees sequential execution of statements.
The initial , condition and update parts may contain no statements at all
for(;i<10;i++,j++,k--)
for(;j<10;)
for(;;)

Nested for loop


/*calculate averages for several differents lists of numbers*/
int n, count, loops,loopcount;
float x, average, sum;
printf(How many lists=);
scanf(%d,&loops);
for(loopcount=1;loopcount<=loops;++loopcount)
{
sum=0;
printf(\n List number %d How many number=,loopcount);
scanf(%d,&n);
for(count=1;count<=n;++count)
{
printf(x=);
scanf(%f,&x);
sum+=x;
}
average=sum/n;
printf(\n The average is %f \n,average);
}
}

/*searching for palindromes noon,peep*/


#include <ctype.h> {
#define EOL \n if(letter[count]!=letter[countback])
#define TRUE 1 {
#define FALSE 0 flag=FALSE;
char letter[80]; break;
int tag,count,countback,flag,loop=TRUE; }
while(loop){ }
flag=TRUE; for(count=0;count<=tag;++count)
printf(Enter a word\n); putchar(letter[count]);
for(count=0;(letter[count]=getchar())!=EOL;++count) if(flag)
; printf(is a palindrome\n);
if((toupper(letter[0]==E)&&( toupper(letter[1]==N)&& else
(toupper(letter[0]==D)) printf(is not a palindrome\n);
break; }
tag=count-1; }
for((count=0;countback=tag);count<=tag/2;(++count;--
countback))

/*This program is used to check how many vowels(a,e,i,o,u) in stirng.*/

#include<stdio.h> if(str[i]=='a' || str[i]=='e'|| str[i]=='i' || str[i]=='o' || str[i]=='u' ||


#include<conio.h> str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' ||
void main() str[i]=='U')
{ {
char str[10]; c++;
int i,c=0; }
clrscr(); }
printf("enter any string"); printf("\n Total vowels are %d",c);
gets(str); getch();
for(i=0;str[i]!='\0';i++) }
{
/*paramid*/
#include <stdio.h> printf("B");
#include <conio.h> }
main() }
{
int i,j,n; for(i=i;i>=1;i--)
clrscr(); {
printf("Enter n:"); printf("\n");
scanf("%d",&n); for(j=i;j>=1;j--)
{
for(i=1;i<=n;i++) printf("A");
{ }
printf("\n"); for(j=i;j>=1;j--)
for(j=1;j<=i;j++) {
{ printf("B");
printf("A"); }
} }
for(j=0;j<i;j++) getch();
{ }
/********************/
#include <stdio.h>
#include <conio.h> for(j=1;j<=i;j++)
{
main() printf("%d ",j);
{ /* if(i==n)
int i,j,n; {
clrscr(); printf("%d ",i);
printf("Enter any number:"); } */
scanf("%d",&n); }
for(i=1;i<=n;i++) }
{ getch();
printf("\n\n\t\t\t"); }
/*This program is used to print triangle shape as 123 and so on.*/
#include<stdio.h> {
#include<conio.h> for(j=1;j<=i;j++)
void main() {
{ printf("%d",j);
int n,i,j; }
clrscr(); printf("\n");
printf("enter value for n :\t"); }
scanf("%d",&n); getch();
for(i=1;i<=n;i++) }

unconditional Jumps

Jump statements interrupt the sequential execution of statements, so that execution continues at a different point in the program. A
jump destroys automatic variables if the jump destination is outside their scope. There are four statements that cause unconditional
jumps in C: break , continue, goto, and return.

Break
The break keyword forces immediate exit from the nearest enclosing loop
for(;;){
printf(type an int=);
if(scanf(%i,&j)==1)
break;
while((c=getchar())!=\n)
;
}
printf(j=%i\n,j);

Continue
The continue keyword forces the next iteration of the nearest enclosing loop.
for(j=1;j<=10;j++){
if(j%3==0)
continue;
printf(j=%i\n,j);
}
if j is exactly divisible by 3 skip.

The goto Statement

The goto statement causes an unconditional jump to another statement in the same function. The destination of the jump is specified
by the name of a label:

goto label_name;

A label is a name followed by a colon:

label_name: statement

Practical exercise
1. Write a program in quant.c which quantifies numbers, Read an integer x and test it, producing the following output;
X greater then or equal to 1000 print hugely positive
X form 999 to 100 very positive
X between 100 and 0 positive
X exactly 0 zero
Between 0 and 100 negative
-100 to 999 very negative
less then or equal to 1000 hugely negative
2. Write a program which accepts four options. The option a calculates the area of a circle (prompting for the radius), the
option C calculates the circumference of a circle the option v calculates the volume of a cylinder (prompting for the radius
and height) while the option q quits the program. The program should loop until the quit option is chosen.
3. write a program in pow.c which reads two numbers, the first a real , the second an integer. The program then outputs the first
number raised to the power of the second.
4. Write a program in drawx,c which draws an x of user specified height. If the user typed 7, the following seris of *
characters would be drawn
5. Write a program in bases.c which offeres the user a choice of converting integers between octal decimal and hexadecimal,
Prompt the user for either o,d orh and read the number in the chosen format. Then prompt the user for the output format
o,d orh and print the number out accordingly

You might also like