Escolar Documentos
Profissional Documentos
Cultura Documentos
Ans.
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int a,b,i;
cin>>a>>b);
i=y(a,b);
getche();
y(int a,int b)
int l;
l=a>b?a:b;
return(l);
}
Q#2.State the advantage of creating your own header files. Explain how you may call your header file from a
c program.
Ans: Making own header file is simpler than defining function in every program. Any user define header
file can be used in any c program, we can call our header file in a c program.
e.g
#include<multi.h>
Int multi(int x)
int y;
y=x*x;
return(y);
#include<constream.h>
#include<multi.h>
void main()
int a,m;
cin>>a;
m=multi(x);
cout<<Result =<<m<<endl;
getche();
}
Q#4. Write a program that gets time in hours, minutes and seconds from the user, convert the time into
seconds using function. The function takes hours, minutes and seconds as arguments and returns the time in
seconds.
#include<constream.h>
void main()
clrscr();
int c,hrs,min,sec;
cin>>hrs;
cout<<Enter minute<<endl;
cin>>min;
cout<<Enter second<<endl;
cin>>sec;
c=time(hrs,min,sec);
getche();
int tot;
tot=(hrs*3600)+(min*60)+sec;
return(tot);
}
Q#6. Write a program which takes input four numbers and returns their average.
Ans.
#include<iostream.h>
#include<conio.h>
int a,b,c,d;
float avrg;
void main()
clrscr();
cin>>a>>b>>c>>d;
avgr= a+b+c+d/4
cout<<Average is<<avgr<<endl;
getch();
}
Q#5.Create function that calculate the result of the following equations.
y(x)=x^2-2x-3
y(a,b)=a^2-2ab+b^2
#include<conio.h>
int y(int x)
int a;
y=x*x-2*x-3;
return(y);
(ii) #include<iostream.h>
#include<conio.h>
int y;
y=a*a-2*a*b+b*b;
return(y);
}
Q#2. Write a program which prints following output.
*
***
*****
*******
*********
***********
#include<constream.h>
void main()
{clrscr();
int a=6;
for(int i=1;i<=a;i++)
for(int j=a-i;j>0;j--)
cout<<" ";
for(int k=1;k<=2*i-1;k++)
cout<<"*";
cout<<endl;
getch();
}
Q#1. Write a program which takes an input and then prints its table.
N x 1=
i. for loop
ii. while loop
iii. do while loop
(Enter any no for table) for e.g. 5
5x1=5 ..5x10=50
(i)FOR LOOP:
#include<constream.h>
void main()
clrscr();
int a,n;
cin>>n;
for(a=1;a<=10;a++)
cout<<"\t"<<n<<"*"<<a<<"="<<n*a<<"\n"; }
getch();
(II)WHILE LOOP:
#include<constream.h>
void main()
clrscr();
int n,b=1;
cin>>n;
while (b<=10)
cout<<"\t"<<n<<"*"<<b<<"="<<n*b<<"\n";
b++;
getch();
(iii)DO-WHILE LOOP:
#include<constream.h>
void main()
clrscr();
int n,i=1;
cin>>n;
do
cout<<n<<*<<i<<=<<n*i<<\n;
i++;
while (i<=10);
getch();
}
Q#3. Write a program which prints following output
#
##
###
####
#####
######
#######
#include<constream.h>
void main()
{
clrscr();
int i,j;
for(i=1;i<=7;i++)
{
cout<<"\n";
for(int j=1;j<=i;j++)
{
cout<<"#";
}
cout<<endl;
}
getch();
}
Q#4. Write a program which takes an input from, user and tells whether the no is prime or not?
#include<constream.h>
void main()
int n, i;
clrscr();
cin>>n;
for(i=2;i<n;i++)
if(n%i==0)
break;
if(i==n)
else
getch();
Q#5. Write a program that will calculate the sum of first 100 natural numbers.
#include< constream.h>
void main()
{
clrscr();
int i,sum;
cout<<This Program will sum the first 100 natural numbers<<endl;
for(i=1;i<=100;i++)
{
sum+=i;
}
cout<<Total sum is:<<sum<<endl;
}
getch()
}
Q#6. Write a program using two nested loops to generate the following output.
1 2 3 4 10
5 6 7 8 26
9 10 11 12 42
13 14 15 16 58
#include<constream.h>
void main()
{
clrscr();
int i,j,a=0,b=0;
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
b++;
a+=b;
cout<<b<<endl;
}
Cout<<a<<endl;
a=0;
cout<<"\n";
}
getch();
}
Q#7. Generate a series of numbers that are divided by 5, from 1 to 100.
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int i;
if(i%5==0)
cout<<i<<endl;
getch();