SlideShare a Scribd company logo
1 of 16
Download to read offline
Task3: Write a C program to sort array elements in ascending order.
Ans:
#include <stdio.h>
int main()
{
int array[100];
int size;
int i, j, temp;
printf("Enter size of array: n");
scanf("%d", &size);
printf("Enter elements in array: n");
for(i=0; i<size; i++)
{
scanf("%d", &array[i]);
}
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printf("nElements of array in sorted ascending order: t");
for(i=0; i<size; i++)
{
printf("%dt", array[i]);
}
return 0;
}
Task 4: Write a C program to add two matrices.
Ans:
#include <stdio.h>
int main()
{
int A[3][3], B[3][3], C[3][3];
int row, col;
printf("Enter elements in matrix A of size 3x3: n");
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
scanf("%d", &A[row][col]);
}
}
printf("nEnter elements in matrix B of size 3x3: n");
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
scanf("%d", &B[row][col]);
}
}
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
C[row][col] = A[row][col] + B[row][col];
}
}
printf("nSum of matrices A+B = n");
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
printf("%d ", C[row][col]);
}
printf("n");
}
return 0;
}
Task 5: Write a C program to find total number of alphabets, digits or special character in a string.
Ans:
#include <stdio.h>
int main()
{
char str[100];
int alphabets, digits, others, i;
alphabets = digits = others = i = 0;
/* Input string from user */
printf("Enter any string : n");
gets(str);
/*
* Check each character of string for alphabet, digit or special character
*/
while(str[i]!='0')
{
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
alphabets++;
}
else if(str[i]>='0' && str[i]<='9')
{
digits++;
}
else
{
others++;
}
i++;
}
printf("Alphabets = %dn", alphabets);
printf("Digits = %dn", digits);
printf("Special characters = %d", others);
return 0;
}
Task6: Write a C program to count total number of vowels and consonants in a string.
Ans:
#include <stdio.h>
#include <string.h>
int main()
{
char string[100];
int i, len, vowel, consonant;
printf("Enter any string: n");
gets(string);
vowel = 0;
consonant = 0;
len = strlen(string);
for(i=0; i<len; i++)
{
if(string[i] =='a' || string[i]=='e' || string[i]=='i' || string[i]=='o' || string[i]=='u' || string[i]=='A'
|| string[i]=='E' || string[i]=='I' || string[i]=='O' || string[i]=='U')
{
vowel++;
}
else if((string[i]>='a' && string[i]<='z') || (string[i]>='A' && string[i]<='Z'))
{
consonant++;
}
}
printf("Total number of vowel = %dn", vowel);
printf("Total number of consonant = %dn", consonant);
return 0;
}
Task7: Write a C program to check whether a string is palindrome or not.
Ans:
#include <stdio.h>
#include <string.h>
int main()
{
char string[100], reverse[100];
int flag;
printf("Enter any string: n");
gets(string);
strcpy(reverse, string);
strrev(reverse);
flag = strcmp(string, reverse);
if(flag == 0)
{
printf("String is Palindrome.n");
}
else
{
printf("String is Not Palindrome.n");
}
return 0;
}
Task8: Write a C program to find all prime numbers between given interval using functions.
Ans:
#include <stdio.h>
int checkPrimeNumber(int n);
int main()
{
int n1, n2, i, flag;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("Prime numbers between %d and %d are: ", n1, n2);
for(i=n1+1; i<n2; ++i)
{
// i is a prime number, flag will be equal to 1
flag = checkPrimeNumber(i);
if(flag == 1)
printf("%d ",i);
}
return 0;
}
// user-defined function to check prime number
int checkPrimeNumber(int n)
{
int j, flag = 1;
for(j=2; j <= n/2; ++j)
{
if (n%j == 0)
{
flag =0;
break;
}
}
return flag;
}
Task9:
Write a C program to find factorial of any number using recursion.
Ans:
#include <stdio.h>
long int multiplyNumbers(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n)
{
if (n >= 1)
return n*multiplyNumbers(n-1);
else
return 1;
}
Task10: Write a C program to generate nth Fibonacci term using recursion.
Ans:
/**
* C program to find nth Fibonacci term using recursion
*/
#include <stdio.h>
/* Function declaration */
unsigned long long fibo(int num);
int main()
{
int num;
unsigned long long fibonacci;
/* Input a number from user */
printf("Enter any number to find nth fiboacci term: ");
scanf("%d", &num);
fibonacci = fibo(num);
printf("%d fibonacci term is %llu", num, fibonacci);
return 0;
}
/**
* Recursive function to find nth Fibonacci term
*/
unsigned long long fibo(int num)
{
if(num == 0) //Base condition
return 0;
else if(num == 1) //Base condition
return 1;
else
return fibo(num-1) + fibo(num-2);
}
Task11: To write a C program to print the abbreviation of an Organization.
Output:
Enter the full form of an organization
bangladesh machines
The abbreviation of bangladesh machines is "BM"
Ans:
/* Write person's name in abbreviated form */
#include <stdio.h>
int main()
{
char fname[20], mname[20], lname[20]; /* person's name */
/* accept full name */
printf("Enter full name (first middle last): ");
scanf("%s %s %s", fname, mname, lname);
/* print abbreviated name */
printf("Abbreviated name: ");
printf("%c. %c. %c.n", fname[0], mname[0], lname[0]);
return 0;
}
Task12: Write a program for nested structure, the two structures are declared within a single
structure. The two inner structures are : “dob” ( fields : dd, mm, yy) and “address” (st, cty) and the
outer structure “Student” ( fields : rollno, name). Write a main program to get and display the details
of N students.
Ans:
eta ektu edit kore nite hobe//////////////////
Ans:
#include<stdio.h>
struct Address
{
char HouseNo[25];
char City[25];
char PinCode[25];
};
struct Employee
{
int Id;
char Name[25];
float Salary;
struct Address Add;
};
void main()
{
int i;
struct Employee E;
printf("ntEnter Employee Id : ");
scanf("%d",&E.Id);
printf("ntEnter Employee Name : ");
scanf("%s",&E.Name);
printf("ntEnter Employee Salary : ");
scanf("%f",&E.Salary);
printf("ntEnter Employee House No : ");
scanf("%s",&E.Add.HouseNo);
printf("ntEnter Employee City : ");
scanf("%s",&E.Add.City);
printf("ntEnter Employee House No : ");
scanf("%s",&E.Add.PinCode);
printf("nDetails of Employees");
printf("ntEmployee Id : %d",E.Id);
printf("ntEmployee Name : %s",E.Name);
printf("ntEmployee Salary : %f",E.Salary);
printf("ntEmployee House No : %s",E.Add.HouseNo);
printf("ntEmployee City : %s",E.Add.City);
printf("ntEmployee House No : %s",E.Add.PinCode);
}
Task13: A file named DATA.txt contains a series of integer numbers. Write a program to read these
numbers and then write all odd numbers to a file to be called ODD.txt and all even numbers to a file
to be called EVEN.txt.
Ans:
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int a,n,i;
FILE *fp1,*fp2,*fp3;
clrscr();
fp1=fopen(“DATA”,”w”);
if(fp1==NULL)
{
printf(“File could not open!!”);
exit(0);
}
printf(“How many numbers?”);
scanf(“%d”,&n);
printf(“Enter contents of DATA file:n”);
for(i=0;i<n;++i)
{
scanf(“%d”,&a);
putw(a,fp1);
}
fclose(fp1);
fp1=fopen(“DATA”,”r”);
fp2=fopen(“ODD“,”w”);
fp3=fopen(“EVEN”,”w”);
if(fp1==NULL||fp2==NULL||fp3==NULL)
{
printf(“File could not open!!”);
exit(0);
}
while((a=getw(fp1))!=EOF)
{
if(a%2!=0)
putw(a,fp2);
else
putw(a,fp3);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp2=fopen(“ODD”,”r”);
fp3=fopen(“EVEN”,”r”);
if(fp2==NULL||fp3==NULL)
{
printf(“File could not open!!”);
exit(0);
}
printf(“nContents of ODD file:n”);
while((a=getw(fp2))!=EOF)
printf(“%d “,a);
printf(“nnContents of EVEN file:n”);
while((a=getw(fp3))!=EOF)
printf(“%d “,a);
fclose(fp2);
fclose(fp3);
getch();
}

