KEMBAR78
Basic C Programming Lab Practice | PDF
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();
}

Basic C Programming Lab Practice

  • 1.
    Task3: Write aC 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 arrayin 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 inmatrix 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 anystring: 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 aC 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> /* Functiondeclaration */ 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 EmployeeE; 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 filenamed 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 notopen!!”); 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 ODDfile: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(); }