0% found this document useful (0 votes)
32 views17 pages

Unit II - Programs in C For BA 1st Year

This document provides an overview of various programming concepts in the C language for a first year computer application course. It includes: 1. Common operators used in C like arithmetic, relational, logical, assignment, increment/decrement, and bitwise operators. 2. Input/output functions in C for single character, string, formatted, and file input/output. 3. Common control structures in C like decision/conditional statements, loops, jumping statements. 4. Examples of simple C programs for tasks like printing text, getting user input, calculations.

Uploaded by

mariejoy040223
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views17 pages

Unit II - Programs in C For BA 1st Year

This document provides an overview of various programming concepts in the C language for a first year computer application course. It includes: 1. Common operators used in C like arithmetic, relational, logical, assignment, increment/decrement, and bitwise operators. 2. Input/output functions in C for single character, string, formatted, and file input/output. 3. Common control structures in C like decision/conditional statements, loops, jumping statements. 4. Examples of simple C programs for tasks like printing text, getting user input, calculations.

Uploaded by

mariejoy040223
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Programming in C language – B.A.

1st Year (Computer Application)


Unit II

Contents
Operators Single Character Input/ Output Function

1. Arithmetic Operator +, -, *, /, % 1. getchar() function


2. Relational Operator <,>,<=,>=,==,!= 2. putchar() function
3. Logical Operator &&, II, ! 3. getch() function
4. Conditional Operator- exp1?exp2:exp3 4. putch() function
5. Assignment Operator = 5. getche() function
6. Compound Assignment Operator
String Input/ Output Function
+=, -=,*=, /=, %=, <<=, >>=,&=, \=, ^=
7. Increment & Decrement Operator ++,-- 1. gets() function
8. Bitwise Operator 2. puts() function

Operator Symbol Formatted Input/ Output Function


AND &
OR I 1. scanf() function
XOR ^ 2. printf() function
Complement ~
Decision Control Statement/Conditional
Shift left <<
Shift Right >> Statement

1. If Statement
2. If_else statement
3. Nested if_else Statement
4. Switch case Statement

Jumping Statement or Breaking Control


Statement

1. Break Statement
2. Continue Statement
3. goto Statement

Loop Control Statement

1. for loop & Nested for loop


2. While loop
3. do_while loop

Prof. Gaurav Gupta Page | 1


