CP 24-25 Programs
CP 24-25 Programs
gets(), puts() */
#include<stdio.h>
#include<conio.h>
void main()
{
int division;
int roll_number,total_marks;
char name[50];
float percentage;
clrscr();
printf("\n Enter students name : ");
//scanf("%s",name);
gets(name);
printf(" Enter students division : ");
//scanf("%c",&division);
division=getchar();
printf(" Enter students roll number : ");
scanf("%d",&roll_number);
printf(" Enter students total marks : ");
scanf("%d",&total_marks);
percentage=total_marks/6.0;
printf("\n\n Details entered by you : \n\n");
// printf(" Students name : %s \n",name);
printf(" Students name : ");
puts(name);
// printf(" Students division : %c \n",division);
printf(" Students division : ");
putchar(division);
printf("\n Students roll number : %d",roll_number);
printf("\n Students percentage : %.2f",percentage);
getch();
}
// Output
// Exp 2: C program to demonstrate various operators
#include<stdio.h>
#include<conio.h>
void main()
{
int a=13,b=4,c=8,d=156,lowest;
clrscr();
//Output
//Exp4: C Program to demonstrate switch case construct
#include <stdio.h>
#include<conio.h>
void main()
{
int score;
clrscr();
getch();
}
// Output
//Exp 5: Armstrong number using while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int n, n1, rem,num=0;
clrscr();
printf(" Enter a positive integer: ");
scanf("%d",&n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
printf("\n %d is an Armstrong number.",n);
else
printf("\n %d is not an Armstrong number.",n);
getch();
}
// Output
//Exp 6: Armstrong number using do-while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int n, n1, rem,num=0;
clrscr();
printf(" Enter a positive integer: ");
scanf("%d",&n);
n1=n;
do
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}while(n1!=0);
if(num==n)
printf("\n %d is an Armstrong number.",n);
else
printf("\n %d is not an Armstrong number.",n);
getch();
}
// Output
// Exp7: Program to generate multiplication table of user entered number
#include<stdio.h>
#include<conio.h>
void main()
{
int num, i;
clrscr();
getch();
}
//Output
//Exp8: Program to demonstrate continue statement
#include<stdio.h>
#include<conio.h>
void main()
{
int num, choice, i;
clrscr();
void main()
{
int num1, num2;
clrscr();
// Original numbers
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
// Call by value
swapByValue(num1, num2);
// The original numbers remain unchanged after call by value
printf("After Call by Value (Main): num1 = %d, num2 = %d\n", num1, num2);
// Call by reference
swapByReference(&num1, &num2);
// The original numbers are swapped after call by reference
printf("After Call by Reference (Main): num1 = %d, num2 = %d\n", num1, num2);
getch();
}
// Output
Explanation:
1. swapByValue:
o This function takes two integers as arguments.
o Inside the function, the values are swapped, but the change does not reflect
outside the function because it's call by value.
2. swapByReference:
o This function takes two pointers to integers as arguments (int *a, int *b).
o It swaps the values at the memory locations pointed to by the pointers, directly
changing the original variables in the main function.
/*Exp10: Program to find sum of first n natural numbers using recursion*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum;
clrscr();
// Input number from user
printf("Enter a number: ");
scanf("%d", &n);
getch();
}
// Output
/*Exp11: Program to find GCD of two numbers by using Recursion*/
#include<stdio.h>
#include<conio.h>
// Recursive function to find GCD of two numbers
int gcd(int a, int b)
{
if (b == 0)
{
return a; /* Base case: if second number becomes 0, return first number*/
}
else {
return gcd(b, a % b); // Recursive case
}
}
void main()
{
int num1, num2,result;
clrscr();
// Input two numbers from user
printf(" Enter two numbers: ");
scanf("%d %d", &num1, &num2);
//Output
//Exp12: Program to find minimum and maximum number in an array
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, min, max, arr[15];
clrscr();
// Input the size of the array
printf("Enter the number of elements in the array(max 15): ");
scanf("%d", &n);
// Output
/* Exp13: Program to find length of a string and comparing two strings
without using standard string functions like strlen(), strcmp() */
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[50], str2[50];
int i=0, j=0, k=0, same=0, len1, len2;
clrscr();
//string length finding
printf("\n Enter any string : ");
gets(str1);
while(str1[i]!='\0')
i++;
len1=i;
printf("\n Length of entered string '%s' is %d",str1,len1);
// Output
/* Exp14: Program to multiply two matrices */
#include<stdio.h>
#include<conio.h>
void main()
{
int row1, col1, row2, col2,firstMatrix[5][5];
int secondMatrix[5][5], result[5][5],i,j,k;
// Matrix multiplication
for (i = 0; i < row1; i++) {
for (j = 0; j < col2; j++) {
for (k = 0; k < col1; k++) {
result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
getch();
}
// Output
//Exp15: Program to demonstrate nested structure
#include<stdio.h>
#include<conio.h>
// Define a structure for the employee
struct Employee
{
int empCode, salary;
char name[50];
struct
{
int day;
int month;
int year;
} dateOfJoining;
};
void main()
{
struct Employee employees[10]; // Array to store 10 employee records
int i;
clrscr();
// Read the details of each employee
for (i = 0; i < 3; i++)
{
printf("Enter details for employee %d:\n", i + 1);
printf("Employee Salary: ");
scanf("%d", &employees[i].salary);
printf("Employee Code: ");
scanf("%d", &employees[i].empCode);
fflush(stdin);
printf("Employee Name: ");
gets(employees[i].name); // Read string with spaces
printf("\n");
}
getch();
}
/* Output
Employee Salary: 32400
Employee Code: 3
Employee Name: Ramesh Sahu
Date of Joining (dd mm yyyy): 24 9 2010
Employee Records:
Employee 1:
Code: 1
Name: Sunil Shende
Salary: 24500
Date of Joining: 12.4.2005
Employee 2:
Code: 2
Name: Kirti Kumari
Salary: 12500
Date of Joining: 30.5.1999
Employee 3:
Code: 3
Name: Ramesh Sahu
Salary: 32400
Date of Joining: 24.9.2010
*/
/*Exp16: Program to demonstrate pointer arithmetic using array */
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5] = {10, 20, 30, 40, 50},i; // Initialize an array of 5 integers
int *ptr = arr; // Pointer pointing to the first element of the array
clrscr();
// Print the array elements using pointer arithmetic
printf("Array elements using pointer arithmetic:\n");
for (i = 0; i < 5; i++)
{
printf("Element %d: %d\n", i, *(ptr + i)); // Accessing element using pointer
arithmetic
}
getch();
}
/* Output
void main()
{
FILE *filePtr; // File pointer
char data[100]; // Buffer to store data
char readBuffer[100]; // Buffer to read data from file
clrscr();
// 1. Creating (or opening) a file for writing
filePtr = fopen("example.txt", "w");
if (filePtr == NULL)
{
printf("Error! Could not create file.\n");
exit(1); // Exit if file creation fails
}
getch();
}
/* Output
Enter some text to write to the file: Hi this is demo for file operations
Data written to the file successfully.
Data read from the file:
Hi this is demo for file operations
*/
Explanation:
fprintf(filePtr, "%s", data): Writes the user input (data) to the file using the file
pointer (filePtr).
fgets(readBuffer, sizeof(readBuffer), filePtr): Reads a line from the file into the
buffer (readBuffer).
The loop continues reading until the end of the file.
The fclose(filePtr) function is used to close the file after both writing and reading
operations.