C Programming
C Programming
Contents
• Introduction to C
• Elements of C
• Control structure
• Array
• String
• Pointer and DMA
• Function
• Structure
• Union
• Enum
• File handling
• Miscellaneous
Chapter-1
Introduction to C
programming
Key Concepts
• Program
• Compiler
• Flowchart
• Pseudocode
• Algorithm
• Programming language and types
• History of C
• Features of C
• Applications of C
Program:-
Program is a collection of set of instructions to be executed to produce the
output using a special type of compiler or software.
Compiler:-
A compiler is a special program or software which is used to convert source
code to machine code. It is also used to detect errors at the time of
compilation of the program.
Flowchart:-
Flow chart is a visually presenting the flow of control through an information
processing systems, the operations performed within the system and the
sequence in which they are performed.
Pseudocode:-
False code that represents flowchart in paragraph format or sentence is called
pseudocode.
Algorithm:-
The programmer begins the programming process by analyzing the problem,
breaking it into manageable pieces, and developing a general solution for each
piece called an algorithm.
Programming Language:-
The language which is used to represent the instructions of a program to execute
it is called a programming language.
Types:-
• Structural/procedural language:-it is based on structure or procedures.
Ex-c
•Partial object oriented language:-which may or may not use the concepts of
oops to write a program.
Ex-c++, php
•Fully object oriented language:-without the help of oops we cannot write a
program.
Ex-java,.net
•Object based language:-based on objects.
Ex-javascript, vbscript
History of C:-
Before c came to the market there exist many programming languages like algol,
cpl, bcpl, b. But Dennis Ritche found some problems in all these languages. So he
combines the best features of all these four languages and develops a new
language called C language. It was developed in the year of 1972 at AT &
T(American Telegraph and Telecom) Bell Lab., USA.
Historical Development of Programming Languages:-
•PASCAL - 1954
•COBOL - 1957
•ALGOL - 1960
•CPL - 1963
•BCPL - 1967
•B - 1970
•C - 1972
•C++ - 1980
•PYTHON – 1980-1990
•JAVA - 1990
•.NET - 1991
•PHP – 1993
• R – 1993
•SCALA - 2003
Features of C:-
•simple and easy to understand
•flexible
•error detection
•Efficient
Applications of C:-
•used for software development
•used for graphics and animation
•used for fifth generation computer like robots
•used for developing games
•used for project design/development
•used for real time development.
•Used in embedded system
•Used for data structure for competitive programming and OS design
• fundamental language to be used in other languages
Chapter-2
Elements of C
Key Concepts
• Character sets
• Constants
• Variables
• Storage classes
• Tokens
• Identifiers/delimeters
• Comment lines
• Keywords
• Datatypes
• Operators
• Operator precedence
Characterset:-
It combines small alphabets, capital alphabets, digits and special symbols to
write a program.
•a-z(ASCII code->97-122)
•A-Z(ASCII code->65-90)
•0-9(ASCII code->48-57)
•Special symbols(“,&,^,$....) space=32
ASCII- American Standard Code for Information Interchange
Constants:-
Constants are the fixed quantity whose value can never be change
throughout the program execution.
Types:-
1. Integer constant-
-may be +ve or –ve.
-range is -32768 to +32767
-size is 2/4 bytes
-examples are 9,-346
2. Float/real constant-
-may be +ve or -ve
-range is -3.4x1038 to +3.4x1038
-size is 4 bytes
-examples are 1/3,-4.89
3. Character/string constant-
-if it is a character constant then it must be enclosed within a pair of single
quote and size is one byte. ex-‘s’(1 byte)
-if it is a string constant then it must be enclosed within a pair of double
quote and size is not fixed. ex-“anil”(no range)
Variables:-
These are the temporary named memory locations where the constant values
are stored. Every constant value must be associated with a variable inside the
memory.
Rules for constructing a variable:-
Variable name must starts with an alphabet or an underscore (_).
It cannot start with a digit.
But digits can be placed after first place.
No blank space is allowed while declaring a variable.
Variable name length must not be more then 38 characters.
No special symbols except underscore is allowed while declaring a variable.
Storage Classes:-
Storage classes decide in which area of memory the variable values are
stored. It also tells about the scope, life time and initial value of the variable
to be used inside the program. A storage class is created by the help of
keywords succeeded by variable name.
Types:-
1. Automatic storage class:-
•Created by the help of the keyword auto. It is the default storage class of C
programming.
Features:-
•Memory:- inside main memory
•Default initial value:- garbage value
•Scope:- local means inside the same block
•Lifetime:- its life will be expires when it comes out of the block where it is
declared.
Ex:- int a=9; (or) auto int a=9;
Write a program for auto storage class ?
#include<stdio.h>
#include<conio.h>
void main()
{
auto int a=9; //int a=9;
clrscr();
printf(“%d”,a);
getch();
}
Datatypes:-
Datatype decides which type of data a variable can take. It always used as a
suffix for the variable. There are different types datatypes used in c
programming language.
1. Primary datatype:-
It is also called predefined/built-in/primitive datatype. These datatypes
are already available in C library and carry a specific meaning for the
compiler having fixed size, range and format specifier.
datatype size(bytes) format specifier range
•int 2 or 4 %d -32768 to +32767
•char 1 %c -128 to 127
•float 4 %f -3.4x1038 to +3.4x1038
•long 4 %ld -1.7e308 to +1.7e308
•double 8 %lf 2.3E-308 to 1.7E+308
•short 2 %d -32,768 to 32,767
•long int 4 %ld
•long double 10 %lf 3.4E-4932to1.1E+4932
•signed int 4 %d
•unsigned int 4 %u 0 to 4,294,967,295
•signed char 1 %c -128 to 127
•unsigned char 1 %c 0 to 255
•short int 2 %hd -32768 to +32767
•unsigned short int 2 %hu 0 to 65535
2. Secondary datatype:-
it is categorized into two types.
i. derived datatype:-
ex:-array, function, pointer
ii. user-defined datatype:-
ex-struct, union, enum
How to write c program ?
We can use any type of application s/w or compiler to write and run C programs.
Like, if we are using turbo c++ ide then we must have to open the s/w and write
the code. After writing the code we save it with a name and extension (.c) and
the default location is c:\tc\bin. We can compile the program by the help of the
shortcut key alt+f9 and run the program by ctrl+f9.
Write a program to input two integer number and find their addition,
subtraction, multiplication, division and modulo-division ?
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,a,s,m,d,md;
clrscr();
printf(“enter two integer number\n”);
scanf(“%d%d”,&n1,&n2);
a=n1+n2;
s=n1-n2;
m=n1*n2;
d=n1/n2;
md=n1%n2;
printf(“addition value=%d\n”,a);
printf(“subtraction value=%d\n”,s);
printf(“multiplication value=%d\n”,m);
printf(“division value=%d\n”,d);
printf(“modulo division value=%d\n”,md);
getch();
}
Write a program to input an uppercase character and convert it into its equivalent
lowercase character ?
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
int no;
clrscr();
printf(“enter a character in uppercase\n”);
scanf(“%c”,&ch);
no=ch+32;
printf(“character in lowercase is=%c”,no);
getch();
}
Write a program to swap two integer number without using third variable ?
#include<stdio.h>
void main()
{
int a,b;
printf(“enter two integer number\n”);
scanf(“%d%d”,&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf(“after swapping value of a=%d\n after swapping value of b=%d”,a,b);
}
Write a program to input five subject marks of a student and display total,
average and percentage of marks ?
#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,m4,m5;
float tot,avg,per;
clrscr();
printf(“enter five subject marks\n”);
scanf(“%d%d%d%d%d”,&m1,&m2,&m3,&m4,&m5);
tot=m1+m2+m3+m4+m5;
avg=tot/5;
per=(tot/500)*100;
printf(“total marks=%f\n average marks=%f\n percentage marks=
%f”,tot,avg,per);
getch();
}
Write a program to calculate simple interest ?
#include<stdio.h>
#include<conio.h>
void main()
{
float p,r,t,si;
clrscr();
printf(“enter principal, rate and time\n”);
scanf(“%f%f%f”,&p,&r,&t);
si=(p*r*t)/100;
printf(“simple interest=%f”,si);
getch();
}
7. Binary operator:-
the operator which is use to operate over two operand is called binary
operator.
ex:- a+b
8. Ternary/conditional operator:-
the operator which is used to operate over more than two operands are called
ternary operator.
syn:- (condition)? print statement1:print statement2;
Write a program to find the greater number among two inputted number
using ternary operator ?
#include<stdio.h>
void main()
{
int a=5,b=6;
clrscr();
(a>b)?printf(“a is greater”):printf(“b is greater”);
getch();
}
Control Structure
Key Concepts
• Definition and types
• Conditional statements(if, if else, nested if, ladder else if)
• Unconditional statements(switch-case statement)
• Jumping statements(goto, continue, exit)
• Looping statements(while, do-while, for)
• Nested loop
Control structure is the control flow of program execution where we can use
different types of statements to process our programs. There are different
types of statements available in control structures that are:
•conditional statements
•unconditional statements
•jumping statements
•looping statements
1. Conditional statements:-
These are the types of statements where we use conditions to process
the program using some relational operators to produce true or false
value.
Types:-
a. If/simple if:-
Here we are able to specify only one condition and one print statement. The
demerit of this statement is it can’t go to the default part.
syn:- if(condition)
print statement;
Write a program to check whether an inputted number is even or not using
simple if?
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“enter a number\n”);
scanf(“%d”,&a);
if(a%2==0)
printf(“number is even”);
getch();
}
b. If else:-
We are able to specify only one condition but two print statement means one
for true part and another for false part.
syn:- if(condition)
print statement1;
else
print statement2;
Write a program to input two integer number and find the greater one using
if else ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“enter two number\n”);
scanf(“%d%d”,&a,&b);
if(a>b)
printf(“a is greater”);
else
printf(“b is greater”);
getch();
}
Write a program to find the roots of a quadratic equation?
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
float root1, root2, discriminant;
float a, b, c;
printf("\nEnter The Values\n");
printf("\nA:\t");
scanf("%f", &a);
printf("\nB:\t");
scanf("%f", &b);
printf("\nC:\t");
scanf("%f", &c);
discriminant = b*b - 4*a*c;
if(discriminant < 0)
{
printf("\nRoots Are Imaginary\n");
}
else
{
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
printf("\nRoot 1 = %f\n", root1);
printf("\nRoot 2 = %f\n", root2);
}
getch(); }
c. Nested if:-
Here we are able to specify n no of conditions and n number of print
statements. but the problem is that it is a very complex type of statement
where we get confuse about the starting and ending of if block because of
nested structure.
syn:- if(condition1)
if(condition2)
print statement1;
else
print statement2;
.
.
else
if(conditionn)
print statement-1;
else
print statementn;
Write a program to input a year and check whether it is leap year or not
using nested if ?
#include<stdio.h>
#include<conio.h>
void main()
{
int y;
clrscr();
printf(“enter a year\n”);
scanf(“%d”,&y);
if(y%100!=0)
if(y%4==0)
printf(“leap year”);
else
printf(“not”);
else
if(y%400==0)
printf(“leap year”);
else
printf(“not”);
getch();
}
Write a program to input three integer number and find the greatest one
using nested if ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“enter three integer number\n”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
if(a>c)
printf(“a is greatest”);
else
printf(“c is greatest”);
else
if(b>c)
printf(“b is greatest”);
else
printf(“c is greatest”);
getch();
}
d. Ladder else if:-
It is the type of conditional statement where we can specify n no of conditions
and n no of print statements. it is very simple as compare to nested if because
here no nesting of condition is there and we can specify one condition and
one print statement so on.
syn:- if(condition1)
print statement1;
else if(condition2)
print statement2;
.
.
else
print statement;
Write a program to input an integer number and check whether it is even or
odd and positive or negative using ladder else if?
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“enter a number\n”);
scanf(“%d”,&n);
if(n%2==0&&n>0)
printf(“number is even and positive”);
else if(n%2!=0&&n<0)
printf(“number is odd and negative”);
else if(n%2==0&&n<0)
printf(“number is even and negative”);
else if(n%2!=0&&n>0)
printf(“number is odd and positive”);
else
printf(“special number”);
getch();
}
write a program to input three integer number and find the greatest one
using ladder else if ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“enter 3 number\n”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b&&a>c)
printf(“a is greatest”);
else if(b>a&&b>c)
printf(“b is greatest”);
else
printf(“c is greatest”);
getch();
}
Write a program o input a character from keyboard and check whether it is
an alphabet or digit or special symbol ?
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf(“enter a character\n”);
scanf(“%c”,&ch);
if((ch>=65&&ch<=90)||(ch>=97&&ch<=122))
printf(“character is an alphabet”);
else if(ch>=48&7ch<=57)
printf(“character is a digit”);
else
printf(“character is a special symbol”);
getch();
}
Write a program to find the grade of an emp by finding gross salary where
basic salary given and ta=5%, da=7.5% and hra=10% of basic salary using
ladder else if ?
Gross salary grade
>=10000 A
75000-99999 B
50000-74999 C
20000-49999 D
<20000 E
#include<stdio.h>
#include<conio.h>
void main()
{
float bs,ta,da,hra,gs;
clrscr();
printf(“enter basic salary of an emp\n”);
scanf(“%f”,&bs);
ta=0.05*bs;
da=0.075*bs;
hra=0.1*bs;
gs=bs+ta+da+hra;
if(gs>=100000)
printf(“a grade employee”);
else if(gs>=75000&&gs<100000)
printf(“b grade employee”);
else if(gs>=50000&&gs<75000)
printf(“c grade employee”);
else if(gs>=20000&&<50000)
printf(“d grade employee”);
else
printf(“e grade employee”);
getch();
}
Write a program to find the daily wages of a worker according to the
following conditions using ladder else if statement ?
duty in hours amount in rupees
within first 8 hours 100 rupees
next 4 hours 20 rs/hr
next 4 hours 40 rs/hr
next 4 hours 60 rs/hr
next 4 hours 80 rs/hr
#include<stdio.h>
#include<conio.h>
void main()
{
int hr,amt;
clrscr();
printf(“enter duty in hours\n”);
scanf(“%d”,&hr);
if(hr>=1&&hr<=8)
amt=100;
else if(hr>=9&&hr<=12)
amt=100+(hr-8)*20;
else if(hr>=13&&hr<=16)
amt=180+(hr-12)*40;
else if(hr>=17&&hr<=20)
amt=340+(hr-16)*60;
else if(hr>=21&&hr<=24)
amt=580+(hr-20)*80;
printf(“amount incurred by the worker=%d”,amt);
getch();
}
Write a program to find the commission amount of a salesman by inputting
his/her sales amount using ladder else if statement?
sales per day in amount commission per day in percentage
>=100000 10%
70000-99999 7.8%
45000-69999 5.9%
25000-44999 4.1%
<25000 2.0%
#include<stdio.h>
#include<conio.h>
void main()
{
float samt,camt;
clrscr();
printf(“enter sales amount per day\n”);
scanf(“%f”,&samt);
if(samt>=100000)
camt=samt*0.1;
else if(samt>=70000&&samt<100000)
camt=samt*0.078;
else if(samt>=45000&&samt<70000)
camt=samt*0.059;
else if(samt>=25000&&samt<=45000)
camt=samt*0.041;
else
camt=samt*0.02;
printf(“sales commission amount incurred by the salesman=%d”,camt);
getch();
}
2. Unconditional statements:-
These are the types of statements where we don’t use any condition rather
we use no. of cases to produce true or false statement. Cases consist of
constants or expressions.
Types:-
a. Switch-case statement:-
It is the type of unconditional statement where we are able to specify n no
of cases. Case blocks are created by the help of keyword case and exited by
the help the keyword break. if no case is satisfied inside the switch block
then it goes to the default block specified by default keyword.
syn:- switch(expression)
{
case <case-constant 1>:
statement(s);
break;
.
.
case <case-constant n>:
statement(s);
break;
default:
statement(s);
}
Write a program to display the day name using switch case ?
#include<stdio.h>
#include<conio.h>
void main()
{
int day;
clrscr();
printf(“enter your choice\n”);
scanf(“%d”,&day);
switch(day)
{
case 1:
printf(“the day is sunday”);
break;
case 2:
printf(“the day is monday”);
break;
case 3:
printf(“the day is tuesday”);
break;
case 4:
printf(“the day is wednesday”);
break;
case 5:
printf(“the day is thursday”);
break;
case 6:
printf(“the day is friday”);
break;
case 7:
printf(“the day is saturday”);
break;
default:
printf(“wrong choice”);
}
getch();
}
Write a program to input a character and check whether it is vowel or
consonant using switch case ?
#include<stdio.h>
void main()
{
char ch;
printf(“enter a character\n”);
scanf(“%c”,&ch);
switch(ch)
{
case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’:
case ‘u’:
case ‘A’:
case ‘E’:
case ‘I’:
case ‘O’:
case ‘U:
printf(“it is a vowel”);
break;
default:
printf(“it is consonant”);
}
}
Write a program to implement a calculator using switch case ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
char ch;
clrscr();
printf(“enter two number\n”);
scanf(“%d%d”,&a,&b);
fflush(stdin);
printf(“enter your choice\n”);
printf(“1. enter 1 for addition\n”);
printf(“2. enter 2 for subtraction\n”);
printf(“3. enter 3 for multiplication\n”);
printf(“4. Enter 4 for division\n”);
printf(“5. enter 5 for modulo dividion\n”);
scanf(“%c”,&ch);
switch(ch)
{
case ‘+’:
c=a+b;
break;
case ‘-‘:
c=a-b;
break;
case ‘*’:
c=a*b;
break;
case ‘/’:
c=a/b;
break;
case ‘%’:
c=a%b;
break;
default:
printf(“wrong choice”);
}
printf(“calculated value=%d”,c);
getch();
}
3. Jumping statements:-
These are the types of statements which is used to jump the control from one
block to another block. It is a very complex type of statement and now a days
rarely used in c programming.
Types:-
a. Goto statement:-
It is a type of jumping statement where we can jump from one statement
block to another by providing an user-defined label name. it is a very complex
type of statement and rarely used in-case of c programming.
syn:- goto label-name;
label-name:
statement(s);
goto stop;
stop:
write a program to find the greater number among two number using goto
statement ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“enter three integer number\n”);
scanf(“%d%d”,&a,&b);
if(a>b)
goto first;
else
goto second;
first:
printf(“a is greater”);
goto stop;
second:
printf(“b is greater”);
goto stop;
stop:
getch();
}
Write a program to input an integer number and check whether it is even or
odd and positive or negative using goto?
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“enter a number\n”);
scanf(“%d”,&n);
if(n%2==0&&n>0)
goto a;
else if(n%2!=0&&n<0)
goto b;
else if(n%2==0&&n<0)
goto c;
else if(n%2!=0&&n>0)
goto d;
else
goto e;
a:
printf(“number is even and positive”);
goto stop;
b:
printf(“number is odd and negative”);
goto stop;
c:
printf(“number is even and negative”);
goto stop;
d:
printf(“number is odd and positive”);
goto stop;
e:
printf(“special number”);
goto stop;
stop:
getch();
}
Write a program to input a character and check whether it is capital letter or
small letter or a digit or a special symbol using goto statement ?
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf(“enter a character\n”);
scanf(“%c”,&ch);
if(ch>=48&&ch<=57)
goto dg;
else if(ch>=65&&ch<=90)
goto cl;
else if(ch>=97&&ch<=122)
goto sl;
else
goto ss;
first:
printf(“character is a digit”);
goto stop;
cl:
printf(“character is a capital letter”);
goto stop;
sl:
printf(“character is a small letter”);
goto stop;
ss:
printf(“character is a special symbol”);
goto stop:
stop:
getch(); }
4. Looping statements:- These are also called iterative or repeatative
statements. When we want to execute or print one statement for more than
one time with a single specification then we use the concept of looping. For
creating a looping statement we need components.
•Initialization:- from which the variable value starts execution or printing.
•Condition:- upto which the variable value should goes.
•Incr/decr:- used to increase or decrease the value of the variable after each
execution.
Types:-
a. While loop:-
It is otherwise called as top-tested loop or pre-tested loop or entry control
loop. in this type of looping statement first the condition is checked after the
statements are get executed and printed. if the condition is false then no
statement is executed or printed.
syn:- variable value initialization;
while(condition)
{
statement(s);
incr/decr;
}
print statement(s);
Write a program to print 1 to 10 ?
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while(i<=10)
{
printf(“%d\n”,i);
i++;
}
getch();
}
Write a program to find the output of 12+22+32+....n2 ?
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,sum=0,n;
clrscr();
printf(“enter the range\n”);
scanf(“%d”,&n);
while(i<=n)
{
sum=sum+(i*i);
i++;
}
printf(“sum=%d”,sum);
getch();
}
Write a program to input a number and check whether it is mirror or not ?
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num,rev1,rev2,rem1,rem2,sqr,sqrt;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
sqr=power(num,2);
while(sqr!=0)
{
rem1=sqr%10;
rev1=rev1*10+rem1;
sqr=sqr/10;
}
sqrt=sqrt(rev1);
while(sqrt!=0)
{
rem2=sqrt%10;
rev2=rev2*10+rem2;
sqrt=sqrt/10;
}
if(rev2==num)
printf(“number is mirror”);
else
printf(“number is not mirror”);
getch();
}
Write a program to input a number and check whether it is prime or not ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i=1,counter=0;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
while(i<=num)
{
if(num%i==0)
counter++;
i++;
}
if(counter==2)
printf(“number is prime”);
else
printf(“number is not prime”);
getch();
}
Write a program to find the reverse of a number ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rev=0,rem;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
while(num!=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
printf(“reverse value=%d”,rev);
getch();
}
Write a program to input a number and check whether it is palindrome or
not ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rev=0,rem,temp;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
temp=num;
while(num!=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
if(temp==rev)
printf(“number is palindrome”);
else
printf(“number is not palindrome”);
getch();
}
Write a program to convert a decimal number into its binary equivalent ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rem,sum=0,prd=1;
clrscr();
printf(“enter a decimal number\n”);
scanf(“%d”,&num);
while(num!=0)
{
rem=num%2;
sum=sum+rem*prd;
prd=prd*10;
num=num/2;
}
printf(“binary equivalent=%d”,sum);
getch();
}
Write a program to input a number and check whether it is strong number or
not ?
#include<stdio.h>
void main()
{
int temp,num,fact,sum=0,rem;
printf(“enter a number\n”);
scanf(“%d”,&num);
while(num!=0)
{
fact=1;
rem=num%10;
while(rem!=0)
{
fact=fact*rem;
rem--;
}
sum=sum+fact;
num=num/10;
}
if(sum==temp)
printf(“number is strong”);
else
printf(“number is not strong”); }
Write a program to input a number and check whether it is perfect number or
not ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num,sum=0,i=1;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
while(i<num)
{
if(num%i==0)
sum=sum+i;
i++;
}
if(sum==num)
printf(“number is perfect”);
else
printf(“number is not perfect”);
getch();
}
Write a program to input a number and check whether it is magic no. or not?
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rem,sum=0,temp;
clrscr();
printf(“enter a number\n”);
scanf(“%d”,&num);
temp=num;
while(num!=0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
s=sum*sum;
if(s==temp)
printf(“magic number”);
else
printf(“not”);
getch(); }
Write a program to find the smallest digit from a number?
#include<stdio.h>
#include<conio.h>
void main()
{
int num, temp = 0, small = 10;
printf("\nEnter A Number:\t");
scanf("%d", &num);
while(num > 0)
{
temp = num%10;
if(temp < small)
{
small = temp;
}
num = num / 10;
}
printf("\nSmallest Digit in the Integer: \t%d\n", small);
getch();
}
Write a program to input a number and check it is a perfect square no. or not?
#include<stdio.h>
#include<conio.h>
void main()
{
int num, temp, count = 1;
printf("\nEnter the Number:\t");
scanf("%d", &num);
temp = num;
while(temp > 0)
{
temp = temp - count;
count = count + 2;
}
if(temp == 0)
{
printf("\n%d is a Perfect Square\n", num);
}
else
{
printf("\n%d is Not a Perfect Square\n", num);
}
getch();
}
b. Do-while loop:-
It is otherwise called as bottom-tested loop or post-tested loop or exit
control loop. in this type of looping statement first the statements are get
executed and printed after that the condition is checked. if the condition is
false then no statement is executed or printed.
syn:- variable value initialization;
do
{
statement(s);
incr/decr;
}
while(condition);
print statement(s);
Write a program to print the series like 11,22,33,....,99 ?
#include<stdio.h>
#include<conio.h>
void main()
{
int i=11;
clrscr();
do
{
printf(“%d\n”,i);
i=i+11;
}
while(i<=99);
getch();
}
Write a program to input an integer number and find the sum of its digits ?
#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,num,rem;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
do
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
while(num!=0);
printf(“sum of digits=%d”,sum);
getch();
}
Write a program to input a number and find its factorial value ?
#include<stdio.h>
#include<conio.h>
void main()
{
int fact=1,num;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
do
{
fact=fact*num;
num--;
}
while(num!=0);
printf(“factorial value=%d”,fact);
getch();
}
Write a program to input a number and check whether it is armstrong or not ?
#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,num,rem,temp;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
temp=num;
do
{
rem=num%10;
sum=sum+(rem*rem*rem);
num=num/10;
}
while(num!=0);
if(temp==sum)
printf(“number is armstrong”);
else
printf(“number is not armstrong”);
getch();
}
c. For loop:-
It is the simplest type of looping statement because here all the three parts of
the loop written in one line; so it reduce the line of codes.
syn:- for(initialization; condition; incr/decr)
Write a program to print the series like 50,45,40,....,5 ?
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=50;i>=5;i=i-5)
{
printf(“%d\n”,i);
}
getch();
}
Write a program to find the hcf and lcm of two integer number ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,hcf,lcm,i;
clrscr();
printf(“enter two number”);
scanf(“%d%d”,&num1,&num2);
for(i=1;i<=num1,i<=num2;i++)
{
if(num1%i==0&&num2%i==0)
hcf=i;
}
lcm=(num1*num2)/hcf;
printf(“hcf value=%d\n lcm value=%d”,hcf,lcm);
getch();
}
Write a program to find n fibonacci numbers starting from 0 ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num,prev=0,curr=1,next=0,i;
clrscr();
printf(“enter the range”);
scanf(“%d”,&num);
printf(“%d\n”,prev);
printf(“%d\n”,curr);
for(i=1;i<=num-2;i++)
{
next=prev+curr;
printf(“%d\n”,next);
prev=curr;
curr=next;
}
getch();
}
Write a program to print Lucas series ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
int i,n;
printf("Enter the Limit:\t");
scanf("%d", &n);
a=2;
b=1;
for(i=0;i <n;i++)
{
printf("%d\t",a);
c=a+b;
a=b;
b=c;
}
getch();
}
Write a program to find sum of all the even numbers and product of all odd
numbers in between 1 to 10?
#include<stdio.h>
#include<conio.h>
void main()
{
int i,even=0,odd=1;
clrscr();
for(i=1;i<=10;i++)
{
if(i%2==0)
even=even+i;
else
odd=odd*i;
}
printf(“even sum=%d\n odd product=%d”,even,odd);
getch();
}
Write a program to print all the prime numbers between 1 and 100 ?
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,c=0;
clrscr();
for(i=1;i<=100;i++)
{
for(j=1;j<=i;j++)
if(i%j==0)
c++;
if(c==2)
printf(“%d\n”,i);
}
getch();
}
Nested for:-
When one or more than one for loops are nested inside another for loop then
it is called nested for loop.
Write a program for pyramid series ?
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf(“enter the row size\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf(“%d”,j);
printf(“\n”);
}
getch();
}
Write a program for pyramid series ?
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
printf(“%d”,j);
printf(“\n”);
}
getch();
}
Write a program for pyramid series ?
A
A B
A B C
A B C D
A B C D E
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=65;i<=69;i++)
{
for(j=65;j<=i;j++)
printf(“%c”,i);
printf(“\n”);
}
getch();
}
Write a program for pyramid series ?
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k=1;
clrscr();
for(i=1;i<=5;i++,k=k+2)
{
for(j=i;j<=4;j++)
printf(“ “);
for(j=1;j<=k;j++)
printf(“*”);
printf(“\n”);
}
getch();
}
Write a program to print floyd triangle upto five rows ?
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k=1;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d”,k);
k++;
}
printf(“\n”);
}
getch();
}
Write a program to generate pascal triangle upto five rows ?
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
#include<stdio.h> #include<conio.h>
void main()
{
int i,j,space,coef=1;
clrscr();
for(i=1;i<=5;i++)
{
for(space=1;space<=5-i;space++)
printf(“ “);
for(j=0;j<=i;j++)
{
if(j==0||i==0)
coef=1;
else
coef=coef*(i-j+1)/j;
printf(“%d”,coef);
}
printf(“\n”);
}
getch();
}