Programming in C language – B.A. 1st Year (Computer Application)
Que1 Hello print करने के लिये C मे प्रोग्राम बनाइए। Que4 Simple Interest (साधारण ब्याज) निकालने के लिये C मे
#include<stdio.h> प्रोग्राम बनाइए।
#include<conio.h> #include<stdio.h>
void main() #include<conio.h>
{ void main()
printf("Hello"); {
getch(); float si, p, r, t;
clrscr();
}
printf("Enter Principal, Rate and Time:");
Output- Hello
scanf("%f%f%f",&p,&r,&t);
++++++++++++++++++++++++++++++++++++++ si=(p*r*t)/100;
Que2 Age को Enter करवाने एवं display करवाने के लिये प्रोग्राम printf("simple Intrest is: %f",si);
लिख़िए getch();
#include<stdio.h> }
Input- Enter Principal Rate and Time: 5000 10 2
#include<conio.h> Output- Simple interest is: 1000
void main() ++++++++++++++++++++++++++++++++++++++
{ Arithmatic Operator Example-
int age; Que5 Program for Addition, Subtraction,
clrscr(); Multiplication and Division of two numbers
printf("Enter Your Age"); #include<stdio.h>
scanf("%d",&age); #include<conio.h>
printf("Your age is: %d",age); void main()
getch(); {
} int a,b,add,sub,multi,division,modulo;
Input- Enter Your Age:25 printf("Enter the two number\n");
Outpur- Your Name Age is:25 scanf("%d%d",&a,&b);
+++++++++++++++++++++++++++++++++++ add=a+b;
Que3 Name को Enter करवाने एवं print करवाने के लिये प्रोग्राम sub=a-b;
लिख़िए | multi=a*b;
#include<stdio.h> division=a/b;
#include<conio.h> modulo=a%b;
void main()
{ printf("Addition is %d \n", add);
char name[30]; printf("subtraction is %d \n", sub);
printf("Enter Your Name:"); printf("Multiplication is %d \n", multi);
scanf("%s",&name); printf("Division is %d \n", division);
printf("Your Name is: %s",name); printf("Remainder after division is %d \n",
getch(); modulo);
} getch();
Input- Enter Your Name: Gaurav }
Outpur- Your Name is: Gaurav Input- Enter two number 9 5
Output- Addition is 14
Subtraction is 4
Multiplication is 45
Division is 1
Remainder after division is 4

Prof. Gaurav Gupta Page | 2


Programming in C language – B.A. 1st Year (Computer Application)

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

Prof. Gaurav Gupta Page | 3


Programming in C language – B.A. 1st Year (Computer Application)
Que10 Example of Decrement Operator Logical Operator Example
#include<stdio.h>
#include<conio.h> Que11 इनपुट किए गए तीन नंबरों मे से बड़ा नंबर निकालने के लिये C मे
प्रोग्राम लिखिए।
void main()
{ #include<stdio.h>
int i; void main()
printf("enter a number\n"); {
scanf ("%d",&i); int a,b,c;
printf("Enter the value of a b and c");
i--;
scanf("%d%d%d",&a,&b,&c);
printf("The Decrement Value of i=%d",i);
if(a>=b && a>=c)
getch (); {
} printf("A is greater");
Input- Enter a number- 5 }
Output- The Increment Value of= 4 else
+++++++++++++++++++++++++++++++++++++ {
if(a<=b && b>=c)
Conditional Operator or Ternary Operator
{
Example- printf("B is greater");
Que11 Find Even or Odd Number using }
Ternary operator or Conditional operator (?:) else
#include<stdio.h> {
#include<conio.h> printf("C is greater");
void main() }
{ }
}
int n;
printf("Enter a number\ n"); Input- Enter the value of a b and c
scanf("%d",&n); 10 45 30
(n%2==0)?printf("Even number\n"): printf("Odd Output- B is greater
number\n"); +++++++++++++++++++++++++++++++++
getch ();
}
Input- Enter a Number 7
Output- Odd Number
++++++++++++++++++++++++++++++++

Prof. Gaurav Gupta Page | 4


Programming in C language – B.A. 1st Year (Computer Application)
Single Character Input/ Output Function

getchar() Function- यह c प्रोग्रामिंग में इनपुट फं क्शन है। हम कं सोल से इस getchar फ़ं क्शन का उपयोग करके के वल एक ही character
पढ़ सकते हैं |

syntax- character_variable= getchar();


example- char ch;
ch=getchar();

putchar() Function- यह c प्रोग्रामिंग में आउटपुट फं क्शन है जो एक character या variable के मान को स्क्रीन पर प्रिंट करने के लिये
putchar() function का प्रयोग करते है |
Syntax- putchar(character_variable);

Example- char ch= ‘G’


putchar(ch);

Program- #include <stdio.h>


void main( )
{
char ch;
printf("Enter a character: ");
ch=getchar();
printf("You have entered a character: ");
putchar(ch);
}
Input- Enter a character: G
Output-You have entered a character: G
----------------------*----------------------------------------------------
getch() Function- getch() एक library function है जिसका उपयोग की- बोर्ड से एक अक्षर का input लेने के लिए किया जाता
है।
syntax- character_variable= getch();

example- char ch;


ch= getch();

putch() Function- putch() function का उपयोग Monitor पर एक अक्षर प्रदर्शित करने के लिये किया जाता है ।
syntax- putch(character_variable);

example- char ch;


ch= getch();
putch(ch);

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");

}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

String Input/ Output Function


1. gets() function
2. puts() function

Prof. Gaurav Gupta Page | 6


Programming in C language – B.A. 1st Year (Computer Application)
gets() function- gets() function का उपयोग की बोर्ड से enter किये गये अक्षरों के string को पढ़ने मैं किया जाता है तथा उन्हे
string variable मे बताये गये अनुसार white space के साथ रखता है।
syntax- gets(string variable);

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);

Decision Control Statement

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

1 if statement- एक if statement एक विशेष 2 if_else statement-


