Cycle 3
1. Write a C program using structure to read and display details of ‘n’ employees.
Program
#include <stdio.h>
/*structure declaration*/ Output
struct employee{
Enter details :
char name[30];
Name ?:Mike
int empId;
float salary; ID ?:1120
}; Salary ?:76543
void main()
{
Entered detail is:
/*declare structure variable*/
struct employee emp; Name: Mike
Id: 1120
/*read employee details*/
Salary: 76543.000000
printf("\nEnter details :\n");
printf("Name ?:"); gets(emp.name);
printf("ID ?:"); scanf("%d",&emp.empId);
printf("Salary ?:"); scanf("%f",&emp.salary);
/*print employee details*/
printf("\nEntered detail is:");
printf("Name: %s" ,emp.name);
printf("Id: %d" ,emp.empId);
printf("Salary: %f\n",emp.salary);
}
2. Write a C program to read details of ‘n’ students and search a student using given
roll number.
Program Output
#include < stdio.h >
#include < conio.h >
struct stud
{
int roll;
char name[15];
int marks;
};
void main()
{
struct stud a[50];
int n,reply,sroll,option,i;
clrscr();
do
{
printf("\n 1. Create records");
printf("\n 2. Search record");
printf("\n 3. exit");
printf("\n\n Select proper option :");
scanf("%d",&option);
switch(option)
{
case 1 : // create
printf("\nEnter n : ");
scanf("%d",&n);
read_data(a,n);
break;
case 2 : // search
printf("\nEnter sroll : ");
scanf("%d",&sroll);
reply = search_data(a,n,sroll);
if( reply == -1)
printf("\n Not found");
else
{
printf("\nRoll\tName\tMarks");
printf("\n%d\t%s\t
%d",a[reply].roll,a[reply].name,a[reply].marks);
}
break;
case 3 : exit(0);
} //swith
}while(1);
} // main
int read_data(struct stud a[], int n)
{
int i;
printf("\nEnter %d records\n",n);
for(i=0;i < n;i++)
{
printf("\nRoll : "); scanf("%d",&a[i].roll);
printf("\nName : "); flushall(); scanf("%s",a[i].name);
printf("\nMarks : "); scanf("%d",&a[i].marks);
} // for
return;
} // read data
int search_data( struct stud a[], int n , int sroll)
{
int i;
for(i=0;i < n;i++)
{
if( a[i].roll == sroll)
return(i);
}//for
return(-1);
}
3. Write a C program to read details of ‘n’ students with 4 marks and display student
name based on the total mark.
Program
#include<stdio.h> Output
struct student
{
int rollno;
char name[20];
char college[40];
int score;
};
void main()
{
struct student s[20],temp;
int i,j,n;
clrscr();
printf("\nEnter no. of Students : ");
scanf("%d",&n);
printf("\nEnter the rollno,name,college name,score ");
for(i=0;i<n;i++)
scanf("%d%s%s%d",&s[i].rollno,s[i].name,s[i].college,&s[i].score);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(s[j].score<s[j+1].score)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
printf("\nThe Merit List is :\n");
for(j=0;j<n;j++)
printf("%d\t%s\t%s\t%d\n",s[j].rollno,s[j].name,s[j].college,s[j].score);
getch();
}
4. Write C programs to demonstrate the following pointer concepts
a. Basic knowledge of pointer
b. Basics understanding of & and * operators
c. Pointer to pointer (Chain of pointers)
Programs Output
1. Basic knowledge of pointer Address of variable a = fff11440
#include <stdio.h>
void main () { Address of a stored in variable p = fff11440
int a = 20;
int *p; Content of p variable = 20
p = &a;
printf("Address of variable a = %p\t", &a );
printf("Address of a stored in variable p = %p\n", p );
printf("Content of p variable = %d\n", *p );
}
2. Basics understanding of & and * operators.
#include <stdio.h>
Output
void main()
{ Value of variable var is: 10
int *p;
int var = 10; Value of variable var is: 10
p= &var;
Address of variable var is: ff114466
printf("Value of variable var is: %d", var);
printf("\nValue of variable var is: %d", *p); Address of variable var is: ff114466
printf("\nAddress of variable var is: %p", &var);
printf("\nAddress of variable var is: %p", p); Address of pointer p is: ff114468
printf("\nAddress of pointer p is: %p", &p);
3. Pointer to pointer (Chain of pointers)
#include <stdio.h>
void main()
{
int num=123; Output
int *p1; Value of num is: 123
int **p2;
p1 = # Value of num using p1 is: 123
p2 = &p1;
Value of num using p2 is: 123
printf("\n Value of num is: %d", num);
printf("\n Value of num using p1 is: %d", *p1); Address of num is: ff114466
printf("\n Value of num using p2 is: %d", **p2);
printf("\n Address of num is: %p", &num); Address of num using p1 is: ff114466
printf("\n Address of num using p1 is: %p", p1);
Address of num using p2 is: ff114466
printf("\n Address of num using p2 is: %p", *p2);
printf("\n Value of Pointer p1 is: %p", p1);
printf("\n Value of Pointer p1 using p2 is: %p", *p2);
printf("\n Address of Pointer p1 is:%p",&p1);
printf("\n Address of Pointer p1 using p2 is:%p",p2);
printf("\n Value of Pointer p2 is:%p",p2);
printf("\n Address of Pointer p2 is:%p",&p2);
}
5. Write a C program to read the content of a file.
Program
#include <stdio.h>
#include <stdlib.h> // For exit()
int main() Output
{
Enter the filename to open
FILE *fptr;
a.txt
char filename[100], c;
/*Contents of a.txt*/
printf("Enter the filename to open \n");
scanf("%s", filename); Hello World
// Open file
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
// Read contents from file
c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fptr);
}
fclose(fptr);
return 0;
}
6. Write a python program to print your name and details.
Program
def personal_details(): Output
name, age = "Simon", 19
Name: Simon
address = "Bangalore, Karnataka, India"
Age: 19
print("Name: {}\nAge: {}\nAddress: {}"
.format(name, age, address)) Address: Bangalore, Karnataka, India
personal_details()
7. Write a python program to print factors of a number using function
Program
Output
Enter a number 320
def print_factors(x):
# This function takes a number and prints the factors The factors of 320 are:
print("The factors of",x,"are:") 1
for i in range(1, x + 1): 2
if x % i == 0:
4
print(i)
5
# change this value for a different result.
num = int(input("Enter a number: ")) 8
print_factors(num) 10
16
20
32
40
64
80
160
320