SlideShare a Scribd company logo
C Programming Lab
BCA 105
/* C program to print Hello World! */
#include <stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
C lab programs
/* C program to print Hello World using function */
#include <stdio.h>
// function to print Hello World!
void printMessage(void)
{
printf("Hello World!");
}
int main()
{
//calling function
printMessage();
return 0;
}
C lab programs
/* C Program to find sum of two numbers */
#include<stdio.h>
int main() {
int a, b, sum;
printf("n Enter two no: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum : %d", sum);
return(0);
}
C lab programs
/* Check Whether Number is Prime or not */
#include<stdio.h>
int main()
{
int num, i, count = 0;
printf("Enter a number:");
scanf("%d", &num);
for (i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
count++;
break;
}
}
if (count == 0)
printf("%d is a prime number", num);
else
printf("%d is not a prime number", num);
return 0;
}
C lab programs
/* Factorial of a Number*/
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
// shows error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
return 0;
}
C lab programs
/* Swap Numbers Using Temporary Variable*/
#include<stdio.h>
int main()
{
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
// Value of first is assigned to temp
temp = first;
// Value of second is assigned to first
first = second;
// Value of temp (initial value of first) is assigned to second
second = temp;
printf("nAfter swapping, firstNumber = %.2lfn", first);
printf("After swapping, secondNumber = %.2lf", second);
return 0;
}
C lab programs
/* C Program to generate the Fibonacci Series starting from any two numbers*/
#include<stdio.h>
int main()
{
int first, second, sum, num, counter = 0;
printf("Enter the term : ");
scanf("%d", &num);
printf("n Enter First Number : ");
scanf("%d", &first);
printf("n Enter Second Number : ");
scanf("%d", &second);
printf("n Fibonacci Series : %d %d ", first, second);
while (counter < num)
{
sum = first + second;
printf("%d ", sum);
first = second;
second = sum;
counter++;
}
return (0);
}
C lab programs
/* C Program to find greatest in 3 numbers*/
#include<stdio.h>
int main()
{
int a, b, c;
printf("nEnter value of a, b & c : ");
scanf("%d %d %d", &a, &b, &c);
if ((a > b) && (a > c))
printf("na is greatest");
if ((b > c) && (b > a))
printf("nb is greatest");
if ((c > a) && (c > b))
printf("nc is greatest");
return(0);
}
•
C lab programs
/* Program to relate two integers using =, > or < symbol using C if...else Ladder*/
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if the two integers are equal.
if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
}
//checks if both test expressions are false
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
C lab programs
/* Example of while loop*/
#include <stdio.h>
int main()
{
int count=1;
while (count <= 4)
{
printf("%d ", count);
count++;
}
return 0;
}
C lab programs
/* do...while loop in C*/
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
do {
printf("value of a: %dn", a);
a = a + 1;
}while( a < 20 );
return 0;
}
C lab programs
/* for loop in C*/
#include <stdio.h>
int main () {
int a;
/* for loop execution */
for( a = 10; a < 20; a = a + 1 ){
printf("value of a: %dn", a);
}
return 0;
}
C lab programs
/*Nested For Loop in C*/
#include <stdio.h>
int main()
{
for (int i=0; i<2; i++)
{
for (int j=0; j<4; j++)
{
printf("%d, %dn",i ,j);
}
}
return 0;
}
C lab programs
/* Switch Statement in C*/
1. #include <stdio.h>
2. int main ()
3. {
4. /* local variable definition */
5. char grade = 'B';
6. switch(grade) {
7. case 'A' :
8. printf("Excellent!n" );
9. break;
10. case 'B' :
11. case 'C' :
12. printf("Well donen" );
13. break;
14. case 'D' :
15. printf("You passedn" );
16. break;
17. case 'F' :
18. printf("Better try againn" );
19. break;
20. default :
21. printf("Invalid graden" );
22. }
23. printf("Your grade is %cn", grade );
24. return 0;
25. }
C lab programs
1. /* continue statement in C*/
2. #include <stdio.h>
3. int main ()
4. {
5. /* local variable definition */
6. int a = 10;
7.
8. /* do loop execution */
9. do {
10.
11. if( a == 15) {
12. /* skip the iteration */
13. a = a + 1;
14. continue;
15. }
16.
17. printf("value of a: %dn", a);
18. a++;
19.
20. } while( a < 20 );
21.
22. return 0;
23. }
C lab programs
/* Example of goto statement*/
1. #include <stdio.h>
2. int main()
3. {
4. int sum=0;
5. for(int i = 0; i<=10; i++){
6. sum = sum+i;
7. if(i==5){
8. goto addition;
9. }
10. }
11. addition:
12. printf("%d", sum);
13. return 0;
14. }
• Output:
• 15
• Explanation: In this example, we have a label addition and when the value of i
(inside loop) is equal to 5 then we are jumping to this label using goto. This is
reason the sum is displaying the sum of numbers till 5 even though the loop is set
to run from 0 to 10.
C lab programs
/* prime Numbers between Two Integers using function*/
1. #include <stdio.h>
2. int checkPrimeNumber(int n);
3. int main()
4. {
5. int n1, n2, i, flag;
6. printf("Enter two positive
integers: ");
7. scanf("%d %d", &n1, &n2);
8. printf("Prime numbers
between %d and %d are: ", n1,
n2);
9. for (i = n1 + 1; i < n2; ++i)
10. {
11. // flag will be equal to 1 if i
is prime
12. flag =
checkPrimeNumber(i);
13. if (flag == 1)
14. printf("%d ", i);
15. }
16. return 0;
17. }
18. // user-defined function to
check prime number
19. int checkPrimeNumber(int n)
20. {
21. int j, flag = 1;
22. for (j = 2; j <= n / 2; ++j) {
23. if (n % j == 0) {
24. flag = 0;
25. break;
26. }
27. }
28. return flag;
29. }
C lab programs
/* Sum of Natural Numbers Using Recursion*/
1. #include <stdio.h>
2. int sum(int n);
3. int main()
4. {
5. int number, result;
6. printf("Enter a positive integer: ");
7. scanf("%d", &number);
8. result = sum(number);
9. printf("sum = %d", result);
10. return 0;
11. }
12. int sum(int n)
13. {
14. if (n != 0)
15. // sum() function calls itself
16. return n + sum(n-1);
17. else
18. return n;
19. }
C lab programs
/* Function call by Value in C*/
1. #include <stdio.h>
2. /* function declaration */
3. void swap(int x, int y);
4. int main ()
5. {
6. /* local variable definition */
7. int a = 100;
8. int b = 200;
9. printf("Before swap, value of a : %dn", a );
10. printf("Before swap, value of b : %dn", b );
11. /* calling a function to swap the values */
12. swap(a, b);
13. printf("After swap, value of a : %dn", a );
14. printf("After swap, value of b : %dn", b );
15. return 0;
16. }
17. void swap(int x, int y)
18. {
19. int temp;
20. temp = x; /* save the value of x */
21. x = y; /* put y into x */
22. y = temp; /* put temp into y */
23. return;
24. }
C lab programs
• /* Function call by reference in C*/
1. #include <stdio.h>
2. int main ()
3. {
4. /* local variable definition */
5. int a = 100;
6. int b = 200;
7. printf("Before swap, value of a : %dn", a );
8. printf("Before swap, value of b : %dn", b );
9. /* calling a function to swap the values */
10. swap(&a, &b);
11. printf("After swap, value of a : %dn", a );
12. printf("After swap, value of b : %dn", b );
13.
14. return 0;
15. }
16. void swap(int *x, int *y)
17. {
18. int temp;
19. temp = *x; /* save the value of x */
20. *x = *y; /* put y into x */
21. *y = temp; /* put temp into y */
22. return;
23. }
C lab programs
• /* C - Arrays*/
1. #include <stdio.h>
2.
3. int main () {
4.
5. int n[ 10 ]; /* n is an array of 10 integers */
6. int i,j;
7.
8. /* initialize elements of array n to 0 */
9. for ( i = 0; i < 10; i++ ) {
10. n[ i ] = i + 100; /* set element at location i to i + 100 */
11. }
12.
13. /* output each array element's value */
14. for (j = 0; j < 10; j++ ) {
15. printf("Element[%d] = %dn", j, n[j] );
16. }
17.
18. return 0;
19. }
•
C lab programs
• /* Simple Two dimensional (2D) Array Example*/
1. #include<stdio.h>
2. int main()
3. { /* 2D array declaration*/
4. int disp[2][3];
5. /*Counter variables for the loop*/
6. int i, j;
7. for(i=0; i<2; i++) {
8. for(j=0;j<3;j++) {
9. printf("Enter value for disp[%d][%d]:", i, j);
10. scanf("%d", &disp[i][j]);
11. } }
12. //Displaying array elements
13. printf("Two Dimensional array elements:n");
14. for(i=0; i<2; i++) {
15. for(j=0;j<3;j++) {
16. printf("%d ", disp[i][j]);
17. if(j==2){
18. printf("n");
19. } } }
20. return 0;
21. }
C lab programs
/* Store Information in Structure and Display it*/
1. #include <stdio.h>
2. struct student {
3. char firstName[50];
4. int roll;
5. float marks; } s[10];
6. int main()
7. {
8. int i;
9. printf("Enter information of
students:n");
10. // storing information
11. for (i = 0; i < 5; ++i) {
12. s[i].roll = i + 1;
13. printf("nFor roll
number%d,n", s[i].roll);
14. printf("Enter first name: ");
15. scanf("%s", s[i].firstName);
16. printf("Enter marks: ");
17. scanf("%f", &s[i].marks);
18. }
19. printf("Displaying
Information:nn");
20. // displaying information
21. for (i = 0; i < 5; ++i) {
22. printf("nRoll number:
%dn", i + 1);
23. printf("First name: ");
24. puts(s[i].firstName);
25. printf("Marks: %.1f",
s[i].marks);
26. printf("n");
27. }
28. return 0;
29. }
C lab programs
Write a 'C' program to accept customer details such as: Account_no, Name, Balance using structure. Assume
3 customers in the bank. Write a function to print the account no. and name of each customer whose balance
< 100 Rs.
#include<stdio.h>
/* Defining Structre*/
struct bank
{
int acc_no;
char name[20];
int bal;
}b[3];
/*Function to find the details of customer whose
balance < 100.*/
void check(struct bank b[],int n)
/*Passing Array of structure to function*/
{
int i;
printf("nCustomer Details whose Balance < 100
Rs. n");
printf("----------------------------------------------n");
for(i=0;i<n;i++)
{
if(b[i].bal<100)
{
printf("Account Number :
%dn",b[i].acc_no);
printf("Name : %sn",b[i].name);
printf("Balance : %dn",b[i].bal);
printf("------------------------------n");
}
}
}
int main()
{
int i;
for(i=0;i<3;i++)
{
printf("Enter Details of Customer %dn",i+1);
printf("------------------------------n");
printf("Enter Account Number : ");
scanf("%d",&b[i].acc_no);
printf("Enter Name : ");
scanf("%s",b[i].name);
printf("Enter Balance : ");
scanf("%d",&b[i].bal);
printf("------------------------------n");
}
check(b,3); //call function check
return 0;
}
C lab programs
/* C - Unions*/
1. #include <stdio.h>
2. #include <string.h>
3. union Data
4. {
5. int i;
6. float f;
7. char str[20];
8. };
9. int main( )
10. {
11. union Data data;
12. printf( "Memory size occupied by data : %dn", sizeof(data));
13. return 0;
14. }
C lab programs
/* example to define union for an employee in c*/
1. #include <stdio.h>
2. #include <string.h>
3. union employee
4. { int id;
5. char name[50];
6. }e1; //declaring e1 variable for union
7. int main( )
8. {
9. //store first employee information
10. e1.id=101;
11. strcpy(e1.name, "Sonoo Jaiswal");//copying string into char
array
12. //printing first employee information
13. printf( "employee 1 id : %dn", e1.id);
14. printf( "employee 1 name : %sn", e1.name);
15. return 0;
16. }
C lab programs
/* C program to read name and marks of n number of students and store them in a file.*/
1. #include <stdio.h>
2. int main()
3. {
4. char name[50];
5. int marks, i, num;
6. printf("Enter number of students: ");
7. scanf("%d", &num);
8. FILE *fptr;
9. fptr = (fopen("C:student.txt", "w"));
10. if(fptr == NULL)
11. {
12. printf("Error!");
13. exit(1);
14. }
15. for(i = 0; i < num; ++i)
16. {
17. printf("For student%dnEnter name: ", i+1);
18. scanf("%s", name);
19. printf("Enter marks: ");
20. scanf("%d", &marks);
21. fprintf(fptr,"nName: %s nMarks=%d n", name, marks);
22. }
23. fclose(fptr);
24. return 0;
25. }
C lab programs
/* C program to read name and marks of n number of students from and store them in a file. If the file
previously exits, add the information to the file.*/
1. #include <stdio.h>
2. int main()
3. {
4. char name[50];
5. int marks, i, num;
6. printf("Enter number of students: ");
7. scanf("%d", &num);
8. FILE *fptr;
9. fptr = (fopen("C:student.txt", "a"));
10. if(fptr == NULL)
11. {
12. printf("Error!");
13. exit(1);
14. }
15. for(i = 0; i < num; ++i)
16. {
17. printf("For student%dnEnter name: ", i+1);
18. scanf("%s", name);
19. printf("Enter marks: ");
20. scanf("%d", &marks);
21. fprintf(fptr,"nName: %s nMarks=%d n", name, marks);
22. }
23. fclose(fptr);
24. return 0;
25. }
C lab programs
Thank You

More Related Content

PDF
C programms
DOCX
Practical File of C Language
DOCX
Practical write a c program to reverse a given number
DOCX
C programs
DOC
Basic c programs updated on 31.8.2020
DOCX
Practical write a c program to reverse a given number
PPTX
C Programming Language Part 4
C programms
Practical File of C Language
Practical write a c program to reverse a given number
C programs
Basic c programs updated on 31.8.2020
Practical write a c program to reverse a given number
C Programming Language Part 4

What's hot (20)

PDF
c-programming-using-pointers
PPTX
C Programming Language Part 7
DOC
C lab-programs
PDF
Understanding storage class using nm
PDF
88 c-programs
PDF
C Programming lab
DOC
C program to check leap year
PDF
C programs
DOCX
C Programming
DOCX
DataStructures notes
DOC
C basics
DOCX
Chapter 8 c solution
PDF
C++ programs
DOCX
Program flowchart
DOCX
C lab manaual
DOC
Pads lab manual final
DOCX
PPTX
Double linked list
PPTX
Double linked list
DOCX
Basic Programs of C++
c-programming-using-pointers
C Programming Language Part 7
C lab-programs
Understanding storage class using nm
88 c-programs
C Programming lab
C program to check leap year
C programs
C Programming
DataStructures notes
C basics
Chapter 8 c solution
C++ programs
Program flowchart
C lab manaual
Pads lab manual final
Double linked list
Double linked list
Basic Programs of C++
Ad

Similar to C lab programs (20)

PPTX
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
PPTX
Introduction to Basic C programming 02
DOCX
PPTX
Programming ppt files (final)
PDF
Cse115 lecture08repetitionstructures part02
PDF
C programming
PDF
7 functions
PDF
Subject:Programming in C - Lab Programmes
PPT
Intro to c programming
DOC
C-programs
PPTX
Chapter 1_C Fundamentals_HS_Tech Yourself C.pptx
PDF
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
PPTX
Najmul
PDF
PPS SSSSHHEHESHSHEHHEHAKAKHEHE12131415.pdf
PPTX
C language
PPT
12 lec 12 loop
PDF
5 c control statements looping
PPTX
C BASICS.pptx FFDJF/,DKFF90DF SDPJKFJ[DSSIFLHDSHF
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
Introduction to Basic C programming 02
Programming ppt files (final)
Cse115 lecture08repetitionstructures part02
C programming
7 functions
Subject:Programming in C - Lab Programmes
Intro to c programming
C-programs
Chapter 1_C Fundamentals_HS_Tech Yourself C.pptx
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Najmul
PPS SSSSHHEHESHSHEHHEHAKAKHEHE12131415.pdf
C language
12 lec 12 loop
5 c control statements looping
C BASICS.pptx FFDJF/,DKFF90DF SDPJKFJ[DSSIFLHDSHF
Ad

More from Dr. Prashant Vats (20)

PDF
Multiplexers
PDF
Financial fucntions in ms e xcel
PDF
4. text functions in excel
PDF
3. lookup functions in excel
PDF
2. date and time function in excel
PDF
1. statistical functions in excel
PDF
3. subtotal function in excel
PDF
2. mathematical functions in excel
PPTX
RESOLVING CYBERSQUATTING DISPUTE IN INDIA
PPTX
India: Meta-Tagging Vis-À-Vis Trade Mark Misuse: An Overview
PPTX
Trademark Cases Arise from Meta-Tags, Frames: Disputes Involve Search-Engine ...
PPTX
Scheme for Notifying Examiner of Electronic Evidence Under section 79A of the...
PPTX
METHODS OF RESOLVING CYBERSQUATTING DISPUTE IN INDIA
PPTX
Computer Software and Related IPR Issues
PPTX
Amendments to the Indian Evidence Act, 1872 with respect to IT ACT 2000
PPTX
Trademark Issues in cyberspace
PPTX
Trade-Related Aspects of Intellectual Property Rights (TRIPS)
PPTX
How to Copyright a Website to Protect It under IPR and copyright act
PPTX
International Treaties for protection of IPR
PPTX
IPR – An Overview, Copyright Issues in Cyberspace
Multiplexers
Financial fucntions in ms e xcel
4. text functions in excel
3. lookup functions in excel
2. date and time function in excel
1. statistical functions in excel
3. subtotal function in excel
2. mathematical functions in excel
RESOLVING CYBERSQUATTING DISPUTE IN INDIA
India: Meta-Tagging Vis-À-Vis Trade Mark Misuse: An Overview
Trademark Cases Arise from Meta-Tags, Frames: Disputes Involve Search-Engine ...
Scheme for Notifying Examiner of Electronic Evidence Under section 79A of the...
METHODS OF RESOLVING CYBERSQUATTING DISPUTE IN INDIA
Computer Software and Related IPR Issues
Amendments to the Indian Evidence Act, 1872 with respect to IT ACT 2000
Trademark Issues in cyberspace
Trade-Related Aspects of Intellectual Property Rights (TRIPS)
How to Copyright a Website to Protect It under IPR and copyright act
International Treaties for protection of IPR
IPR – An Overview, Copyright Issues in Cyberspace

Recently uploaded (20)

PPTX
Welding lecture in detail for understanding
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Digital Logic Computer Design lecture notes
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
composite construction of structures.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Sustainable Sites - Green Building Construction
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Welding lecture in detail for understanding
Internet of Things (IOT) - A guide to understanding
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Digital Logic Computer Design lecture notes
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
composite construction of structures.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Foundation to blockchain - A guide to Blockchain Tech
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
Structs to JSON How Go Powers REST APIs.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Lesson 3_Tessellation.pptx finite Mathematics
OOP with Java - Java Introduction (Basics)
Sustainable Sites - Green Building Construction
Model Code of Practice - Construction Work - 21102022 .pdf
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...

C lab programs

  • 2. /* C program to print Hello World! */ #include <stdio.h> int main() { printf("Hello World!"); return 0; }
  • 4. /* C program to print Hello World using function */ #include <stdio.h> // function to print Hello World! void printMessage(void) { printf("Hello World!"); } int main() { //calling function printMessage(); return 0; }
  • 6. /* C Program to find sum of two numbers */ #include<stdio.h> int main() { int a, b, sum; printf("n Enter two no: "); scanf("%d %d", &a, &b); sum = a + b; printf("Sum : %d", sum); return(0); }
  • 8. /* Check Whether Number is Prime or not */ #include<stdio.h> int main() { int num, i, count = 0; printf("Enter a number:"); scanf("%d", &num); for (i = 2; i <= num / 2; i++) { if (num % i == 0) { count++; break; } } if (count == 0) printf("%d is a prime number", num); else printf("%d is not a prime number", num); return 0; }
  • 10. /* Factorial of a Number*/ #include <stdio.h> int main() { int n, i; unsigned long long fact = 1; printf("Enter an integer: "); scanf("%d", &n); // shows error if the user enters a negative integer if (n < 0) printf("Error! Factorial of a negative number doesn't exist."); else { for (i = 1; i <= n; ++i) { fact *= i; } printf("Factorial of %d = %llu", n, fact); } return 0; }
  • 12. /* Swap Numbers Using Temporary Variable*/ #include<stdio.h> int main() { double first, second, temp; printf("Enter first number: "); scanf("%lf", &first); printf("Enter second number: "); scanf("%lf", &second); // Value of first is assigned to temp temp = first; // Value of second is assigned to first first = second; // Value of temp (initial value of first) is assigned to second second = temp; printf("nAfter swapping, firstNumber = %.2lfn", first); printf("After swapping, secondNumber = %.2lf", second); return 0; }
  • 14. /* C Program to generate the Fibonacci Series starting from any two numbers*/ #include<stdio.h> int main() { int first, second, sum, num, counter = 0; printf("Enter the term : "); scanf("%d", &num); printf("n Enter First Number : "); scanf("%d", &first); printf("n Enter Second Number : "); scanf("%d", &second); printf("n Fibonacci Series : %d %d ", first, second); while (counter < num) { sum = first + second; printf("%d ", sum); first = second; second = sum; counter++; } return (0); }
  • 16. /* C Program to find greatest in 3 numbers*/ #include<stdio.h> int main() { int a, b, c; printf("nEnter value of a, b & c : "); scanf("%d %d %d", &a, &b, &c); if ((a > b) && (a > c)) printf("na is greatest"); if ((b > c) && (b > a)) printf("nb is greatest"); if ((c > a) && (c > b)) printf("nc is greatest"); return(0); } •
  • 18. /* Program to relate two integers using =, > or < symbol using C if...else Ladder*/ #include <stdio.h> int main() { int number1, number2; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); //checks if the two integers are equal. if(number1 == number2) { printf("Result: %d = %d",number1,number2); } //checks if number1 is greater than number2. else if (number1 > number2) { printf("Result: %d > %d", number1, number2); } //checks if both test expressions are false else { printf("Result: %d < %d",number1, number2); } return 0; }
  • 20. /* Example of while loop*/ #include <stdio.h> int main() { int count=1; while (count <= 4) { printf("%d ", count); count++; } return 0; }
  • 22. /* do...while loop in C*/ #include <stdio.h> int main () { /* local variable definition */ int a = 10; /* do loop execution */ do { printf("value of a: %dn", a); a = a + 1; }while( a < 20 ); return 0; }
  • 24. /* for loop in C*/ #include <stdio.h> int main () { int a; /* for loop execution */ for( a = 10; a < 20; a = a + 1 ){ printf("value of a: %dn", a); } return 0; }
  • 26. /*Nested For Loop in C*/ #include <stdio.h> int main() { for (int i=0; i<2; i++) { for (int j=0; j<4; j++) { printf("%d, %dn",i ,j); } } return 0; }
  • 28. /* Switch Statement in C*/ 1. #include <stdio.h> 2. int main () 3. { 4. /* local variable definition */ 5. char grade = 'B'; 6. switch(grade) { 7. case 'A' : 8. printf("Excellent!n" ); 9. break; 10. case 'B' : 11. case 'C' : 12. printf("Well donen" ); 13. break; 14. case 'D' : 15. printf("You passedn" ); 16. break; 17. case 'F' : 18. printf("Better try againn" ); 19. break; 20. default : 21. printf("Invalid graden" ); 22. } 23. printf("Your grade is %cn", grade ); 24. return 0; 25. }
  • 30. 1. /* continue statement in C*/ 2. #include <stdio.h> 3. int main () 4. { 5. /* local variable definition */ 6. int a = 10; 7. 8. /* do loop execution */ 9. do { 10. 11. if( a == 15) { 12. /* skip the iteration */ 13. a = a + 1; 14. continue; 15. } 16. 17. printf("value of a: %dn", a); 18. a++; 19. 20. } while( a < 20 ); 21. 22. return 0; 23. }
  • 32. /* Example of goto statement*/ 1. #include <stdio.h> 2. int main() 3. { 4. int sum=0; 5. for(int i = 0; i<=10; i++){ 6. sum = sum+i; 7. if(i==5){ 8. goto addition; 9. } 10. } 11. addition: 12. printf("%d", sum); 13. return 0; 14. } • Output: • 15 • Explanation: In this example, we have a label addition and when the value of i (inside loop) is equal to 5 then we are jumping to this label using goto. This is reason the sum is displaying the sum of numbers till 5 even though the loop is set to run from 0 to 10.
  • 34. /* prime Numbers between Two Integers using function*/ 1. #include <stdio.h> 2. int checkPrimeNumber(int n); 3. int main() 4. { 5. int n1, n2, i, flag; 6. printf("Enter two positive integers: "); 7. scanf("%d %d", &n1, &n2); 8. printf("Prime numbers between %d and %d are: ", n1, n2); 9. for (i = n1 + 1; i < n2; ++i) 10. { 11. // flag will be equal to 1 if i is prime 12. flag = checkPrimeNumber(i); 13. if (flag == 1) 14. printf("%d ", i); 15. } 16. return 0; 17. } 18. // user-defined function to check prime number 19. int checkPrimeNumber(int n) 20. { 21. int j, flag = 1; 22. for (j = 2; j <= n / 2; ++j) { 23. if (n % j == 0) { 24. flag = 0; 25. break; 26. } 27. } 28. return flag; 29. }
  • 36. /* Sum of Natural Numbers Using Recursion*/ 1. #include <stdio.h> 2. int sum(int n); 3. int main() 4. { 5. int number, result; 6. printf("Enter a positive integer: "); 7. scanf("%d", &number); 8. result = sum(number); 9. printf("sum = %d", result); 10. return 0; 11. } 12. int sum(int n) 13. { 14. if (n != 0) 15. // sum() function calls itself 16. return n + sum(n-1); 17. else 18. return n; 19. }
  • 38. /* Function call by Value in C*/ 1. #include <stdio.h> 2. /* function declaration */ 3. void swap(int x, int y); 4. int main () 5. { 6. /* local variable definition */ 7. int a = 100; 8. int b = 200; 9. printf("Before swap, value of a : %dn", a ); 10. printf("Before swap, value of b : %dn", b ); 11. /* calling a function to swap the values */ 12. swap(a, b); 13. printf("After swap, value of a : %dn", a ); 14. printf("After swap, value of b : %dn", b ); 15. return 0; 16. } 17. void swap(int x, int y) 18. { 19. int temp; 20. temp = x; /* save the value of x */ 21. x = y; /* put y into x */ 22. y = temp; /* put temp into y */ 23. return; 24. }
  • 40. • /* Function call by reference in C*/ 1. #include <stdio.h> 2. int main () 3. { 4. /* local variable definition */ 5. int a = 100; 6. int b = 200; 7. printf("Before swap, value of a : %dn", a ); 8. printf("Before swap, value of b : %dn", b ); 9. /* calling a function to swap the values */ 10. swap(&a, &b); 11. printf("After swap, value of a : %dn", a ); 12. printf("After swap, value of b : %dn", b ); 13. 14. return 0; 15. } 16. void swap(int *x, int *y) 17. { 18. int temp; 19. temp = *x; /* save the value of x */ 20. *x = *y; /* put y into x */ 21. *y = temp; /* put temp into y */ 22. return; 23. }
  • 42. • /* C - Arrays*/ 1. #include <stdio.h> 2. 3. int main () { 4. 5. int n[ 10 ]; /* n is an array of 10 integers */ 6. int i,j; 7. 8. /* initialize elements of array n to 0 */ 9. for ( i = 0; i < 10; i++ ) { 10. n[ i ] = i + 100; /* set element at location i to i + 100 */ 11. } 12. 13. /* output each array element's value */ 14. for (j = 0; j < 10; j++ ) { 15. printf("Element[%d] = %dn", j, n[j] ); 16. } 17. 18. return 0; 19. } •
  • 44. • /* Simple Two dimensional (2D) Array Example*/ 1. #include<stdio.h> 2. int main() 3. { /* 2D array declaration*/ 4. int disp[2][3]; 5. /*Counter variables for the loop*/ 6. int i, j; 7. for(i=0; i<2; i++) { 8. for(j=0;j<3;j++) { 9. printf("Enter value for disp[%d][%d]:", i, j); 10. scanf("%d", &disp[i][j]); 11. } } 12. //Displaying array elements 13. printf("Two Dimensional array elements:n"); 14. for(i=0; i<2; i++) { 15. for(j=0;j<3;j++) { 16. printf("%d ", disp[i][j]); 17. if(j==2){ 18. printf("n"); 19. } } } 20. return 0; 21. }
  • 46. /* Store Information in Structure and Display it*/ 1. #include <stdio.h> 2. struct student { 3. char firstName[50]; 4. int roll; 5. float marks; } s[10]; 6. int main() 7. { 8. int i; 9. printf("Enter information of students:n"); 10. // storing information 11. for (i = 0; i < 5; ++i) { 12. s[i].roll = i + 1; 13. printf("nFor roll number%d,n", s[i].roll); 14. printf("Enter first name: "); 15. scanf("%s", s[i].firstName); 16. printf("Enter marks: "); 17. scanf("%f", &s[i].marks); 18. } 19. printf("Displaying Information:nn"); 20. // displaying information 21. for (i = 0; i < 5; ++i) { 22. printf("nRoll number: %dn", i + 1); 23. printf("First name: "); 24. puts(s[i].firstName); 25. printf("Marks: %.1f", s[i].marks); 26. printf("n"); 27. } 28. return 0; 29. }
  • 48. Write a 'C' program to accept customer details such as: Account_no, Name, Balance using structure. Assume 3 customers in the bank. Write a function to print the account no. and name of each customer whose balance < 100 Rs. #include<stdio.h> /* Defining Structre*/ struct bank { int acc_no; char name[20]; int bal; }b[3]; /*Function to find the details of customer whose balance < 100.*/ void check(struct bank b[],int n) /*Passing Array of structure to function*/ { int i; printf("nCustomer Details whose Balance < 100 Rs. n"); printf("----------------------------------------------n"); for(i=0;i<n;i++) { if(b[i].bal<100) { printf("Account Number : %dn",b[i].acc_no); printf("Name : %sn",b[i].name); printf("Balance : %dn",b[i].bal); printf("------------------------------n"); } } } int main() { int i; for(i=0;i<3;i++) { printf("Enter Details of Customer %dn",i+1); printf("------------------------------n"); printf("Enter Account Number : "); scanf("%d",&b[i].acc_no); printf("Enter Name : "); scanf("%s",b[i].name); printf("Enter Balance : "); scanf("%d",&b[i].bal); printf("------------------------------n"); } check(b,3); //call function check return 0; }
  • 50. /* C - Unions*/ 1. #include <stdio.h> 2. #include <string.h> 3. union Data 4. { 5. int i; 6. float f; 7. char str[20]; 8. }; 9. int main( ) 10. { 11. union Data data; 12. printf( "Memory size occupied by data : %dn", sizeof(data)); 13. return 0; 14. }
  • 52. /* example to define union for an employee in c*/ 1. #include <stdio.h> 2. #include <string.h> 3. union employee 4. { int id; 5. char name[50]; 6. }e1; //declaring e1 variable for union 7. int main( ) 8. { 9. //store first employee information 10. e1.id=101; 11. strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array 12. //printing first employee information 13. printf( "employee 1 id : %dn", e1.id); 14. printf( "employee 1 name : %sn", e1.name); 15. return 0; 16. }
  • 54. /* C program to read name and marks of n number of students and store them in a file.*/ 1. #include <stdio.h> 2. int main() 3. { 4. char name[50]; 5. int marks, i, num; 6. printf("Enter number of students: "); 7. scanf("%d", &num); 8. FILE *fptr; 9. fptr = (fopen("C:student.txt", "w")); 10. if(fptr == NULL) 11. { 12. printf("Error!"); 13. exit(1); 14. } 15. for(i = 0; i < num; ++i) 16. { 17. printf("For student%dnEnter name: ", i+1); 18. scanf("%s", name); 19. printf("Enter marks: "); 20. scanf("%d", &marks); 21. fprintf(fptr,"nName: %s nMarks=%d n", name, marks); 22. } 23. fclose(fptr); 24. return 0; 25. }
  • 56. /* C program to read name and marks of n number of students from and store them in a file. If the file previously exits, add the information to the file.*/ 1. #include <stdio.h> 2. int main() 3. { 4. char name[50]; 5. int marks, i, num; 6. printf("Enter number of students: "); 7. scanf("%d", &num); 8. FILE *fptr; 9. fptr = (fopen("C:student.txt", "a")); 10. if(fptr == NULL) 11. { 12. printf("Error!"); 13. exit(1); 14. } 15. for(i = 0; i < num; ++i) 16. { 17. printf("For student%dnEnter name: ", i+1); 18. scanf("%s", name); 19. printf("Enter marks: "); 20. scanf("%d", &marks); 21. fprintf(fptr,"nName: %s nMarks=%d n", name, marks); 22. } 23. fclose(fptr); 24. return 0; 25. }