condition का परीक्षण करता है यदि condition सत्य होती है Syntax- if(condition)
तो एक स्टेटमेंट या स्टेटमेंट का समुह संचालित होता है । {
syntax- if(condition) Statement;
{ }
Statement; else
{
}
Statement;
}
Flowchart of if 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

3 Nested if_else statement- Flowchart of Nested if statement-


syntax- if(condition)
{
if(condition)
{
Statement;
}
else
{
Statement;
Prof. Gaurav Gupta} Page | 8
}
Programming in C language – B.A. 1st Year (Computer Application)

Flowchart of Switch Statement Example2 of Switch Statement-

#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)

Jump or Breaking Control Statement

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)

2 Continue Statement- Example of continue statement-


सी लैंग्वेज में कं टिन्यू स्टेटमेंट का इस्तेमाल प्रोग्राम कं ट्रोल को लूप की शुरुआत #include<stdio.h>
में लाने के लिए किया जाता है। continue statement लूप के अंदर void main()
कोड की कु छ पंक्तियों को छोड़ देता है और अगले पुनरावृत्ति के साथ जारी रहता {
है। यह मुख्य रूप से एक शर्त के लिए उपयोग किया जाता है ताकि हम किसी int i;
विशेष स्थिति के लिए कु छ कोड छोड़ सकें । //starting a loop from 1 to 10
Syntax- continue; for(i=1;i<=10;i++)
{
Flowchart of continue statement-
if(i==5)
Prof. Gaurav Gupta Page | 11 {
//if value of i is equal to 5, it will continue
Programming in C language – B.A. 1st Year (Computer Application)

3 goto statement- Example of goto statement-

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)

Loop Control Statement


Looping Statements- Program में किसी condition के true रहने तक किसी block की पुनरावृत्ति होने को लूपिंग कहते है |
जब प्रोग्राम में एक या एक से अधिक statements के समूह को एक या एक से अधिक बार किसी विशेष condition के true रहने तक execute
कराना हो तब हम looping का उपयोग करते है | C language में तीन प्रकार के लूप उपयोग में लाये जाते है –
1. for loop
2. While loop
3. do_while loop

1 for loop-for statement एक loop statement है जो Flowchart of Nested for loop-


की बार बार क्रियान्वित होता रहता है जब तक की इसमें दी गयी condition
false नही हो जाती है |
Syntax –
for(initialization; condition; increment/decrement)
{
Prof. Gaurav Gupta Page | 13
Block of Statements;
}
Programming in C language – B.A. 1st 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- }

Input- Enter any number for table: 5


Output- 5 10 15 20 25 30 35 40 45 50
+++++++++++++++++++++++++++++++++
Prof. Gaurav Gupta Page | 16 Example 3- While loop का प्रयोग करते हुए Input किए गये नम्बर का
factorial निकालने के लिए प्रोग्राम लिखिए ।
Programming in C language – B.A. 1st Year (Computer Application)

3 do_While loop- Example 1- do_while loop का उपयोग करते हुये 1 से 10 तक नम्बर


do statement एक exit control loop है। इसे do_while या प्रिंट करने के लिये प्रोग्राम लिखिये|
do_while loop भी कहते है । इसमे सबसे पहले दी गयी loop body
execute होती है, और उसके पश्चात condition check की जाती है। #include<stdio.h>
यदि condition सत्य होती है तो loop body अर्थात block of void main()
statement पुन: execute होते है और तब तक बार बार execute {
होते रहते है, जब तक कि दी गयी condition false नही हो जाती है । int i;
इस loop मे एक खास बात यह भी है की यदि पहली बार मे दी गयी i=1;
condition असत्य भी हो, तब भी loop body एक बार execute do
अवश्य हो जाती है। {
printf("\t %d",i);
i++;
Syntax-
}
initialization;
while(i<=10);
do
}
{
block of statements;
Output-1 2 3 4 5 6 7 8 9 10
increment/decrement;
+++++++++++++++++++++++++++++
} while(condition);
Example 2- do_while loop का उपयोग करते हुये इनपुट किए गए नम्बर कि
Statement-x;
table प्रिंट करने के लिये प्रोग्राम लिखिये

Flowchart of do_while loop- #include<stdio.h>


void main()
Prof. Gaurav Gupta Page | 17 {
int i,num, result;
i=1;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy