Experiment 1: Aim: Code
Experiment 1: Aim: Code
Experiment 1: Aim: Code
Aim:
Code:
include<stdio.h>
int main()
intn,p,i=0,digit,large=0;//variables
scanf("%d",&n);//taking input
while(n>0)
digit=n%10;//finding digit
n=n/10;//dividing number by 10
return 0;
Output:
7865
Aim:
Code:
#include<stdio.h>
int main()
intn,p,i=0,digit,small=9;//variables
scanf("%d",&n);//taking input
while(n>0)
digit=n%10;//finding digit
if(digit==0)
{
small=0;//assigning 0 to small
if(digit<=small)
n=n/10;//dividing number by 10
return 0;
Output:
76545412
Smallest digit in number 76545412=1
#include <stdio.h>
int count_3s(int n)
{
int count = 0;
while (n > 0)
if (n % 10 == 3)
count++;
n = n / 10;
return count;
intcount_in_range(int n)
int count = 0 ;
count += count_3s(i);
return count;
int main()
int n;
scanf(“%d”, &n);
return 0;
}
Output
Enter the end value : 100
Total occurrences of 3 from 0 to 100 is 20
#include<stdio.h>
int main(){
/* 2D array declaration*/
intdisp[2][3];
/*Counter variables for the loop*/
inti, j;
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("Enter value for disp[%d][%d]:",i, j);
scanf("%d",&disp[i][j]);
}
}
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("%d ",disp[i][j]);
if(j==2){
printf("\n");
}
}
}
return0;
}
Experiment: 8.1
Aim:
Code:
#include <stdio.h>
intmain()
intarr[MAX_SIZE];
int N,i;
scanf("%d",&N);
for(i=0;i< N;i++)
scanf("%d",(ptr+i));
for(i=0;i< N;i++)
printf("%d, ",*(ptr+i));
return0;
}
Output:
Output
Enter size of array: 10
Enter elements in array:
1
2
3
4
5
6
7
8
9
10
Array elements: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
Experiment: 8.2
Aim:
Code:
/**
* C program to swap two arrays using pointers
*/
#include <stdio.h>
/* Function declarations */
voidinputArray(int*arr,int size);
voidprintArray(int*arr,int size);
voidswapArray(int*sourceArr,int*destArr,int size);
intmain()
{
intsourceArr[MAX_SIZE];
intdestArr[MAX_SIZE];
int size;
/*
* Print elements of both arrays before swapping
*/
printf("\n\nSource array before swapping: ");
printArray(sourceArr, size);
/*
* Print elements of both arrays after swapping
*/
printf("\n\nSource array after swapping: ");
printArray(sourceArr, size);
return0;
}
/**
* Function used to read input from user in an array.
*
* @arr Pointer to array to store input
* @size Size of the array
*/
voidinputArray(int*arr,int size)
{
// Pointer to last element of array.
int*arrEnd=(arr+(size -1));
/**
* Function used to print elements of array.
*
* @arr Pointer to array, which is to print.
* @size Size of the array
*/
voidprintArray(int*arr,int size)
{
// Pointer to last element of array.
int*arrEnd=(arr+(size -1));
/*
* Swap individual element of both arrays
*/
while(sourceArr<=sourceArrEnd&&destArr<=destArrEnd)
{
*sourceArr^=*destArr;
*destArr^=*sourceArr;
*sourceArr^=*destArr;
}
}
Output
Enter size of array: 10
Enter 10 elements in source array: 1 2 3 4 5 6 7 8 9 10
Enter 10 elements in destination array: 10 20 30 40 50 60 70 80 90 100
Experiment: 8.3
Aim:
Code:
#include<stdio.h>
#include <string.h>
intmain()
chars[1000];
inti,alphabets=0,digits=0,specialcharacters=0;
gets(s);
for(i=0;s[i];i++)
{
if((s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122))
alphabets++;
elseif(s[i]>=48&&s[i]<=57)
digits++;
else
specialcharacters++;
printf("Alphabets = %d\n",alphabets);
printf("Digits = %d\n",digits);
return0;
}
Output:
1 Enter the string:hello1234!@#$%
2 Alphabets=5
3 Digits=4
4 Special characters=5
Experiment: 8.4
Aim:
Code:
#include<stdio.h>
struct stud
{
intrno;
float per;
char name[20],add[20];
}s;
int main()
{
FILE *fp;
fp=fopen("student.txt","w");
printf("Enter record of student:\n\n");
printf("\nEnter student number : ");
scanf("%d",&s.rno);
printf("\nEnter name of student: ");
scanf("%s",s.name);
printf("\nEnter student address : ");
scanf("%s",s.add);
printf("\nEnter percentage of student : ");
scanf("%f",&s.per);
fprintf(fp,"%d\n%s\n%s\n%f",s.rno,s.name,s.add,s.per);
printf("\nRecord stored in file...");
fclose(fp);
return 0;
}
1. #include<stdio.h>
2. int main(){
3. char name[50];
4. intmarks,i,n;
6. scanf("%d",&n);
7. FILE *fptr;
8. fptr=(fopen("C:\\student.txt","a"));
9. if(fptr==NULL){
10. printf("Error!");
11. exit(1);
12. }
13. for(i=0;i<n;++i){
15. scanf("%s",name);
17. scanf("%d",&marks);
19. }
20. fclose(fptr);
21. return0;
22.}
Output:
Experiment: 8.5
Aim:
Code:
#include <stdio.h>
int main()
char filename[100], c;
scanf("%s", filename);
if (fptr1 == NULL)
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
if (fptr2 == NULL)
exit(0);
c = fgetc(fptr1);
while (c != EOF)
fputc(c, fptr2);
c = fgetc(fptr1);
fclose(fptr1);
fclose(fptr2);
return 0;
Output:
Enter the filename to open for reading
a.txt
Enter the filename to open for writing
b.txt
Contents copied to b.txt