More Related Content

Similar to Basic C Programming Lab Practice

Data Structure using C
Data Structure using CData Structure using C
Data Structure using CBilal Mirza
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkarsandeep kumbhkar
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programsPrasadu Peddi
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...DR B.Surendiran .
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSKavyaSharma65
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using cArghodeepPaul
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solutionyogini sharma
 
In C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docxIn C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docxtristans3
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020vrgokila
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2YOGESH SINGH
 
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or notaluavi
 
presentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.pptpresentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.pptNamakkalPasanga
 

Similar to Basic C Programming Lab Practice (20)

Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
C programming
C programmingC programming
C programming
 
C-programs
C-programsC-programs
C-programs
 
C Programming
C ProgrammingC Programming
C Programming
 
C faq pdf
C faq pdfC faq pdf
C faq pdf
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
 
Array Programs.pdf
Array Programs.pdfArray Programs.pdf
Array Programs.pdf
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solution
 
In C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docxIn C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docx
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
C programms
C programmsC programms
C programms
 
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or not
 
presentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.pptpresentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.ppt
 

More from Mahmud Hasan Tanvir

More from Mahmud Hasan Tanvir (16)

DATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEMDATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEM
 
ANALOG-TO-DIGITAL CONVERSION
ANALOG-TO-DIGITAL CONVERSIONANALOG-TO-DIGITAL CONVERSION
ANALOG-TO-DIGITAL CONVERSION
 
