Você está na página 1de 12

Q#1.Write a program that prints out the larger of two numbers entered from the keyboard.

Use the function


to do the actual comparison of the two numbers. Pass the two numbers to the function as arguments, and
have the function return the answer with return()?

Ans.

#include<iostream.h>

#include<conio.h>

int y(int a, int b);

void main()

clrscr();

int a,b,i;

cout<<\nEnter two numbers<<endl;

cin>>a>>b);

i=y(a,b);

cout<<i is larger number"<<endl;

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

Our header file is

#include<multi.h>

Int multi(int x)

int y;

y=x*x;

return(y);

Now we are making a program using our own header:

#include<constream.h>

#include<multi.h>

void main()

int a,m;

cout<<Enter any number<<endl;

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>

int time( int hrs, int min, int sec);

void main()

clrscr();

int c,hrs,min,sec;

cout<<Enter hour <<endl;

cin>>hrs;

cout<<Enter minute<<endl;

cin>>min;

cout<<Enter second<<endl;

cin>>sec;

c=time(hrs,min,sec);

cout<<\nThe time in seconds is<<c<<endl;

getche();

int time(int hrs, int min, int sec)

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();

cout<<Enter four numbers<<endl;

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

Ans. (i) #include<iostream.h>

#include<conio.h>

int y(int x);

int y(int x)

int a;

y=x*x-2*x-3;

return(y);

(ii) #include<iostream.h>

#include<conio.h>

int y(int a, int b);

int y(int a, int b)

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;

cout<<"Enter any number:";

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;

cout<<Enter the number :<<endl;

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;

cout<<Enter the number :<<endl;

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();

cout<<Enter a number : <<endl;

cin>>n;

for(i=2;i<n;i++)

if(n%i==0)

break;

if(i==n)

cout<<n is Prime Number"<<endl;

else

cout<<n is not a Prime Number"<<endl;

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;

for(i=1; i<=100; i++)

if(i%5==0)

cout<<i<<endl;

getch();

Você também pode gostar