Introduction To Programming ETCS-108: Practical File)
Introduction To Programming ETCS-108: Practical File)
Introduction To Programming ETCS-108: Practical File)
ETCS-108
(Practical File)
Code –
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
Output-
Experiment – 2.1
Aim: Write a program in C to print the sum of four numbers with taking
input from the user.
Code-
#include <stdio.h>
int main(){
int num1,num2,num3,num4,ans;
printf("Enter 4 integers: ");
scanf("%d %d %d %d",&num1,&num2,&num3,&num4);
OutPut-
Experiment – 2.2
Aim: Write a program in C to take input from the user and print user's first
alphabet of his name and age of the user.
Code-
#include <stdio.h>
int main(){
int age;
char name[30];
printf("Enter name: ");
fgets(name, sizeof(name), stdin);
printf("\nEnter age: ");
scanf("%d",&age);
printf("\nFirst alphabet of name: %c",name[0]);
printf("\nAge: %d\n",age);
return 0;
}
Output-
Experiment – 3.1
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
int temp, a, b;
printf("Enter 2 numbers A B :");
scanf("%d%d", &a, &b);
temp = a;
a = b;
b = temp;
printf("number swapped! \nA = %d \nB = %d", a, b);
getch();
return 0;
}
Output-
Experiment – 3.2
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
int temp, a, b;
printf("Enter a numbers :");
scanf("%d", &b);
if (b % 5 == 0 || b % 9 == 0 || b % 15 == 0)
printf("Number is divisible by 5, 9 & 15");
else
printf("Number is not divisible by 5, 9 & 15");
getch();
return 0;
}
Output-
Experiment – 3.3
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
char n;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &n);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (n)
{
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
default:
printf("Error! operator is not correct");
}
getch();
}
Output-
Experiment – 3.4
Code-
#include <stdio.h>
#include <conio.h>
#include <math.h>
Output-
Enter the first number of the G.P. series: 1
Enter the total numbers in the G.P. series: 5
Enter the common ratio of G.P. series: 2
tn term of G.P. : 16.000000
Sum of the G.P. : 63.000000
Experiment – 4.1
Code-
#include <stdio.h>
#include <conio.h>
Output-
Experiment – 4.2
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
char c;
printf("Enter an alphabet: ");
scanf("%c", &c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c
== 'E' || c == 'I' || c == 'O' || c == 'U')
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
getch();
return 0;
}
Output-
Enter an alphabet: G
G is a consonant.
Experiment – 4.3
Code-
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int a, b, c, d;
double root1, root2;
d = b * b - 4 * a * c;
if (d < 0)
{ // complex roots, i is for iota (√-1, square root of -1)
printf("First root = %.2lf + i%.2lf\n", -b / (double)(2 * a), sqrt(-
d) / (2 * a));
printf("Second root = %.2lf - i%.2lf\n", -b / (double)(2 * a), sqrt(-
d) / (2 * a));
}
else
{ // real roots
root1 = (-b + sqrt(d)) / (2 * a);
root2 = (-b - sqrt(d)) / (2 * a);
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
int n, m;
printf("Enter the number to check prime:");
scanf("%d", &n);
m = n / 2;
for (int i = 2; i <= m; i++)
{
if (n % i == 0)
{
printf("Number is not prime");
getch();
return 0;
}
}
printf("Number is prime");
return 0;
}
Output-
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
int a;
printf("Enter a number :");
scanf("%d", &a);
printf("\n");
for (int i = 0; i < a; i++)
{
for (int j = 0; j <= i; j++)
{
printf("*");
}
printf("\n");
}
getch();
return 0;
}
Output-
Experiment – 5.2
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
int a;
printf("Enter a number :");
scanf("%d", &a);
printf("\n");
for (int i = 0; i < a; i++)
{
for (int j = 0; j <= i; j++)
{
printf("%d\t", j + 1);
}
printf("\n");
}
getch();
return 0;
}
Output-
Experiment – 5.3
Code-
#include <stdio.h>
#include <conio.h>
int main()
{
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0)
{
for (space = 1; space <= rows - i; ++space)
{
printf(" ");
}
while (k != 2 * i - 1)
{
printf("* ");
++k;
}
printf("\n");
}
getch();
return 0;
}
Output-
Experiment – 6.1
Aim: Write a program in C to print largest & smallest digit in the no.
entered by the user.
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
int n, p, digit, large = 0, small = 10;
p = n;
while (n > 0)
{
digit = n % 10;
Code-
#include <stdio.h>
using namespace std;
int count_3s(int n)
{
int count = 0;
while (n > 0)
{
if (n % 10 == 3)
{
count++;
}
n = n / 10;
}
return count;
}
int count_in_range(int n)
{
int count = 0;
for (int i = 2; i <= n; i++)
{
count += count_3s(i);
}
return count;
}
int main()
{
int n;
printf("\nEnter the end value: ");
scanf("%d", &n);
printf("\nTotal occurrences of 3 from 0 to % d is % d\n", n,
count_in_range(n));
return 0;
}
Output-
Experiment – 6.3
Code-
#include<stdio.h>
int main()
{
char str[20];
int i;
printf("\n\nEnter The String: ");
scanf("%s",str);
int len=0;
// Calculating length of input string
while(str[len]!='\0')
{
len++;
}
for (i=0;i<len;i++)
{
if (str[i]>=65 && str[i]<=90)
{
str[i] = str[i] + 32;
}
else if (str[i] >= 97 && str[i] <= 122)
{
str[i] = str[i] - 32;
}
}
printf("\nConverted String(Lower/Upper) Is: %s\n",str);
return 0;
}
Output-
Experiment – 6.4
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
int arr[10] = {1, 7, 2, 6, 0, 8, 5, 3, 9, 4};
int element = 0, position = 0, index = 0;
printf("Available elements are:");
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main(void)
{
int array1[10];
int array2[10];
int i;
Aim: Write a program in C to enter 2-D array of size m*n from the user
and display the content row-wise and column wise.
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
/* 2D array declaration*/
int disp[20][20];
/*Counter variables for the loop*/
int i, j, m, n;
printf("Enter Number of rows :");
scanf("%d", &m);
printf("Enter Number of columns :");
scanf("%d", &n);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying array elements printf("\
nrow-wise array elements:\n"); for (i =
0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d \t", disp[i][j]);
}
}
printf("\ncolumn-wise array elements:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
printf("%d \t", disp[j][i]);
}
}
getch();
return 0;
}
Output-
Experiment – 7.1
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
getch();
return 0;
}
Output-
Experiment – 7.2
Code-
#include <stdio.h>
#include <conio.h>
int main()
{
char s1[100], s2[100];
int i;
i = 0;
while (s1[i] != '\0')
{
s2[i] = s1[i]; i+
+;
}
s2[i] = '\0';
printf("\nString s2: %s ", s2); getch();
return (0);
}
Output-
Code-
#include <stdio.h>
#include <conio.h>
struct student
{
char firstName[50];
int roll;
float marks;
} s[10];
int main()
{
int i, n;
printf("Enter number of students:");
scanf("%d", &n);
printf("Enter information of students:\n");
// storing information
for (i = 0; i < n; ++i)
{
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("\n\nDisplaying Information:\n\n");
// displaying information
for (i = 0; i < n; ++i)
{
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
getch();
return 0;
}
Output-
Experiment – 7.4
Code-
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
using namespace std;
void display(int[][3]);
int main(){
int c;
void func1();
void func2();
void func3();
void func4();
void func5();
system(“cls”);
printf(“\n- : Matrix Manipulation Functions (for 3 X 3 Matrix) : -“);
printf(“\n “);
printf(“\n Matrix Addition : 1”);
printf(“\n Matrix Subtraction : 2”);
printf(“\n Matrix Multiplication : 3”);
printf(“\n Find Transpose Matrix : 4”);
printf(“\n Matrix is Symmetric or not : 5”);
printf(“\n Enter Your Choice : “);
scanf(“%d”, &c);
switch I
{
case 1:
func1();
break;
case 2:
func2();
break;
case 3:
func3();
break;
case 4:
func4();
break;
case 5:
func5();
break;
default:
printf(“\nInvalid Choice”);
}
getch();
return 0;
}
void func1()
{
int x[3][3], y[3][3], z[3][3];
void getmatrix(int[][3]);
void addition(int[][3], int[][3], int[][3]);
system(“cls”);
getmatrix(x);
getmatrix(y);
addition(x, y, z);
printf(“\n - : Matrix 1: - \n”);
display(x);
printf(“\n - : Matrix 2: - \n”);
display(y);
printf(“\n - : Matrix Addition (Result): - \n”);
display(z);
}
void getmatrix(int t[][3])
{
int I, j;
for (I = 0; I < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf(“Enter element [%d][%d] : “, I, j);
scanf(“%d”, &t[i][j]);
}
}
}
void addition(int p[][3], int q[][3], int r[][3])
{
int I, j;
for (I = 0; I < 3; i++)
{
for (j = 0; j < 3; j++) r[i]
[j] = p[i][j] + q[i][j];
}
}
void func2()
{
int x[3][3], y[3][3], z[3][3];
void getmatrix(int[][3]);
void subtraction(int[][3], int[][3], int[][3]);
system(“cls”);
getmatrix(x);
getmatrix(y);
subtraction(x, y, z);
printf(“\n - : Matrix 1: - \n”);
display(x);
printf(“\n - : Matrix 2: - \n”);
display(y);
printf(“\n - : Matrix Subtraction (Result): - \n”);
display(z);
}
void subtraction(int p[3][3], int q[3][3], int r[3][3])
{
int I, j;
for (I = 0; I < 3; i++)
{
for (j = 0; j < 3; j++) r[i]
[j] = p[i][j] – q[i][j];
}
}
void func3()
{
int x[3][3], y[3][3], z[3][3];
void getmatrix(int[][3]);
void multiplication(int[][3], int[][3], int[][3]);
system(“cls”);
getmatrix(x);
getmatrix(y);
multiplication(x, y, z);
printf(“\n - : Matrix 1: - \n”);
display(x);
printf(“\n - : Matrix 2: - \n”);
display(y);
printf(“\n - : Matrix Multiplication (Result): - \n”);
display(z);
}
void multiplication(int p[][3], int q[3][3], int r[3][3])
{
int I, j, k;
for (I = 0; I < 3; i++)
//condition i< total row of matrix1
{
for (j = 0; j < 3; j++)
//condition i< total col of matrix1 or//condition i< total row of
matrix2
{
r[i][j] = 0;
for (k = 0; k < 3; k++) //condition i< total col of matrix2 r[i]
[j] = r[i][j] + (p[i][j] * q[j][k]);
}
}
}
void func4()
{
int x[3][3], y[3][3];
void getmatrix(int[][3]);
void transpose(int[][3], int[][3]);
system(“cls”);
getmatrix(x);
transpose(x, y);
printf(“\n - : Matrix 1: - \n”);
display(x);
printf(“\n - : Transpose Matrix : - \n”);
display(y);
}
void transpose(int p[][3], int q[][3])
{ int I, j;
for (I = 0; I < 3; i++)
{
for (j = 0; j < 3; j++)
q[i][j] = p[j][i];
}
}
void func5()
{
int x[3][3], y[3][3];
void getmatrix(int[][3]);
void transpose(int[][3], int[][3]);
int symmetric(int[][3], int[][3]);
system(“cls”);
getmatrix(x);
transpose(x, y);
if (symmetric(x, y) == 1) printf(“\
nMatrix is Symmetric”);
else
printf(“\nMatrix is Not Symmetric”);
}
int symmetric(int p[][3], int q[][3])
{
int I, j;
for (I = 0; I < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (q[i][j] != p[i][j])
return 0;
}
}
return 1;
}
void display(int m[][3]){
int I, j;
printf(“\n\n”);
for (I = 0; I < 3; i++)
{
for (j = 0; j < 3; j++)
printf(“%d “, m[i][j]);
printf(“\n”);
}
}
Output-
Enter element [0] l
[0] Enter element 2
[0][i] Enter 3
element [0][2]
Enter element [1] 5ñ
0] 7
Enter element [1] 8
[1] 9
Enter element [1] 0
[2] l
Enter element [2] 2
[0] 3 8 ' c:\Users\soura\Documents\Basic program'
Enter element [2]
[1]
Enter element [2]
[2]
Enter element [0]
[0]
Enter element [0]
[1]
EnterMatrix
element [0] 9
[2] l:
Enter element [1]
0]
Enter element
9Enter element
Enter element
EnterMatrix
element
2:
Enter element [2]
[2]
30 72
840 lo8
£8 135 0
Experiment – 8.1
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
int arr[40];
int N, i;
int *ptr = arr;
ptr = arr;
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
int sourceArr[40];
int destArr[40];
int size;
printf("Enter size of array: ");
scanf("%d", &size);
getch();
return 0;
}
Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
int upper = 0, lower = 0, num = 0, special = 0;
FILE *fp;
char c;
fp = fopen("test.txt", "r");
if (fp == NULL) {
printf("Could not open file test.c");
return 1;
}
while (1)
{
c = fgetc(fp);
if (c == EOF)
break;
if (c >= 'A' && c <= 'Z')
upper++;
else if (c >= 'a' && c <= 'z')
lower++;
else if (c >= '1' && c <= '9')
num++;
else
special++;
}
fclose(fp);
printf("\nUpper case letters: %d", upper);
printf("\nLower case letters: %d", lower);
printf("\nnumbers: %d", num); printf("\
nSpecial characters: %d", special); getch();
return 0;
}
Output-
Experiment – 8.4
Aim: Write a Program to store records of a student in student file. The data
must be stored using Binary File. Read the record stored in “Student.txt”
file in Binary code. Edit the record stored in Binary File. Append a record in
the Student file.
Code-
#include <stdio.h>
#include <conio.h>
Code-
#include <stdio.h>
#include <stdlib.h>
main(int argc, char *argv[])
{ FILE *fp1, *fp2;
char ch;
if (argc != 3)
{
printf("\n insufficient arguments.\n");
exit(0);
}
fp1 = fopen(argv[1], "r");
fp2 = fopen(argv[2], "w");
if (fp1 == NULL || fp2 == NULL)
{
printf("\n File not created.\n");
exit(0);
}
if (NULL != fp1)
{
fseek(fp1, 0, SEEK_END);
int size = ftell(fp1);
if (0 == size)
{
printf("File is empty.\n");
exit(0);
}
}
while (!feof(fp1))
{
ch = fgetc(fp1);
fputc(ch, fp2);
}
printf("\n File successfully copied ");
fclose(fp1);
fclose(fp2);
}
Output-