Cybercrime
CybercrimeCybercrime
Cybercrime
 
COMPUTER VIRUSES AND WORMS.pdf
COMPUTER VIRUSES AND WORMS.pdfCOMPUTER VIRUSES AND WORMS.pdf
COMPUTER VIRUSES AND WORMS.pdf
 
Computer Fundamental
Computer FundamentalComputer Fundamental
Computer Fundamental
 
Chemistry Fundamentals
Chemistry FundamentalsChemistry Fundamentals
Chemistry Fundamentals
 
C++ QNA
C++ QNAC++ QNA
C++ QNA
 
Attributes in Entity-Relationship Model
Attributes in Entity-Relationship ModelAttributes in Entity-Relationship Model
Attributes in Entity-Relationship Model
 
Applications of Radioisotopes.pdf
Applications of Radioisotopes.pdfApplications of Radioisotopes.pdf
Applications of Radioisotopes.pdf
 
Arithmetic Expression
Arithmetic ExpressionArithmetic Expression
Arithmetic Expression
 
FOUR DIFFERENT TYPES OF WRITING STYLES
FOUR DIFFERENT TYPES OF WRITING STYLESFOUR DIFFERENT TYPES OF WRITING STYLES
FOUR DIFFERENT TYPES OF WRITING STYLES
 
Fable.docx
Fable.docxFable.docx
Fable.docx
 
Rape in Rural Bangladesh.pdf
Rape in Rural Bangladesh.pdfRape in Rural Bangladesh.pdf
Rape in Rural Bangladesh.pdf
 
First Law Of Thermodynamics.pdf
First Law Of Thermodynamics.pdfFirst Law Of Thermodynamics.pdf
First Law Of Thermodynamics.pdf
 
Difference between a variable and a constant.pdf
Difference between a variable and a constant.pdfDifference between a variable and a constant.pdf
Difference between a variable and a constant.pdf
 
Defects In Image Formation By Lenses.pdf
Defects In Image Formation By Lenses.pdfDefects In Image Formation By Lenses.pdf
Defects In Image Formation By Lenses.pdf
 

Recently uploaded

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 

Recently uploaded (20)

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 

