ACP Unit-2 Programs

ACP Unit 2 programs

1. Write a user defined function which will swap the values of two
variables declared locally in the main program.
ANS)
#include<stdio.h>
#include<conio.h>
void swap(int *a,int *b);
void main()
{
int n1,n2;
clrscr();
printf("Enter value 1 = ");
scanf("%d",&n1);
printf("Enter value 2 = ");
scanf("%d",&n2);
printf("\nValues After swapping.........\n");
swap(&n1,&n2);

printf("N1 = %d \nN2 = %d ",n1,n2);
getch();
}
void swap(int *a,int *b)
{
int tmp;
//clrscr();
tmp=*b;
*b=*a;
*a=tmp;
}


2. Write a user defined function calc(), which will returns the sum,
subtraction, multiplication, and division values of two variable
locally declared in the main function.
ANS)
#include<stdio.h>
#include<conio.h>
void calc();
void main()
{
int n1,n2,add,sub,mul,div;
clrscr();
printf("\nEnter value 1 = ");
scanf("%d",&n1);
printf("\nEnter value 2 = ");
scanf("%d",&n2);
if(n1<=0 || n2<=0)
{
printf("Please Enter positive values......!");
getch();
exit(0);
}
else
{
calc(&n1,&n2,&add,&sub,&mul,&div);
printf("\nAddition = %d",add);
printf("\nSubstraction = %d",sub);
printf("\nMultiplication = %d",mul);
printf("\nDivsion = %d",div);
getch();
}
}
void calc(int *a,int *b,int *add,int *sub,int *mul,int *div)
{
*add= *a + *b ;
*sub= *a - *b ;
*mul= (*a) * (*b) ;
*div= *a / *b;
}


3. Write a user defined function which will return the length of the
string declared locally in the main function.
ANS)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void length();
void main()
{
char str[50];
int len=0;
clrscr();
printf("Enter any string = ");
gets(str);

length(&str,&len);
printf("\n\nLength of string = %d",len);
getch();
}
void length(char *a,int *len)
{
*len = strlen(a);
}


4. Write a program, which takes a name of the user in the
lowercase letters. Call a user defined function upper which will
convert all lowercase letters into the uppercase letter. Finally print
the string.
ANS)
#include<stdio.h>
#include<conio.h>
#include<string.h>
int upper(char *text);
void main()
{
char str[50];
clrscr();
printf("Enter a string = ");
gets(str);
upper(&str);
printf("Upper string = %s",str);
getch();
}

int upper(char *text)
{
text = strupr(text);
return *text;
}


5. Write a user defined function to reverse the given string.
ANS)
#include<stdio.h>
#include<conio.h>
#include<string.h>
int rev(char *text);
void main()
{
char str[50];
clrscr();
printf("Enter a string = ");
gets(str);
rev(str);
printf("Reverse string = %s",str);
getch();
}

int rev(char *text)
{
text = strrev(text);
return *text;
}


6. Create a structure product with ProductCode (int), Name (char
array) and Price data elements. In the main function declare p[5] of
product. Do the necessary data entry for all five products. Pass the
base address of an array to user defined function inc_price(), which
will increase the price of all the products by 10%. Print all the
products with all the details again after increasing the price.
ANS)
#include<stdio.h>
#include<conio.h>
void inc_price();
struct product
{
char p_name[30];
int p_code,p_price;
};
void main()
{
int i;
struct product p[5];
clrscr();
printf("---------Enter product detail-------------");
for(i=0;i<5;i++)
{
printf("\nEnter product %d code = ",i+1);
scanf("%d",&p[i].p_code);     flushall();
printf("Enter product %d Name =",i+1);
gets(p[i].p_name);
printf("Enter product %d price = ",i+1);
scanf("%d",&p[i].p_price);
}
inc_price(p);
printf("\n-----------Product detail (with incre..price)-----------\n\n");
printf("Product Code\tProduct Name\tProduct Price");
for(i=0;i<5;i++)
{
printf("\n%d\t\t%s\t\t%d",p[i].p_code,p[i].p_name,p[i].p_price);
}
getch();
}
void inc_price(struct product p[])
{
int i;
clrscr();
for(i=0;i<5;i++)
{
p[i].p_price = p[i].p_price + (p[i].p_price*10/100);
}
}


7. Create a structure student with rollno (int), name (char array),
marks (int), grade (char). Create an array stu[5] of type student.
Take the details of students like rollno, name, and marks from the
user. Call UDF prepare_result() which will store the values for
grade based on marks (if marks >=75 then ‘A’, Between 60 to 75
‘B’, Between 50 to 60 ‘C’, Between 35 to 50 ‘D’ and Below 35 ‘F’
grade). Print the details of the students with Grade.
ANS)
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20],grade;
int roll,marks;
};
void prepare_result(struct student[]);
void main()
{
struct student s[2];
int i=0,j;
clrscr();
printf("\n-----Enter Students Detail-----\n");
for(i=0;i<2;i++)
{
printf("\nEnter student %d roll no = ",i+1);
scanf("%d",&s[i].roll); flushall();
printf("Enter student %d name = ",i+1);
gets(s[i].name);
printf("Enter student %d marks(in per) = ",i+1);
scanf("%d",&s[i].marks);
}
prepare_result(s);
printf("\n---------------------------Student Detail-----------------------------");
printf("\n\nRoll no\t\tName\t\tMarks(in per)\t\tGrade");
for(i=0;i<2;i++)
{
printf("\n%d\t\t%s\t\t%d\t\t\t%c",s[i].roll,s[i].name,s[i].marks,s[i].grade);
}
getch();
}
void prepare_result(struct student s[])
{
int i=0;
clrscr();
for(i=0;i<2;i++)
{
if(s[i].marks>=75)
{
s[i].grade='A';
}
else if(s[i].marks>=60)
{
s[i].grade='B';
}
else if(s[i].marks>=50)
{
s[i].grade='C';
}
else if(s[i].marks>=35)
{
s[i].grade='D';
}
else
{
s[i].grade='F';
}
}
}

Comments

Popular posts from this blog

C++ Practice Program

Cloud Computing Important Question-Answer for University Exam

Software Testing Gujarat University Important Questions For Exam