अध्याय 1 (Chapter 1)
सी का अवलोकन (Overview of C)
सी एक शक्तिशाली प्राग्रामिंग भाषा है क्योंकि यह हाई लेबल स्ट्रकचर्ड मशीन आधारित भाषा है। सी को डेनिस रिची ने बेल लैबोरेटरी में 1972 को बनाया हेै। सी में बहुत सारे शक्तिशाली फिचर्स है और डाटा टाइप का भी व्यवहार इस में जोडा गया है। सी यूनिक्स और डॉॅस दोनांे आपरेटिंग सिस्टम में काम करता है।
1 सबसे पहले प्रोग्राम बनाये ।
2 उसके बाद कम्पाइल कर लें।
3 उसके बाद प्रोग्राम को लिंक करवायें।
4 फिर प्रोग्राम को एकसीक्यूट करे।
प्रत्येक सी प्रोग्राम में एक main( ) फंक्शन का प्रयोग किया जाता है।
ऽ प्रत्येक प्रयोग के फंक्शन के बाद braces { और } का प्रयोग किया जाता है।
ऽ सी प्रयोग लोवरकेस में लिखा जाता है।
ऽ प्रोग्राम लाइन शब्दों के बीच में गेप देना चाहिये।
ऽ प्रत्येक स्टेटमेंट के लास्ट में सेमीकोलेन ; लगाते है।
ऽ सभी वेरियेबल का डाटा टाइप घोषित किया जाना चाहिए।
हेडर फाइल को प्रोग्राम के सबसे उपर लिखना चाहिए। जिसे #include का प्रयागे किया जाता है जिसके द्वारा फंक्शन को एक्सीक्यूट किया जाता है। जैसे #include<stdio.h> के द्वारा printf() और scanf() फंक्शन एक्सीक्यूट होते है।
ऽ #include header हेडर को define करने के बाद सेमीकोलेन(;) नही लगाया जाता है।
ऽ # चिन्ह हमेशा पहले कॉलम में लिखा जाता है।
ऽ जब भी ग्रुप स्टेटमेंट में { braces का प्रयोग करते है तब ओपनिग braces और } क्लोसिंग braces को सही तरीके से बंद किया जाना चाहिए।
ऽ सी एक फ्री फार्म भाषा है इसलिये इंडेशन का सही प्रयोग करते हुए प्रोग्राम को बनाना चाहिए।
ऽ कमेंट लाइन बनाने के लिए /* */ लिखना चाहिए।
प्रोग्राम लिखने के लिये
Steps
1 सबसे पहले turbo C या C++ open करे।
2 फिर प्रोग्राम को टाइप करें।
3 अब प्रोग्राम को सेव कर लें। सेव करने के लिये F2 या File में जा कर save को क्लिक करें । अब वहॉ पर फाइल का नाम लिखे और एक्सटेंशन नाम .c लगाये। जैसे sum.c
4 अब फाइल को कम्पाइल करें। कम्पाइल करने के लिये Alt+F9 दबाये। अब प्रोग्राम ेsum.obj बना लेगा।
5 अब यदि कोई एरर हो तो उसे सुधार कर फिर से सेव कर कम्पाइल करें ।
6 अब प्रोग्राम को लिंक और रन करने के लिये Ctrl+F9 प्रेस करेंगें। तब प्रोग्राम एक्सीक्यूटेबल फाइल बनायेगा sum.exe
7 और प्रोग्राम एक्सीक्यूट हो जायेगा।
फाइल नाम देने के नियम
1 फाइल का नाम 8 अक्षर का होना चाहिए और एक्सटेंशन नाम . लगा कर 3 तक अक्षर का होना चाहिए। जैसे sum.c या कोई और नाम।
2 फाइल के नाम और एक्सटेंशन के बीच में गेप नही होना चाहिए।
3 फाइल का नाम एलफाबेट से शुरू होना चाहिए। जैसे sum1.c ठीक है पर 1sum.c गलत है।
4 फाइल के नाम में कोई भी स्पेशल कैरेक्टर नही होना चाहिए। जैसे sum$.c
5 फाइल के नाम में स्पेशल कैरेक्टर _ लगा सकते है। जैसे Sum_1.c
प्रोग्राम
1.Sum
of two numbers
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a,b,c;
clrscr();
printf(“Enter
any two numbers “);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“Sum=%d”,c);
getch();
}
======================================================
2.Subtraction
two numbers
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a,b,c;
clrscr();
printf(“Enter
any two numbers “);
scanf(“%d%d”,&a,&b);
c=a-b;
printf(“Subtraction=%d”,c);
getch();
}
======================================================
3.Multiplication of two numbers
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a,b,c;
clrscr();
printf(“Enter
any two numbers “);
scanf(“%d%d”,&a,&b);
c=a*b;
printf(“Multiplication=%d”,c);
getch();
}
======================================================
4.Division
of two numbers
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a,b,c;
clrscr();
printf(“Enter
any two numbers “);
scanf(“%d%d”,&a,&b);
c=a/b;
printf(“Sum=%d”,c);
getch();
}
======================================================
5.Module
of two numbers(reminder)
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a,b,c;
clrscr();
printf(“Enter
any two numbers “);
scanf(“%d%d”,&a,&b);
c=a%b;
printf(“Module
=%d”, c);
getch();
}
======================================================
6. Program Formula x=a/(b-c)
#include<stdio.h>
#include<conio.h>
void
main()
{
int
x,a,b,c;
clrscr();
printf(“Enter
any three numbers “);
scanf(“%d%d%d”,&a,&b,&c);
x=a/(b-c);
printf(“X=%d”,x);
getch();
}
======================================================
7.Find
Celsius to Fahrenheit f=(1.8*c)+32
#include<stdio.h>
#include<conio.h>
void
main()
{
float
f,c;
clrscr();
printf(“Enter
any Celsius Degree “);
scanf(“%f”,&c);
f=(1.8*c)+32;
printf(“Fahrenheit=%f”,
f);
getch();
}
======================================================
8.Find
Fahrenheit to Celsius c=(0.55)*(f-32)
#include<stdio.h>
#include<conio.h>
void
main()
{
float
f,c;
clrscr();
printf(“Enter
any Fahrenheit Degree “);
scanf(“%f”,&f);
c=(0.55)*(f-32);
printf(“Celsius
Degree=%f”,c);
getch();
}
======================================================
9.
Write your Name with Address
#include<stdio.h>
#include<conio.h>
void
main()
{
clrscr();
printf(“
Sadhana Sharma “);
printf(“
\nWel-Come to MLC “);
printf(“
\nKorba “);
printf(“
\nMIG-85 “);
printf(“
\nNehru Nagar “);
printf(“
\nKorba “);
getch();
}
======================================================
10.
Area of Triangle Area = sqrt(s*(s-a)*(s-b)*(s-c))
a=3
b=6
c=5
#include<stdio.h>
#include<conio.h>
#include<math.h>
void
main()
{
int
area,area1,a,b,c,s;
clrscr();
printf(“
Enter any three number for area of triangle
“);
scanf(“%d%d%d”,&a,&b,&c);
s=(a+b+c)/2;
area=(s*(s-a)*(s-b)*(s-c));
if(area==0)
{
printf(“Square
Root not found!!”);
}
else
area1=sqrt(area);
printf(“Area
of triangle=%d”,area1);
getch();
}
======================================================
11. Average of any three Numbers avg =(a+b+c)/3;
#include<stdio.h>
#include<conio.h>
void
main()
{
int
avg,a,b,c;
clrscr();
printf(“
Enter any three number for area of triangle
“);
scanf(“%d%d%d”,&a,&b,&c);
avg=(a+b+c)/3;
printf(“Average
of three numbers =%d”,avg);
getch();
}
======================================================
अध्याय 2 (Chapter 2)
कानसटेंट वेरियेबल और डाटा टाइप (Constant, Variables, and Data Types)
परिचय (Introduction)
प्रोग्राम में डाटा टाइप होते है जो कि नम्बर अक्षर और स्ट्रिंग (number, characters and string )से मिल कर बने होते है। जो कि हमें उपयोगी आउटपुट देता है जिसे हम इनफार्मेशन कहते है।इनसट्रक्शन के समूह (Set of Instruction) को प्रोग्राम (Program) कहते है। प्रोग्राम (Program) लिखने के कुछ नियम होते है जिसे हम सिनटेक्स नियम (syntax rules ) कहते है।
करैक्टर सेट (Character
Set)
करैक्टर (Character) जो कि वर्ड नम्बर और एक्सप्रेशन(words, numbers and expressions) जिसके द्वारा कमप्यूूटर में प्रोग्राम (program) चलता है। सी में करैक्टर केे ग्रुप को 4 समूह में बांटा गया है।
1
Letters – upper letter A-Z lower letters a-z
2
Digits-0 to 9
3
Special Characters - , ; . :? / \ ~
4
White Spaces-new line, carriage return
C
Tokens
1
Keywords
2
Constant
3
Identifiers
4
String
5
Operators
6
Special Characters
Keywords
and Identifiers
Keywords कुछ Keywords का अपना meaning होता है जिसे change नही कर सकते। जैसे auto, if, break, char, int, float, void, long, double, do, while, for etc.
Identifier variables, functions, array होते है। ये letters और digits हो सकते है जो कि lower या upper case में हो सकते है। अधिकतर हम lowercase में ही variable का नाम देते है। बडे variable के नाम के बीच में ; (underscore _ ) लगाते है।
Variable का नाम लिखने का नियम
1. underscore _ के लिए पहला अक्षर alphabet होना चाहिए। जैसे sum_1
2. सिर्फ letters, digits या underscore होना चाहिए।
3. keywords का use नही करना चाहिए।
4 Variable name के बीच में white space या gap नही होना चाहिए। जैसे sum 1
5 variable का नाम लिखने के लिये 31 character से ज्यादा नही होना चाहिए।
Constant
वो values जिसका मान स्थिर होता है और execution के समय नही बदलता है। जैसे a=32
Single
character constant ‘A’ ‘5’
Backslash
character constant
\a audible alert
\b back space
\f
form feed
\n new line
\r carriage return
\t
horizontal tab
\v vertical tab
\’ single quote
\” double quote
\? Question mark
\\ backslash
\0 null
Data
types
int
2 bytes
char 1 byte
long
int 4 bytes
float 4 bytes
double 8 bytes
long
double 10 bytes
keybord से data read करने के लिये scanf का use किया जाता है।
scanf(“Enter
a number %d “, &num);
अध्याय 3 (Chapter 3)
Operators and Expressions
Introduction
C के द्वारा buit in operator को support करता है। जैसे+ - * ?/ & < . Operator वह सिम्बाल है जिसके द्वारा लॉजिकल या मैथमेटिकल आपरेशन को करवा सकते है। आपरेटर के द्वारा डाटा और वेरियेबल को प्रोग्राम में चलाया जाता है। Operator कई तरह के होते है।
1 Arithmetic
Operator:
+ Addition
- Subtraction
* Multiplication
/ Division
% Module Division
12. Find Months and days in days
#include<stdio.h>
#include<conio.h>
void
main()
{
int day,days, mon;
clrscr();
printf(“
Enter days\n “);
scanf(“%d”,&days);
mon=days/30;
day=days%30;
printf(“Month=%d
and Days=%d”,mon,day);
getch();
}
======================================================
2
Relational Operator:
< less than
> greater than
<= less than or equal to
>=
greater than or equal to
== equal to
!= not equal to
tSls a>b
a<b
a==b
a!=b
13. Greatest among two numbers
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a,b;
clrscr();
printf(“
Enter any two numbers\n “);
scanf(“%d%d”,&a,&b);
if(a>b)
{
printf(“A
is greater %d “,a);
}
else
{
printf(“B
is greater %d “,b);
}
getch();
}
======================================================
3
Logical Operator
&&
and
|| or
! not
tSls a>b && a>c
a>b
|| a>c
a!=b
14.Greatest among three numbers
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a,b,c;
clrscr();
printf(“
Enter any three numbers\n “);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b
&& a>c)
{
printf(“A
is greater %d “,a);
}
else
{
printf(“B
is greater %d “,b);
}
getch();
}
======================================================
4 Assignment Operator
एसानगमेंट आपरेटर रिजल्ट को वेरियेबल में रखता है। एसानगमेंट आपरेटर को ‘=’ चिन्ह से दिखाते है।
जैसे
a=a+1 a+=1
a=a-1 a-=1
a=a*(n+1) a*=n+1
a=a/1 a/=1
a=a%b a%=b
15.
Sum of n numbers
#include<stdio.h>
#include<conio.h>
void
main()
{
int
num,i=1,sum=0;
clrscr();
printf(“
Enter any numbers for sum \n “);
scanf(“%d”,&num);
do
{
sum=sum+i;
i=i+1;
}
while(i<=num);
printf(“sum
of %d is %d “,num,sum);
getch();
}
======================================================
5
Increment and Decrement Operator
यह एक उपयोगी आपरेटर जो कि दूसरे कम्प्यूटर भाषा में नही पाया जाता है। जिसे इनक्रीमेंट आपरेटर डिक्रिमेंट आपरेटर कहे जाता है।
++ इनक्रीमेंट आपरेटर
_ _ डिक्रिमेंट आपरेटर
++a जो कि a=a+1 या a+=1 के बराबर है।
- -a जो कि a=a-1 या a-=1 के बराबर है।
a=10;
b=a++;
यहॉ पर a और b का मान 11 होगा
लेकिन
a=10;
b=a++;
यहॉ पर a का मान 10 और b का मान 11 होगा।
16.
Print 1-10 numbers
#include<stdio.h>
#include<conio.h>
void
main()
{
int
i;
clrscr();
for(i=1;i<=10;i++)
{
printf(“\n
%d”,i);
}
getch();
}
======================================================
17.
Print 10-1 numbers
#include<stdio.h>
#include<conio.h>
void
main()
{
int
i;
clrscr();
for(i=10;i>=1;i-
-)
{
printf(“\n
%d”,i);
}
getch();
}
======================================================
6
Conditional Operator
इसे हम ternary operator भी कहते है। इसमें दो चिन्ह होते है ? और :
18.Greatest among two numbers with ternary or conditional operator
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a,b,c;
clrscr();
printf(“
Enter any two numbers\n “);
scanf(“%d%d”,&a,&b);
c=(a>b?a:b);
printf(“A
is greater %d “,c);
printf(“B
is greater %d “,c);
getch();
}
======================================================
7
Bitwise Operator
बीटवाइस आपरेटर एक स्पेशल आपरेटर जिसके द्वारा डाटा बीट लेबल में काम करेगा। बीटवाइस आपरेटर फलोट और डबल के साथ काम नही करेगा
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
<< shift left
>> shift right
अध्याय 4 (Chapter 4)
Managing Input and Output Operations
हम अपने program में a
#include<stdio.h>
stdio header file के द्वारा printf() और scanf() फंक्शन को आउटपुट देता है।
#include<math.h>
math header file के द्वारा cos() sin() tan() sqrt() आदि फंक्शन को आउटपुट देता है।
#include<conio.h>
conio header file के द्वारा clrscr() getch() आदि फंक्शन को आउटपुट देता है।
Reading
a Character
स्टैर्ण्ड इनपुट यूनिट अधिकतर की बोर्ड होता है जबकि स्टैर्ण्ड आउटपुट यूनिट स्क्रीन होता है। तो की बार्ड से सिंगल कैरेक्टर के लिए हम getchar() function का प्रयोग करते है।
Ex variable_name=getchar();
char
name;
name=getchar();
19. Write a program for reading a character from keyboard
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void
main()
{
char
ans;
clrscr();
printf(“
Would you like to know my name “);
printf(“Press
Y for yes and N for no:”);
ans=getchar();
if
(ans= =’Y’ || ans= =’y’)
printf(“\nMy
name is Sharma “);
else
printf(“\n
You don’t want to know your name “);
getch();
}
======================================================
char
name;
isalpha(name)
isdigi(name)
isalnum(name)
islower(name)
isupper(name)
isspace(name)
isprint(name)
इन सभी फंक्शन के लिए #include<ctype.h> header file का उपयोग किया जाता है।
Writing a Character
स्टैर्ण्ड आउटपुट यूनिट स्क्रीन होता है। तो स्क्रीन में सिंगल कैरेक्टर प्रिंट करने के लिए putchar() function का प्रयोग करते है।
Ex
variable_name=putchar();
20.Write a program for getchar() and putchar()
char
name;
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
main()
{
char
alpha;
clrscr();
putchar(‘\n’);
printf("Enter any Character ");
alpha=getchar();
if(islower(alpha))
putchar(toupper(alpha));
else
putchar(tolower(alpha));
getch();
}
======================================================
कुछ scanf का फार्मेट
%c read single character
%d read a decimal integer
%e read a floating point value
%f read a floating point value
%g read a floating point value
%h read a short integer
%i read a decimal, hexadecimal, octal
%o read an octal
%s read a string
%[…]
read a string of word
अध्याय 5 (Chapter 5)
डिसिजन मेकिंग और ब्रांच्रिग
Decision Making and Branching
तर्क के लिये निम्न स्टेटमेंट
परिचय
1 if statement
2
switch statement
3
conditional statement
4
goto statement
1
if statement
if
(test expression)
{
True
block statement;
}
else
{
False
block statement;
}
Statement;
21.Write a program for Marksheet
#include<stdio.h>
#include<conio.h>
void
main()
{
char
name[15];
long
int os, funda, msoffice, tally, c, tot;
float
per;
clrscr();
printf(“Enter
your name :”);
scanf(“%s”,&name);
printf(“\nEnter
marks of os, msoffice, funda, tally, c :”);
scanf(“%ld%ld%ld%ld%ld”,&os,&msoffice,&funda,&tally,&c);
tot=os+msoffice+funda+tally+c;
per=(tot*100)/500;
printf(“\nTotal
%ld “,tot);
printf(“\nPercent
%.2f”,per);
if
(per>=60)
printf(“\nFisrt
Division “);
else
if(per>=45)
printf(“\nSecond
Division “);
else
if
(per>=33)
printf(“\nThird
Division “);
else
printf(“\nFail
“);
getch();
}
======================================================
22. Write a program for calculator switch case
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a, b, sum, sub, multi, div, modu;
int
ch;
char
ans;
clrscr();
do
{
printf(“\n
press 1 for Sum “);
printf(“\n
press 2 for Subtraction “);
printf(“\n
press 3 for Multiply “);
printf(“\n
press 4 for Division “);
printf(“\n
press 5 for Module “);
printf(“\nEnter
any two numbers “);
scanf(“%d%d”,&a,&b);
printf(“\nEnter
your Choice 1/2/3/4/5 “);
scanf(“%d”,&ch);
switch(ch)
{
case
1:
c=a+b;
printf(“\n
Sum=%d”,c);
break;
case
2:
c=a-b;
printf(“\n
Subtraction=%d”,c);
break;
case
3:
c=a*b;
printf(“\n
Multiplication =%d”,c);
break;
case
4:
c=a/b;
printf(“\n
Division =%d”,c);
break;
case
5:
c=a%b;
printf(“\n
Module=%d”,c);
break;
default:
printf(“\nWrong
Choice !!”);
}
printf(“Do
you want to continue…..y/n “);
scanf(“%s”,&ans);
}while(ans= =’y’
|| ans= =’Y’);
}
======================================================
23.Write a program for Simple Interest, Average and Table by switch case
#include<stdio.h>
#include<conio.h>
void
main()
{
int
p,r,t,si,a,b,c,avg,num,t,i;
char
ch;
char
ans;
clrscr();
do
{
printf(“\n
press S or s for Simple Interest “);
printf(“\n
press A or a for Average “);
printf(“\n
press T or t for Table “);
printf(“\nEnter
your Choice S/s,A/a,T/t “);
scanf(“%s”,&ch);
switch(ch)
{
case
‘s’:
case
‘S’:
printf(“Enter
Principal Amount, Rate, Time “);
scanf(“%d%d%d”,&p,&r,&t);
si=(p*r*t)/100;
printf(“\n
Simple Interest =%d”,si);
break;
case
‘a’:
case
‘A’:
printf(“Enter
any three numbers for Average “);
scanf(“%d%d%d”,&a,&b,&c);
avg=(a+b+c)/3;
printf(“\n
Average of three numbers =%d”,avg);
break;
case
‘t’:
case
‘T’:
printf(“Enter
any number for table “);
scanf(“%d”,
&num);
printf(“Table
of given number :%d”,num);
for(i=1;i<=10;i++)
{
t=num*i;
printf(“\n
%d * %d = %d", num, i, t);
break;
default:
printf(“\nWrong
Choice !!”);
}
printf(“Do
you want to continue…..y/n “);
scanf(“%s”,&ans);
}while(ans==’y’
|| ans==’Y’);
}
======================================================
24.Write a program to check number is Negative, Positive or Zero
#include<stdio.h>
#include<conio.h>
void
main()
{
int
num;
char
ans;
clrscr();
do
{
printf(“Enter
any number to check number is Positive Negative or Zero “);
scanf(“%d”,
&num);
if(num>0)
printf(“Number
is Positive “);
else
if
(num<0)
printf(“Number
is Negative “);
else
printf(“Number
is Zero “);
printf(“Do
you want to continue…..y/n “);
scanf(“%s”,&ans);
}while(ans==’y’
|| ans==’Y’);
}
======================================================
25. Write a program to check whether Number is Odd or Even
#include<stdio.h>
#include<conio.h>
void
main()
{
int
num;
char
ans;
clrscr();
do
{
printf(“\nEnter
any number to check number is Odd or Even “);
scanf(“%d”,&num);
if(num%2=
=0)
printf(“\nNumber
is Even “);
else
printf(“\nNumber
is Odd “);
printf(“Do
you want to continue…..y/n “);
scanf(“%s”,&ans);
}while(ans==’y’
|| ans==’Y’);
}
======================================================
26. Write a program to check whether year is leap year or Not
#include<stdio.h>
#include<conio.h>
void
main()
{
int
year;
char
ans;
clrscr();
do
{
printf(“\nEnter
year to check Year is leap year or not “);
scanf(“%d”,&year);
if(year
%4= =0 || year /100==0)
printf(“\nYear
is Leap “);
else
printf(“\nYear
is not Leap Year “);
printf(“Do
you want to continue…..y/n “);
scanf(“%s”,&ans);
}while(ans==’y’
|| ans==’Y’);
}
======================================================
अध्याय 6 डिसिजन मेकिंग और लूपिंग
परिचय
हम पिछले अध्याय में पढे है कि प्रोग्राम के कुछ भाग को बार बार काउंटर के माध्यम से प्रिंट करवा सकते है और टेस्टिंग के लिये if स्टेटमेंट का प्रयोग करते है।
s=0;
n=1;
loop:
s=s+n;
if(n==10)
goto
print;
else
{
n=n+1;
goto
loop;
}
print:
……..
……..
इस प्रोग्राम से निम्नलिखित काम होगा
1 वेरियबल n का मान 1 रखे है।
2 n को s में जोड रहे हैं।
3 तब तक जोडेगा जब तक n का मान 10 न हो जायें उसके बाद प्रोग्राम रिजल्ट को प्रिंट करेगा
4 यदि n का मान 10 से कम होगा तो n का मान 1 से बढते जायेगा और कंट्रोल वापस loop में जा कर sum करते जायेगा।
एक लूपिंग प्रासेस को निम्न चार स्टेपस में करते है
1 कंडिशनल वेरियेबल को प्रारंभिक मान दे कर सेट किया जाता है।
2 स्टेटमेंट को लूप के द्वारा लिखा जाता है।
3 कंडिशन के आधार पर लूप में टेस्ट किया जाता है।
4 मान को कंडिशन के आधार पर बढाया या घटाया जाता है।
सी भाषा में लूपिंग के लिये 3 तरीका है।
1 while स्टेटमेंट
2 do स्टेटमेंट
3 for स्टेटमेंट
1 while स्टेटमेंट सी के सभी लूप में सबसे आसान लूप while है।
while
(test condition)
{
body
of the loop;
}
27
Write a Program for 1-10 number ….. while loop
#include<stdio.h>
#include<conio.h>
void
main()
{
int
n=1;
clrscr();
while(n<=10)
{
printf(“%d”,n);
n=n+1;
}
getch();
}
======================================================
28
Write a Program for even number 0-10….. while loop
#include<stdio.h>
#include<conio.h>
void
main()
{
int
n=0;
clrscr();
while(n<=10)
{
printf(“%d”,n);
n=n+2;
}
getch();
}
======================================================
29
Write a Program for odd number 1-10 ….. while loop
#include<stdio.h>
#include<conio.h>
void
main()
{
int
n=1;
clrscr();
while(n<=10)
{
printf(“%d”,n);
n=n+2;
}
getch();
}
======================================================
30
Write a Program for how many even and odd numbers ….. while loop
#include<stdio.h>
#include<conio.h>
void
main()
{
int
num=0,even=0,odd=0;
clrscr();
while(num<=10)
{
if(num%2==0)
{
printf(“\nEven
Numbers=%d”,num);
even=even+1;
}
else
{
printf(“\nOdd
Numbers=%d”,num);
odd=odd+1;
}
num=num+1;
}
printf(“\nTotal
Even numbers are %d”,even);
printf(“\nTotal
Odd numbers are %d”,odd);
getch();
}
======================================================
2 do स्टेटमेंट
while लूप में जैसे देखें है कि कंडिशन चेक करने के बाद ही प्रोग्राम को एक्सीक्यूट करता है जबकि कव में लूप एक बार एक्सीक्यूट करता ही है।
do
{
body
of the loop;
}while(test-condition);
31
Write a Program for how many even and odd numbers ….. do while loop
#include<stdio.h>
#include<conio.h>
void
main()
{
int
num=0,even=0,odd=0;
clrscr();
do
{
if(num%2==0)
{
printf(“\nEven
Numbers=%d”,num);
e=e+1;
}
else
{
printf(“\nOdd
Numbers=%d”,num);
odd=odd+1;
}
num=num+1;
}while(num<=10);
printf(“\nTotal
Even numbers are %d”,even);
printf(“\nTotal
Odd numbers are %d”,odd);
getch();
}
======================================================
3 for स्टेटमेंट
for लूप एक और तरह का लूपिंग है
for(initial
value; test condition; increment/decrement)
{
body
of the loop;
}
for(i=0;
i<=10; i++)
{
printf(“%d”,i);
}
32
Write a Program for even number 0-10….. for loop
#include<stdio.h>
#include<conio.h>
void
main()
{
int
n;
clrscr();
for(n=0;n<=10;n=n+2)
{
printf(“%d”,n);
}
getch();
}
======================================================
33
Write a Program to print
1
12
123
1234
12345
#include<stdio.h>
#include<conio.h>
void
main()
{
int
j,k;
clrscr();
for(j=1;
j<=5; j++)
{
for(k=1;
k<=j; k++)
{
printf(“%d”,k);
}
printf(“\n”);
}
getch();
}
======================================================
यहॉ प्रोग्राम में पहला for loop row के लिए है जबकि दूसरा for loop column के लिए है।
34
Write a Program to print
*
**
***
****
*****
#include<stdio.h>
#include<conio.h>
void
main()
{
int
j,k;
clrscr();
for(j=1;
j<=5; j++)
{
for(k=1;
k<=j; k++)
printf(“*”,k);
}
printf(“\n”);
}
getch();
}
======================================================
35 Write a Program to print reverse of digit 12345….54321
#include<stdio.h>
#include<conio.h>
void
main()
{
long
int num, num1;
clrscr();
printf(“Enter
5 digit number :”);
scanf(“%ld”,&num);
printf(“\nReverse
of Digit is : “);
while(num!=0)
{
num1=num%10;
num=num/10;
printf(“%ld”,num1);
}
getch();
}
======================================================
36
Write a Program to Factorial of given number 5 is 120 (1*2*3*4*5)
#include<stdio.h>
#include<conio.h>
void
main()
{
int
num,fact=1 ;
clrscr();
printf(“Enter
any number :”);
scanf(“%d”,&num);
printf(“\nFactorial
of Number “);
while(num>1)
{
fact=fact*num;
printf(“\n%d”,fact);
num=num-1;
}
getch();
}
======================================================
अध्याय 7
ऐरे Array
ऐरे एक ही तरह के डाटा टाइप के ऐलिमेंटस का समूह है। या हम कह सकते है कि एक ही तरह के डाटा टाइप का समूह है। यह एक कॉमन नाम के साथ डाटा को प्रतिनिधित्व करता है। जैसे
char name[20];
int
salary[10];
ऐरे निम्नलिखित प्रकार का होता है।
One – dimensional array
·
Two – dimensional
array
·
Multidimensional
array
One – dimensional array को देने का तरीका
type variable_name[size];
Example:
int
mark[10];
37 Write a program for one-dimensional array
#include<stdio.h>
#include<conio.h>
void
main()
{
int
num[5],i ;
clrscr();
printf(“Enter
5 elements :”);
for(i=0;i<5;
i++)
{
scanf(“%d”,&num[i]);
}
printf(“Your
entered :”);
for(i=0;i<5;i++)
{
printf(“%d”,num[i]);
}
getch();
}
======================================================
38
Write a program for one-dimensional array and sum of them
#include<stdio.h>
#include<conio.h>
void
main()
{
int
num[5],i ;
int
sum=0;
clrscr();
printf(“Enter
5 elements :”);
for(i=0;i<5;
i++)
{
scanf(“%d”,&num[i]);
}
printf(“Your
entered :”);
for(i=0;i<5;i++)
{
printf(“%d”,num[i]);
sum=sum+num[i];
}
getch();
}
======================================================
39
Write a program for two-dimensional two array
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a[2][2],b[2][2] ;
int
i,j;
clrscr();
printf(“Enter
2*2 matrix array 4 elements for A:”);
for(i=0;i<2;
i++)
{
for(j=0;j<2;j++)
{
scanf(“%d”,&a[i][j]);
}}
printf(“Your
entered :”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“%d”,a[i][j]);
}
printf(“\n”);
}
printf(“Enter
2*2 matrix array 4 elements for B:”);
for(i=0;i<2;
i++)
{
for(j=0;j<2;j++)
{
scanf(“%d”,&b[i][j]);
}}
printf(“Your
entered :”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“%d”,b[i][j]);
}
printf(“\n”);
}
getch();
}
======================================================
40
Write a program for sum of two matrix
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a[2][2],b[2][2];
int
sum[2][2];
int
i,j;
clrscr();
printf(“Enter
2*2 matrix array 4 elements for A:”);
for(i=0;i<2;
i++)
{
for(j=0;j<2;j++)
{
scanf(“%d”,&a[i][j]);
}}
printf(“Your
entered 2*2 A Matrix :”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“%d”,a[i][j]);
}
printf(“\n”);
}
printf(“Enter
2*2 matrix array 4 elements for B:”);
for(i=0;i<2;
i++)
{
for(j=0;j<2;j++)
{
scanf(“%d”,&b[i][j]);
}}
printf(“Your
entered 2*2 B Matrix :”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“%d”,b[i][j]);
}
printf(“\n”);
}
printf(“Sum
of 2*2 Matrix :”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
sum[i][j]=a[i][j]+b[i][j];
printf(“\t%d”,sum[i][j]);
}
printf(“\n”);
}
getch();
}
======================================================
41
Write a program for input 20 numbers 1-100 and find how many 1-9,10-19, 20-29,
30-39, 40-49…… 90-100
#define
max 20
#define
count 11
void
main()
{
float
value[max];
int
i,low,high;
int group[count]={0,0,0,0,0,0,0,0,0,0,0};
printf("\nEnter 20 numbers from 0-100 ");
for(i=0;
i<max; i++)
{
scanf(“%f”,&value[i]);
//counting
frequency of group
++group[(int)
(value[i])/10];
}
printf(“\n”);
printf("You have Entered %d ",value[i]);
printf(“
Group Range Frequency \n”);
for(i=0;
i<count; i++)
{
low=i*10;
if(i
== 10)
high
= 100;
else
high= low + 9;
printf(“
%2d %3d to %3d %d\n”,i+1, low, high,
group[i]);
}
getch();
}
======================================================
42
Write a program for table of 1-10 numbers
#define
r 10
#define
c 10
void
main()
{
int
row,col,prod[r][c];
int
i, j;
printf(“\n
Multiplication Table “);
printf(“
“);
for(j=1;
j<= c; j++)
printf(“\n”);
printf(“------------------------------------“);
for(i=0;
i<=r ;r++)
{
row=i
+ 1;
printf(“%2d
*”,row);
for(j=1;
j<=c ; j++)
{
col
= j;
prod[i][j]=
row*col;
printf(“%4d”,prod[i][j]);
}
printf(“\n”);
}
getch();
}
======================================================
अध्याय 8
कैरेक्टर ऐरेस और स्ट्रिंग
स्ट्रिंग कैरेक्टर का समूह है जिसे एकल डाटा के रुप में इस्तमाल किया जाता है। एक प्रोग्राम में स्ट्रिंग का इस्तेमाल किया जाता है। स्ट्रिंग निम्नलिखित काम करता है।
ऽ स्ट्रिंग को पढना और लिखना
ऽ स्ट्रिंग को एक साथ जोडना
ऽ एक स्ट्रिंग से दूसरे में कॉपी करना
ऽ स्ट्रिंग का तुलना करना
ऽ स्ट्रिंग से कुछ भाग को प्राप्त करना
स्ट्रिंग वेरियेबल का मान देना और स्टोर करना
char
string_name[size];
char
city[20];
char
name[20];
char
city[9]=”NEW DELHI”;
char
city[9]={‘N’,’E’,’W’,’D’,’E’,’L’,’H’,’I’,’\0’};
स्ट्रिंग को टर्मिनल से पढना
char
city[20];
scanf(“%s”,city);
43 Write a program for 3 text and print the text
#include<stdio.h>
#include<conio.h>
void
main()
{
char
city1[20],city2[20],city3[20];
clrscr();
printf(“Enter
3 text :”);
scanf(“%s
%s”,city1,city2);
scanf(“%s”,city3);
printf(“\n”);
printf(“city1=%s\n
city2=%s \ncity3=%s”,city1,city2,city3);
getch();
}
======================================================
scanf(“%ws”,city1);
scanf %s या %ws के द्वारा हम बिना स्पेस वाले स्ट्रिंग को पढते है। वैसे स्ट्रिंग जिसमें एक से ज्यादा वर्ड होते है उसे पढने के लिए
char
line[80];
scanf(“%[^\n]”,line);
printf(“%s”,line);
44
Write a program to print a line.
#include<stdio.h>
#include<conio.h>
void
main()
{
char
line[80];
printf(“\nEnter
a line \n”);
scanf(“%[^\n]”,line);
printf(“\n
You Entered :\n”,line);
getch();
}
======================================================
getchar और gets फंक्शन
char ch;
ch=getchar();
नोट getchar() फंक्शन में कोई भी पैरामीटर नही होता है।
45
Write a program for enter a line and print it.
#include<stdio.h>
#include<conio.h>
void main()
{
char line[81],line1;
int
n=0;
printf(“\nEnter
text .Press enter at end \n”);
do
{
line1=getchar();
line[n]=line1;
n++;
}
while(line1!=’\n’);
n=n-1;
line[n]=’\0’;
printf(“\n%s\n”,line);
getch();
}
======================================================
46 Write a program to print ASCII values for alphabets.
#include<stdio.h>
#include<conio.h>
void main()
{
char
c;
printf(“\n\n”);
for(c=65;c<=122;c++)
{
if(c>90
&& c<97)
continue;
printf(“*%4d
- %c”,c,c);
}
printf(“\n”);
getch();
}
======================================================
47
Write a program to print “MLC COLLEGE “ .
#include<stdio.h>
#include<conio.h>
void
main()
{
int
i,j;
char
string[]=”MLCComputer”;
printf(“\n\n”);
printf(“---------------------\n”);
for(i
= 0; i <= 10; i++)
{
j=i
+1;
printf(“|%-11.*s|\n”,j,string);
}
printf(“|-------------------*\n”);
for(i
= 10; i>= 0;i--)
{
j=i+1;
printf(“|
%-11.*s|\n”,j,string);
}
printf(“------------------“);
getch();
}
======================================================
परिचय
Structure and Union
स्ट्रक्चर जटिल डाटा को साधारण फार्मेट में प्रयोग किया जाता है। यह एक शक्तिशाली तरीका है जिसके माध्यम से हम प्रोग्राम बनाते है।
Structure लिखने का तरीका
struct
structure name
{
data
type variable name;
data
type variable name;
.
.
};
यदि Book database में book name, author, page number, price है तो
struct
book
{
char
title[20];
char
author[10];
int
page;
float
price;
};
Example
II
struct
stu
{
char
rollno[10];
char
stuname[20];
float
marks1;
float
marks2;
float
marks3;
};
======================================================
Program
#include<stdio.h>
#include<conio.h>
struct
personal
{
char
name[20];
int
day;
char
month[10];
int
year;
float
salary;
};
void
main()
{
struct
personal person;
clrscr();
printf(“\nInput
Data “);
printf(“\nInput
Name Day Month Year Salary “);
scanf(“%s
%d %s %d %f”,person.name
&person.day,
person.month,
&person.year,
&person.salary);
printf(“\nName=%s
\nDay=%d \nMonth=%s \nYear=%d \nSalary=%.2f “,
person.name,
person.day,
person.month,
person.year,
person.salary);
getch();
}
======================================================
Program
#include<stdio.h>
#include<conio.h>
struct
stu
{
char
name[20];
char
rollno[10];
char
class[10];
int
mark1;
int
mark2;
int
tot;
};
void
main()
{
struct
stu stu1;
clrscr();
printf(“\nInput
Data “);
printf(“\nInput
Name Roll Number Class Mark1 Mark2 “);
scanf(“%s
%s %s %d %d”,stu.name
stu.rollno,
stu.class,
&stu.mark1,
&stu.mark2);
tot=stu.mark1=stu.mrk2;
printf(“\nName=%s
\nRoll Number=%s \nClass=%s \nMark1=%d \nMark2=%d \nTotal=%d“,
stu.name,
stu.rollno,
stu.class,
stu.mark1,
stu.mark2,
stu.tot;
getch();
}
======================================================
Assignment Questions
C Array Range frequency
#include<stream.h>
#define maxvalue 10
#define counter 11
void main()
{
float value[maxvalue];
int i,low,high;
int group[counter]={0,0,0,0,0,0,0,0,0,0,0};
printf("Enter any 10 numbers :");
for(i=0;i<maxvalue;i++)
{
scanf("%f", &value[i]);
++group[(int)(value[i])/10];
printf("\n");
printf(" Group Range Frequency \n\n");
for(i=0;i<counter ; i ++)
{
low=i*10;
if(i==10)
high=100;
else
high=low+9;
printf("%2d %3d to %3d %d\n", i+1,low, high, group[i]);
}
getch();
}
=============================================================
Q1 Write a C Program to Add Two Integer
#include<stdio.h>
#include<conio>
void main()
{
int a,b,c;
clrscr();
printf("Enter any two numbers :");
scanf("%d%d",&a,&b);
c=a+b;
printf("Sum of two numbers is :%d ",c);
getch();
}
Q2 Write a C Program whether the number is Even or Odd
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter Number for Even or Odd ");
scanf("%d",&num);
if (num%2==0)
{
printf("Number %d is Even ",num);
}
else
{
printf("Number %d is Odd ",num);
}
getch();
}
Q3 Write a C Program to check Whether a Number is Positive or Negative or Zero
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter Number for Even or Odd ");
scanf("%d",&num);
if (num > 0)
{
printf("Number %d is Positive ", num);
}
else
{
printf("Number %d is Negative ", num);
}
getch();
}
if (num < 0 )
else
printf("Number %d is Zero " , num);
=====================================================================
Q4 Write a C Program to Display Fibonacci Series (0,1,1,2,3,5,8,13,21,......)
#include<stdio.h>
#include<conio.h>
void main()
{
int f,f1=0,f2=1;
clrscr();
printf(" Fibonacci Series :");
printf("\n%d%d",f1,f2);
f=f1+f2;
do
{
f2=f1;
f1=f;
f=f1+f2;
printf("\n%d",f);
}while(f<10);
getch();
}
Q5 Write a C Program to Reverse a Number
#include<stdio.h>
#include<conio.h>
void main()
{
long int num, num1;
clrscr();
printf(" Enter 5 Digit Number :");
scanf("%ld",&num);
printf("Reverse Number is \n ");
do
{
num=num/10;
num1=num%10;
printf("%ld",num1 );
}while(num>=1);
getch();
}
#include<conio.h>
void main()
{
long int num,num1,num2,revnum=0;
clrscr();
printf("Enter 5 Digit Number :");
scanf("%ld", &num);
num1=num;
do
{
num2=num1%10;
revnum=revnum*10+num2;
num1=num1/10;
}while(num1>=1);
if (revnum==num)
{
printf("\nNumber is Palindrome %ld ",num);
}
else
{
printf("Number is not Palindrome :%ld ",num);
}
getch();
}
==================================================================
Q7 Write a C Program to make a Simple Calculator to Add, Subtract, Multiply or Divide using Switch .. Case
#include<stdio.h>
#include<conio.h>
void main()
{int a,b,c,ch; char ans='y';
do
{clrscr();
printf("Enter your Choice 1/2/3/4 :");
printf("\n1.For Add\n2. For Subtract\n3.For Multiply\n4.For Divied \n");
scanf("%d",&ch);
printf("Enter any two numbers :");
scanf("%d%d",&a,&b);
switch (ch)
{case 1:
c=a+b;
printf("Sum of two Number is :%d ",c);
break;
case 2:
c=a - b;
printf("Subtraction of two Numbers is :%d ",c);a
break;
case 3:
c=a * b;
printf("Multiply of two Numbers is :%d ",c);
break;
case 4:
c=a / b;
printf("Division of two Numbers is :%d ", c);
break;
}
default:
printf("Wrong Choice ......");
break;
}printf("Do you want to continue.....y/n ");
scanf("%s",&ans);
}while (ans=='y' || ans=='Y');
}
===================================================================
Q8 Write a C Program to Calculate Factorial of a Number using Recursion (Example 5=120)
#include<stdio.h>
#include<conio.h>
factor(int n)
Q9 Write a Program to Calculate Average using Array
#include<stdio.h>
#include<conio.h>
void main()
{
int num[10],sum=0,avg,i;
printf("Enter 10 numbers :");
//Input data
for(i=0;i<10;i++)
{
scanf("%d",&num);
}
//Print the data
printf("\nList of Array :");
for(i=0;i<10;i++)
{
printf("\n%d",num[i]);
}
//Sum and Average of data
for(i=0;i<10;i++)
{
sum=sum+num[i];
avg=sum/10;
}
printf("\nSum of 10 Numbers is %d :",sum);
printf("\nAverage of 10 Numbers is %d :,avg);
getch();
}
=======================================================================
Q10 Write a C Program to Add two Matrix Using Multi Demission Array
#include<stdio.h>
#include<conio.h>
void main()
{
int A[2][2], B[2][2], Sum[2][2], i, j;
printf("Enter 2*2 Matrix for A :");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&A[i][j]);
}}
printf("\nPrint 2*2 Matrix for A :\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t %d", A[i][j]);
}
printf("\n");
}
printf("\nEnter 2*2 Matrix for B :");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d", &B[i][j]);
}}
printf("\nPrint 2*2 Matrix for B :\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t %d", B[i][j]);
}
printf("\n");
}
printf("\nSum of 2*2 Matrix A & B \n:");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
Sum[i][j]=A[i][j]+B[i][j];
}}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t %d", Sum[i][j]);
}
printf("\n");
}
getch();
}
Q11 Write a C program to Swap Numbers in Cyclic Order using call by Reference....
#include<stdu=io.h>
#include<conio.h>
//global define function
void swap(int *,int *);
void main();
{
int a,b,c;
clrscr();
printf("Enter any two numbers for swap :");
scanf("%d%d",&a,&b);
printf("\n Before Swapping the numbers are a=%d & b=%d \n", a , b);
swap(&a, &b);
printf("After swapping the numbers are a=%d & b=%d\n", a, b);
getch();
}
void swap(int *a, int *b)
{
int c;
c=*a;
*a=*b;
*b=c;
printf(" After swapping numbers in function a=%d, b=%d \n",a,b);
}
================================================================