Basic C Programming Lab Practice

  • 1. Task3: Write a C program to sort array elements in ascending order. Ans: #include <stdio.h> int main() { int array[100]; int size; int i, j, temp; printf("Enter size of array: n"); scanf("%d", &size); printf("Enter elements in array: n"); for(i=0; i<size; i++) { scanf("%d", &array[i]); } for(i=0; i<size; i++) { for(j=i+1; j<size; j++) { if(array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp;
  • 2. } } } printf("nElements of array in sorted ascending order: t"); for(i=0; i<size; i++) { printf("%dt", array[i]); } return 0; } Task 4: Write a C program to add two matrices. Ans: #include <stdio.h> int main() { int A[3][3], B[3][3], C[3][3]; int row, col; printf("Enter elements in matrix A of size 3x3: n"); for(row=0; row<3; row++) { for(col=0; col<3; col++) { scanf("%d", &A[row][col]); } }
  • 3. printf("nEnter elements in matrix B of size 3x3: n"); for(row=0; row<3; row++) { for(col=0; col<3; col++) { scanf("%d", &B[row][col]); } } for(row=0; row<3; row++) { for(col=0; col<3; col++) { C[row][col] = A[row][col] + B[row][col]; } } printf("nSum of matrices A+B = n"); for(row=0; row<3; row++) { for(col=0; col<3; col++) { printf("%d ", C[row][col]); } printf("n"); }
  • 4. return 0; } Task 5: Write a C program to find total number of alphabets, digits or special character in a string. Ans: #include <stdio.h> int main() { char str[100]; int alphabets, digits, others, i; alphabets = digits = others = i = 0; /* Input string from user */ printf("Enter any string : n"); gets(str); /* * Check each character of string for alphabet, digit or special character */ while(str[i]!='0') { if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')) { alphabets++; }
  • 5. else if(str[i]>='0' && str[i]<='9') { digits++; } else { others++; } i++; } printf("Alphabets = %dn", alphabets); printf("Digits = %dn", digits); printf("Special characters = %d", others); return 0; } Task6: Write a C program to count total number of vowels and consonants in a string. Ans: #include <stdio.h> #include <string.h> int main() { char string[100]; int i, len, vowel, consonant; printf("Enter any string: n"); gets(string);
  • 6. vowel = 0; consonant = 0; len = strlen(string); for(i=0; i<len; i++) { if(string[i] =='a' || string[i]=='e' || string[i]=='i' || string[i]=='o' || string[i]=='u' || string[i]=='A' || string[i]=='E' || string[i]=='I' || string[i]=='O' || string[i]=='U') { vowel++; } else if((string[i]>='a' && string[i]<='z') || (string[i]>='A' && string[i]<='Z')) { consonant++; } } printf("Total number of vowel = %dn", vowel); printf("Total number of consonant = %dn", consonant); return 0; } Task7: Write a C program to check whether a string is palindrome or not. Ans: #include <stdio.h> #include <string.h> int main() { char string[100], reverse[100];
  • 7. int flag; printf("Enter any string: n"); gets(string); strcpy(reverse, string); strrev(reverse); flag = strcmp(string, reverse); if(flag == 0) { printf("String is Palindrome.n"); } else { printf("String is Not Palindrome.n"); } return 0; } Task8: Write a C program to find all prime numbers between given interval using functions. Ans: #include <stdio.h> int checkPrimeNumber(int n); int main()
  • 8. { int n1, n2, i, flag; printf("Enter two positive integers: "); scanf("%d %d", &n1, &n2); printf("Prime numbers between %d and %d are: ", n1, n2); for(i=n1+1; i<n2; ++i) { // i is a prime number, flag will be equal to 1 flag = checkPrimeNumber(i); if(flag == 1) printf("%d ",i); } return 0; } // user-defined function to check prime number int checkPrimeNumber(int n) { int j, flag = 1; for(j=2; j <= n/2; ++j) { if (n%j == 0) { flag =0; break; } }
  • 9. return flag; } Task9: Write a C program to find factorial of any number using recursion. Ans: #include <stdio.h> long int multiplyNumbers(int n); int main() { int n; printf("Enter a positive integer: "); scanf("%d", &n); printf("Factorial of %d = %ld", n, multiplyNumbers(n)); return 0; } long int multiplyNumbers(int n) { if (n >= 1) return n*multiplyNumbers(n-1); else return 1; } Task10: Write a C program to generate nth Fibonacci term using recursion. Ans: /** * C program to find nth Fibonacci term using recursion */
  • 10. #include <stdio.h> /* Function declaration */ unsigned long long fibo(int num); int main() { int num; unsigned long long fibonacci; /* Input a number from user */ printf("Enter any number to find nth fiboacci term: "); scanf("%d", &num); fibonacci = fibo(num); printf("%d fibonacci term is %llu", num, fibonacci); return 0; } /** * Recursive function to find nth Fibonacci term */ unsigned long long fibo(int num) { if(num == 0) //Base condition return 0;
  • 11. else if(num == 1) //Base condition return 1; else return fibo(num-1) + fibo(num-2); } Task11: To write a C program to print the abbreviation of an Organization. Output: Enter the full form of an organization bangladesh machines The abbreviation of bangladesh machines is "BM" Ans: /* Write person's name in abbreviated form */ #include <stdio.h> int main() { char fname[20], mname[20], lname[20]; /* person's name */ /* accept full name */ printf("Enter full name (first middle last): "); scanf("%s %s %s", fname, mname, lname); /* print abbreviated name */ printf("Abbreviated name: ");
  • 12. printf("%c. %c. %c.n", fname[0], mname[0], lname[0]); return 0; } Task12: Write a program for nested structure, the two structures are declared within a single structure. The two inner structures are : “dob” ( fields : dd, mm, yy) and “address” (st, cty) and the outer structure “Student” ( fields : rollno, name). Write a main program to get and display the details of N students. Ans: eta ektu edit kore nite hobe////////////////// Ans: #include<stdio.h> struct Address { char HouseNo[25]; char City[25]; char PinCode[25]; }; struct Employee { int Id; char Name[25]; float Salary; struct Address Add; }; void main()
  • 13. { int i; struct Employee E; printf("ntEnter Employee Id : "); scanf("%d",&E.Id); printf("ntEnter Employee Name : "); scanf("%s",&E.Name); printf("ntEnter Employee Salary : "); scanf("%f",&E.Salary); printf("ntEnter Employee House No : "); scanf("%s",&E.Add.HouseNo); printf("ntEnter Employee City : "); scanf("%s",&E.Add.City); printf("ntEnter Employee House No : "); scanf("%s",&E.Add.PinCode); printf("nDetails of Employees"); printf("ntEmployee Id : %d",E.Id); printf("ntEmployee Name : %s",E.Name); printf("ntEmployee Salary : %f",E.Salary); printf("ntEmployee House No : %s",E.Add.HouseNo); printf("ntEmployee City : %s",E.Add.City); printf("ntEmployee House No : %s",E.Add.PinCode); }
  • 14. Task13: A file named DATA.txt contains a series of integer numbers. Write a program to read these numbers and then write all odd numbers to a file to be called ODD.txt and all even numbers to a file to be called EVEN.txt. Ans: #include<stdio.h> #include<conio.h> #include<process.h> void main() { int a,n,i; FILE *fp1,*fp2,*fp3; clrscr(); fp1=fopen(“DATA”,”w”); if(fp1==NULL) { printf(“File could not open!!”); exit(0); } printf(“How many numbers?”); scanf(“%d”,&n); printf(“Enter contents of DATA file:n”); for(i=0;i<n;++i) { scanf(“%d”,&a); putw(a,fp1); }
  • 15. fclose(fp1); fp1=fopen(“DATA”,”r”); fp2=fopen(“ODD“,”w”); fp3=fopen(“EVEN”,”w”); if(fp1==NULL||fp2==NULL||fp3==NULL) { printf(“File could not open!!”); exit(0); } while((a=getw(fp1))!=EOF) { if(a%2!=0) putw(a,fp2); else putw(a,fp3); } fclose(fp1); fclose(fp2); fclose(fp3); fp2=fopen(“ODD”,”r”); fp3=fopen(“EVEN”,”r”); if(fp2==NULL||fp3==NULL) { printf(“File could not open!!”); exit(0);
  • 16. } printf(“nContents of ODD file:n”); while((a=getw(fp2))!=EOF) printf(“%d “,a); printf(“nnContents of EVEN file:n”); while((a=getw(fp3))!=EOF) printf(“%d “,a); fclose(fp2); fclose(fp3); getch(); }