Unit II - Programs in C For BA 1st Year
Unit II - Programs in C For BA 1st Year
Contents
Operators Single Character Input/ Output Function
1. If Statement
2. If_else statement
3. Nested if_else Statement
4. Switch case Statement
1. Break Statement
2. Continue Statement
3. goto Statement
Que6 Compound Interest (चक्रव्रध्दि ब्याज) निकालने के लिये Que7 Third Variable का उपयोग करते हुए Two Values को
C मे प्रोग्राम बनाइए। आपस मे exchange करने के लिये C मे प्रोग्राम बनाइए।
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<conio.h>
#include<math.h>
void main()
int main()
{
{
int a,b,c;
float p,r,t,ci;
clrscr();
clrscr();
printf("enter two values\n");
printf("Enter Principal,Rate and Time\n");
scanf("%d%d",&a,&b);
scanf("%f%f%f",&p,&r,&t);
printf ("before swapping a=%d and b=%d",a,b);
ci =(p*pow(1+r/100,t))-p;
c=a;
printf("Compound Interest = %f\n",ci);
a=b;
getch();
b=c;
return 1;
printf("after swapping Values are a=%d and b=
}
%d",a,b);
Input-
getch();
Enter Principal Rate and Time
}
5000
Input- Enter Two Values 10
10
20
2
Output- After Swapping Values are a=20 and
Output-
b=10
Compound Interest =1050.000
+++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++ Que8 Third Variable का उपयोग किए बिना Two Values को
Que9 Example of Increment Operator
आपस मे exchange करने के लिये C मे प्रोग्राम बनाइए।
#include<stdio.h>
#include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
int i; int a,b;
printf("enter two values\n");
printf("enter a number\n");
scanf("%d%d",&a,&b);
scanf ("%d",&i); printf("before swapping a=%d and b=%d",a,b);
i++; //++i; a=a+b;
printf("The Increment value of i=%d",i); b=a-b;
getch (); a=a-b;
printf ("after swapping a=%d and b=%d",a,b);
}
getch();
Input- Enter a number- 5 }
Output- The Increment Value of= 6
Input- Enter two values: 25 15
Output- after swapping: a=15 b=25
getchar() Function- यह c प्रोग्रामिंग में इनपुट फं क्शन है। हम कं सोल से इस getchar फ़ं क्शन का उपयोग करके के वल एक ही character
पढ़ सकते हैं |
putchar() Function- यह c प्रोग्रामिंग में आउटपुट फं क्शन है जो एक character या variable के मान को स्क्रीन पर प्रिंट करने के लिये
putchar() function का प्रयोग करते है |
Syntax- putchar(character_variable);
putch() Function- putch() function का उपयोग Monitor पर एक अक्षर प्रदर्शित करने के लिये किया जाता है ।
syntax- putch(character_variable);
Program1- #include<stdio.h>
void main()
{
Prof. Gaurav Gupta Page | 5
Programming in C language – B.A. 1st Year (Computer Application)
char var1;
printf("Enter The Character:");
var1 = getch(); //Not getting echo to the screen
printf("Character was %c",var1);
}
Program2- #include<stdio.h>
void main()
{
char var1;
printf("Enter The Character>");
var1 = getchar(); //using getchar() function
putch(var1);
}
------------------*-------------
getche() Function- getche() का उपयोग की- बोर्ड से एक अक्षर का input लेने के लिए किया जाता है। इसमें इंटर की दबाने की
आवश्यकता नहीं होती है ।
syntax- character_variable=getche();
example- char ch;
ch= getche();
Program3- #include<stdio.h>
void main
{
char var1;
printf("Enter The Character:");
var1 = getche(); // echo to the screen
printf("Character was %c",var1");
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
puts() function- puts() function का प्रयोग string को स्क्रीन print कराने के लिए किया जाता है। puts() व printf() मे मुख्य
अंतर यह है कि puts() स्वत: हि new line character को print करता है जबकि printf() मे यदि newline नहीं दि है तो वह new
line screen पर print नहीं करता है ।
syntax- puts(string variable);
Program1- Reading string using gets() Program2- example to read a string using gets()
#include<stdio.h> and print it on the console using puts().
void main () #include<stdio.h>
{ void main()
char name[30]; {
printf("Enter the string"); char name[50];
gets(name); printf("Enter your name: ");
printf("You entered: %s",name); gets(name); //reads string from user
} printf("Your name is: ");
puts(name); //displays string
Output- }
Enter the string: Gupta
You entered: Gupta Output-
Enter Your Name: Gaurav
Your Name is: Gaurav
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Formatted Input/ Output Function
1. scanf() function
2. printf() function
scanf() function- इस function का उपयोग अंकीय मान, एक अक्षर, तथा स्ट्रिंग के मिश्रण को पढने कै लिये किया जाता है । यदि विभिन्न प्रकार के
data type के एक से अधिक variable कों पढ़ना है तो के वल एक scanf() स्टेटमेंट का उपयोग किया जा सकता है ।
syntax- scanf(“format string”, &list of variables);
printf() function- इस function का उपयोग करके वेरिएबल के मान को प्रदर्शित या प्रिंट करने के लिए किया जाता है ।
syntax- prinf(“format string”, list of variable);
1. If Statement
2. If_else statement
3. Nested if_else Statement
4. Switch case Statement
5. Break Statement
6. Continue Statement
Prof. Gaurav Gupta Page | 7
Programming in C language – B.A. 1st Year (Computer Application)
7. goto Statement
Example- इनपुट किये गये नंबर को चेक कीजिये कि नम्बर even (सम) है
या odd (विषम) के लिये Program लिखिये।
#include<stdio.h>
void main()
Example- {
#include<stdio.h> int a;
Void main() printf("Enter a Number:");
{ scanf(" %d",&a);
int marks; if(a%2==0)
printf(“Enter Marks Scored in Test:”); {
printf("a is even no");
scanf(“%d”, &marks);
}
if(marks>=35) else
{ {
Printf(“Admission Granted”); printf("a is odd no");
} }
} }
Input- Enter a Number: 5
Output- a is odd no
#include<stdio.h>
void main()
{
char grade = 'B';
switch(grade)
{
case 'A' :
Prof. Gaurav Gupta Page | 9 printf("Excellent! Your Grade is A\n" );
break;
case 'B' :
Programming in C language – B.A. 1st Year (Computer Application)
1 break Statement-
Example2 break statement with while loop-
ब्रेक सी में एक कीवर्ड है जिसका उपयोग प्रोग्राम नियंत्रण को लूप से बाहर लाने
के लिए किया जाता है। ब्रेक स्टेटमेंट का उपयोग लूप या स्विच स्टेटमेंट के #include<stdio.h>
अंदर किया जाता है। ब्रेक स्टेटमेंट लूप को एक-एक करके तोड़ता है, यानी
void main ()
{
नेस्टेड लूप के मामले में, यह पहले आंतरिक लूप को तोड़ता है और फिर बाहरी
int i=1;
लूप में जाता है। C में ब्रेक स्टेटमेंट का उपयोग निम्नलिखित दो परिदृश्यों में
while(i<=10)
किया जा सकता है: {
1 With Switch case printf("%d",i);
Prof.2Gaurav Gupta Page | 10
With loop i++;
if(i==8)
Programming in C language – B.A. 1st Year (Computer Application)
goto स्टेटमेंट को सी में जंप स्टेटमेंट के रूप में जाना जाता है। जैसा कि include<stdio.h>
नाम से पता चलता है, goto का उपयोग प्रोग्राम नियंत्रण को पूर्वनिर्धारित void main()
लेबल में स्थानांतरित करने के लिए किया जाता है। किसी विशेष स्थिति के {
लिए कोड के कु छ भाग को दोहराने के लिए goto स्टेटमेंट का उपयोग किया int i,sum=0;
जा सकता है। इसका उपयोग कई लूपों को तोड़ने के लिए भी किया जा सकता for(i=0; i<=10; i++)
है जो एकल ब्रेक स्टेटमेंट का उपयोग करके नहीं किया जा सकता है। हालाँकि, {
इन दिनों goto का उपयोग करने से बचा जाता है क्योंकि यह program sum = sum+i;
if(i==5)
को कम readable और जटिल बनाता है।
{
Syntax- goto label_name;
goto addition;
----------------
---------------- }
label_name: statements; }
Prof. Gaurav Gupta Page | 12 addition:printf("%d", sum);
}
Flowchart of goto Statement-
Output-15
st
Programming in C language – B.A. 1 Year (Computer Application)
Example2- for loop का उपयोग करते हुये इनपुट किए गए नम्बर कि table Nested for loop example
प्रिंट करने के लिये प्रोग्राम लिखिये | Example 4- निम्न PYRAMID प्रिंट करने के लिए प्रोग्राम लिख़िए ।
#include<stdio.h> *
void main() **
{ ***
int i,num,result; ****
printf("Enter any number for table"); *****
scanf("%d",&num);
printf("Table is:"); #include<stdio.h>
for(i=1;i<=10;i++) void main()
{ {
result=num*i; int i, j;
printf("%d",result); for(i=1;i<=5;i++)
} {
} for(j=1;j<=i;j++)
Input- Enter any number for table: 5 {
Output- Table is: 5 10 15 20 25 30 35 40 45 50 printf("*");
++++++++++++++++++++++++++++++++++++ }
Prof. Gaurav Gupta Page | 14
Nested for loop example printf("\n");
Example3- निम्न PYRAMID प्रिंट करने के लिए प्रोग्राम लिख़िए । }
Programming in C language – B.A. 1st Year (Computer Application)
Example 6- Input किए गये नम्बर का factorial निकालने के Nested for loop example
लिए प्रोग्राम लिखिए । Example 8- निम्न PYRAMID प्रिंट करने के लिए प्रोग्राम लिख़िए ।
#include<stdio.h> 54321
void main() 5432
{ 543
int i,fact=1,number; 54
printf("Enter a number: "); 5
scanf("%d",&number);
for(i=1; i<=number;i++) #include<stdio.h>
{ void main()
fact=fact*i; {
} int i, j;
printf("Factorial is: %d",fact); for(i=1;i<=5;i++)
} {
for(j=5;j>=i;j--)
Input- Enter a number: 5 {
Output: Factorial is: 120 printf("%d",j);
}
5! = 5*4*3*2*1 = 120 printf("\n");
+++++++++++++++++++++++++++++++ }
Prof. Gaurav Gupta Page | 15
Nested for loop example }
Example 7- निम्न PYRAMID प्रिंट करने के लिए प्रोग्राम लिख़िए ++++++++++++++++++++++++++++++++
Programming in C language – B.A. 1st Year (Computer Application)
2 While loop- Example 2- while loop का उपयोग करते हुये इनपुट किए गए नम्बर कि
table प्रिंट करने के लिये प्रोग्राम लिखिये |
while statement या while loop एक entry control loop है । इस #include<stdio.h>
loop मे सबसे पहले condition को check किया जाता है यदि दि गयी void main()
condition true होती हैं, तो loop body क्रियान्वित होती है । यह बार बार {
execute होती रहती है जब तक कि दी गयी condition false नही हो जाती है । int i, num, result;
Syntax- i=1;
printf("Enter any number for table:");
initialization; scanf("%d",&num);
While(condition) while(i<=10)
{ {
block of statement; result=i*num;
increment/decrement; i++;
} printf("%d",result);
Statement-x; printf("\t");
}
Flowchart of While loop- }