C Lab
C Lab
Programming in C
Lab
//Arithmetic.cpp
#include<stdio.h>
#include<conio.h>
int main( )
{
clrscr( );
int a,b;
int op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1:
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2:
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3:
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4:
printf("Division of Two Numbers is %d : ",a/b);
break;
default:
printf(" Enter Your Correct Choice.");
break;
}
getch( );
return 0;
}
2) Write a program using logical operator.
//Logical.cpp
#include<stdio.h>
#include<conio.h>
int main( )
{
clrscr( );
int a = 5, b = 10 ,ret;
ret = ( (a <= b) || (a != b) );
// 5 <= 10 ==> true. 5 != 10 ==> true. So, 1 || 1 will return 1.
printf("Return value of above expression is %d\n",ret);
//Bitwise.cpp
#include<stdio.h>
#include<conio.h>
int main( )
{
clrscr( );
int num1,num2;
printf(“Enter two integers:”);
scanf(“%d %d”,&num1 , &num2);
//Bitwise AND
int bitwiseAND = num1 & num2;
printf(“Bitwise AND :%d\n”,bitwiseAND);
//Bitwise OR
int bitwiseOR = num1 | num2;
printf(“Bitwise OR:%d\n”,bitwiseOR );
//Bitwise XOR
int bitwiseXOR = num1 ^ num2;
printf(“Bitwise XOR :%d\n”,bitwiseXOR );
5) Write a program using simple control statements for Roots of a Quadratic Equation.
//Roots of a Quadratic equation.
//Quadratic.cpp
#include <math.h>
#include<conio.h>
#include <stdio.h>
int main( ) {
clrscr( );
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
6) Write a program using simple control statements for extracting digits of integers.
//Reversing Digits:
//Reversing.cpp
#include <stdio.h>
#include<conio.h>
int main( ) {
clrscr( );
int n, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n = n/ 10;
}
printf("Reversed number = %d", reverse);
getch( );
return 0;
}
8)Write a program using simple control statements for finding sum of digits.
//Finding Sum of Digit.
//Sumofdigit.cpp
#include<stdio.h>
#include<conio.h>
int main( )
{
clrscr( );
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
getch( );
return 0;
}
9)Write a program using simple control statements for printing multiplication tables.
//Printing multiplication tables.
//Multitables.cpp
#include <stdio.h>
#include<conio.h>
int main( ) {
clrscr( );
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", n, i, n * i);
}
getch( );
return 0;
}
10) Write a program using simple control statements for Armstrong Numbers.
//Armstrong.cpp
#include <stdio.h>
#include<conio.h>
int main( ) {
clrscr( );
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;
result += remainder * remainder * remainder;
// removing last digit from the orignal number
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
getch( );
return 0;
}
11) Write a program using simple control statements for checking for prime.
//Checkprime.cpp
#include <stdio.h>
#include<conio.h>
int main( ) {
clrscr( );
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// 0 and 1 are not prime numbers
// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}
// flag is 0 for prime numbers
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
getch( );
return 0;
}
12) Write a program using simple control statements for magic numbers.
//Magicnum.cpp
#include <stdio.h>
#include <conio.h>
int main ( )
{
clrscr( );
// declare integer variables
int n, temp, rev = 0, digit, sum_of_digits = 0;
printf (" Enter a Number: \n");
scanf (" %d", &n); // get the number
temp = n; // assign the number to temp variable
// use while loop to calculate the sum of digits
while ( temp > 0)
{
// extract digit one by one and store into the sum_of_digits
sum_of_digits = sum_of_digits + temp % 10;
/* use modulus symbol to get the remainder of each iteration by temp % 10 */
temp = temp / 10;
}
temp = sum_of_digits; // assign the sum_of_digits to temp variable
printf (" \n The sum of the digits = %d", temp);
13) Write a program to find the value of sin(x) using series expansion.
/* A C program to computer value of sin(x) using series expansion */
#include<stdio.h>
#include<math.h>
#include<conio.h>
unsigned long int fact(int n);
void main( )
{
clrscr( );
int sign=1,x,i;
double sum=0,r;
printf(“Enter the radius :”);
scanf(“%d”,&x);
r=(x*3.14f)/180; /* Converting radius value */
for(i=1;i<=10;i+=2)
{
sum=sum+( (pow(r,i)/fact(i)) * sign );
sign= -sign;
}
printf(“sin(%d) = %lf\n”,x,sum);
}
unsigned long int fact(int n)
{
unsigned long int f=1;
int i;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
14) Write a program to find the value of cos(x) using series expansion.
#include<stdio.h>
#include<math.h>
#include<conio.h>
unsigned long int fact(int n);
void main( )
{
clrscr( );
int sign=1,x,i;
double sum=1,r;
printf(“Enter the radius :”);
scanf(“%d”,&x);
r=(x*3.14f)/180; /* Converting radius value */
for(i=2;i<=10;i+=2)
{
sign= -sign;
sum=sum+( (pow(r,i)/fact(i)) * sign );
}
printf(“Cos(%d) = %lf\n”, x,sum);
}
unsigned long int fact(int n)
{
unsigned long int f=1;
int i;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
15) Write a program to convert Binary to Decimal.
// Convert binary to decimal
//Binarytodec.cpp
#include <stdio.h>
#include <math.h>
#include<conio.h>
// function prototype
int convert(long long);
int main( ) {
clrscr( );
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
getch( );
return 0;
}
// function definition
int convert(long long n) {
int dec = 0, i = 0, rem;
while (n!=0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}
#include<stdio.h>
#include<conio.h>
void main( )
{
long int binary_number, hexadecimal_number = 0, i = 1, remainder;
clrscr();
printf("Please Enter any Binary Number: ");
scanf("%ld", &binary_number);
while (binary_number != 0)
{
remainder = binary_number % 10;
hexadecimal_number = hexadecimal_number + remainder * i;
i = i * 2;
binary_number = binary_number / 10;
}
printf("Equivalent Hexadecimal Number %lX", hexadecimal_number);
getch( ); }
#include<stdio.h>
#include<conio.h>
#define MAX 1000
void main()
{
char binary_number[MAX], hexa[MAX];
long int i = 0;
clrscr();
#include<stdio.h>
#include<conio.h>
int main( )
{
clrscr( );
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
getch( );
return 0;
}
25)Write a program to GCD of two numbers.
//GCD.cpp
#include <stdio.h>
#include<conio.h>
int hcf(int n1, int n2);
int main( ) {
clrscr( );
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
getch( );
return 0;
}
int hcf(int n1, int n2) {
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}
26)Write a program to find the maximum, minimum, average and standard deviation
of given set of numbers using arrays.
//Array.cpp
#include<stdio.h>
#include<conio.h>
int main( )
{
clrscr( );
int a[1000],i,n,min,max,sum=0,SD,sum1;
float average,mean;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
sum1 += (a[i] - mean) * (a[i] - mean);
}
min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i]; }
average = 1.0*sum/n;
SD = sqrt(sum1 / n);
printf("sum is : %d\n ",sum);
printf("mean is : %d\n ",sum1);
printf("standard deviation is : %d\n ",SD);
printf("Your average is %.2f\n",average);
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);
getch( );
return 0;
}
// Function to add two matrices and store the result in a third matrix
void addMatrices(int matrix1[][100], int matrix2[][100], int result[][100], int rows, int
columns) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}
int main() {
int rows, columns;
return 0;
}
30) Write a program to find the transpose of a square matrix using functions.
#include <stdio.h>
void transpose(int p[3][3], int t[3][3]);
int main() {
int i, j;
int p[3][3], t[3][3];
printf("Enter matrix P\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("Enter the elements of matrix P [%d,%d]: ", i, j);
scanf("%d", & p[i][j]);
}
}
transpose(p, t);
printf("Transpose of matrix P is:\n\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", t[i][j]);
}
printf("\n");
}}
void transpose(int p[3][3], int t[3][3]) {
int row, col;
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {
t[row][col] = p[col][row];
} }}
31) Write a program for functions of string manipulation: inputting and outputting string , using
string functions such as strlen( ), strcat( ), strcpy( ) ………etc.
#include <stdio.h>
#include <string.h>
int main() {
char inputString[100]; // To store user input
char resultString[200]; // To store manipulated strings
// Input a string
printf("Enter a string: ");
scanf("%s", inputString);
return 0;
}
32) Write a simple programs for strings without using string functions.
#include <stdio.h>
#include <string.h>
int main( ) {
char inputString[100]; // To store user input
char resultString[200]; // To store manipulated strings
// Input a string
printf("Enter a string: ");
scanf("%s", inputString);
// Calculate and print the length of the input string
int length = strlen(inputString);
printf("Length of the string: %d\n", length);
// Copy the input string to the result string
strcpy(resultString, inputString);
printf("Copied string: %s\n", resultString);
// Concatenate a second string to the result string
char appendString[100];
printf("Enter a string to concatenate: ");
scanf("%s", appendString);
strcat(resultString, appendString);
printf("Concatenated string: %s\n", resultString);
return 0;
}
33) Write a program to find the No. of characters, words and lines of given text file.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * file;
char path[100];
char ch;
int characters, words, lines;
/* Input path of files to merge to third file */
printf("Enter source file path: ");
scanf("%s", path);
/* Open source files in 'r' mode */
file = fopen(path, "r");
/* Check if file opened successfully */
if (file == NULL)
{
printf("\nUnable to open file.\n");
printf("Please check if file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
}
/*
* Logic to count characters, words and lines.
*/
characters = words = lines = 0;
while ((ch = fgetc(file)) != EOF)
{
characters++;
/* Check new line */
if (ch == '\n' || ch == '\0')
lines++;
/* Check words */
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
/* Increment words and lines for last word */
if (characters > 0)
{
words++;
lines++;
}
/* Print file statistics */
printf("\n");
printf("Total characters = %d\n", characters);
printf("Total words = %d\n", words);
printf("Total lines = %d\n", lines);
/* Close files to release resources */
fclose(file);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char name[50];
int rollNumber;
float marks;
} Student;
int main() {
FILE *inputFile, *outputFile;
char inputFileName[] = "student_info.txt";
char outputFileName[] = "student_memos.txt";
// Read student information from the input file and generate memos
Student student;
while (fscanf(inputFile, "%s %d %f", student.name, &student.rollNumber,
&student.marks) != EOF) {
// Generate the student memo
fprintf(outputFile, "Student Name: %s\n", student.name);
fprintf(outputFile, "Roll Number: %d\n", student.rollNumber);
fprintf(outputFile, "Marks: %.2f\n", student.marks);
fprintf(outputFile, "----------------------------------\n");
}
return 0;
}
35)Write a program in C to copy the elements of one array into another array using for loop.
#include <stdio.h>
int main()
{
//Initialize array
int arr1[] = {1, 2, 3, 4, 5};
printf("\n");
36)Write a C program to check whether a given number is positive or negative using conditional
statement.
#include <stdio.h>
int main() {
int number;
// Input
printf("Enter a number: ");
scanf("%d", &number);
return 0;
}
int main() {
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
return 0;
}
// Recursively call the function with updated start and end positions
reverseString(str, start + 1, end - 1);
}
int main() {
char str[100];
int length = 0;
while (str[length] != '\0') {
length++;
}
reverseString(str, 0, length - 1);
printf("Reversed string: %s\n", str);
return 0;
}
return 0;
}
int main( ) {
char *str1, *str2, *result;
int len1, len2;
if (result == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
return 1;
}
return 0;
}
if (isSquare) {
printf("The matrix is square (%dx%d matrix).\n", n, n);
} else {
printf("The matrix is not square.\n");
}
}
return 0;
}
if (limit < 1) {
printf("Please enter a positive limit.\n");
return 1; // Exit with an error code.
}
printf("Natural numbers up to %d are:\n", limit);
for (int i = 1; i <= limit; i++) {
printf("%d ", i);
}
printf("\n");
return 0; // Exit successfully.
}
49) Write a C program to find the sum of the series 12 + 22+ 32 + …….n2.
#include <stdio.h>
int main() {
int n, sum = 0;
// Prompt the user to enter the value of n
printf("Enter the value of n: ");
scanf("%d", &n);
// Calculate the sum of the series
for (int i = 1; i <= n; i++) {
sum += i * i;
}
// Display the sum
printf("The sum of the series is: %d\n", sum);
return 0;
}