[E] Attempt the following:
(a)Write a program to print all prime numbers from 1 to 300. (Hint: Use nested loops, break and continue)
click here for Answer!!!
Answer:
void main()
{
int no,i,rem,j,flag=0;
for(no=2;no<=300;no++) { flag=0; j=no/2; for(i=j;i>=2;i--)
{
rem=no%i;
if(rem==0)
{
flag=1;
break;
}
}
if(flag!=1)
printf("%d\n",no);
}
}
(b)Write a program to fill the entire screen with a smiling face. The smiling face has an ASCII value 1.
click here for Answer!!!
Answer:
void main()
{
int smiling=1,i;
for(i=1;i<=2000;i++)
{
printf("%c",smiling); }
}
(c)Write a program to add first seven terms of the following series using a for loop: 1/1! + 2/2! + 3/3! + .........
click here for Answer!!!
Answer:
void main()
{
int factorial,fact,i;
float sum=0.0;
i=0;
while(i<=7) { fact=i;//for finding factorial see note: 1. factorial=1; while(fact>=1)
{
factorial=factorial*fact;
fact--;
}
sum=sum+(float)i/factorial;//int/int gives integer value so
// we converted into float value.
i++;
}
printf("sum upto 7th term is %f",sum);
}
(d)Write a program to generate all combinations of 1, 2 and 3 using for loop.
click here for Answer!!!
Answer:
void main()
{
int i,j,k,l,count=0;
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
for(k=1;k<=4;k++)
{
for(l=1;l<=4;l++)
{
if(i!=j&&j!=k&&i!=k&&i!=l&&j!=l&&k!=l)
{
printf("\n%d %d %d %d",i,j,k,l); count++; }
}
}
}
}
printf("\ncount = %d",count);
}
(e) According to a study, the approximate level of intelligence of a person can be calculated using the following formula: i = 2 + ( y + 0.5 x ) Write a program, which will produce a table of values of i, y and x, where y varies from 1 to 6, and, for each value of y, x varies from 5.5 to 12.5 in steps of 0.5.
click here for Answer!!!
Answer:
void main()
{
int y;
float i,x;
for(y=1;y<=6;y++)
{
for(x=5.5;x<=12.5;)//x varies from5.5 to 12.5 so
{
i=2+(y+0.5*x); //formula
printf("i=%f y=%d x=%f\n",i,y,x);//displaying
x=x+0.5; //x increase in step of 0.5 by question
}
}
}
(f)Write a program to produce the following output:
A B C D E F G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
click here for Answer!!!
Answer:
void main()
{
int i,j,k,count1,count2=0;
for(i=104;i>=97;i--)
{
for(j=97;j<=104;j++)
{
if(i>=j)
printf("%c ",j);
}
for(count1=0;count1=97;k--)
{
if(k<=i&&k!=104)
printf("%c ",k);
}
if(i==103)
count2=count2+1;
else
count2=count2+2;
printf("\n"); //new line for each i.
}
}
[7]int i=10;
(g)Write a program to fill the entire screen with diamond and heart alternatively. The ASCII value for heart is 3 and that of diamond is 4.
click here for Answer!!!
Answer:
void main()
{
int i;
for(i=0;i<=600;i++)
{
printf(" %c %c",3,4);
}
}
(h)Write a program to print the multiplication table of the number entered by the user. The table should get displayed in the following form. 29 * 1 = 29 29 * 2 =58 ....
click here for Answer!!!
Answer:
void main()
{
int no,i,multiply;
printf("enter number for multiplication table:");
scanf("%d",&no);
for(i=1;i<=9;i++)//we can make upto any number.
{
multiply=no*i;
printf("\n%d * %d = %d",no,i,multiply);
}
}
(i) Write a program to produce the following output:
1
2 3
4 5 6
7 8 9 10
click here for Answer!!!
Answer:
void main()
{
int i,j,k,cnt=1,icpy,icpy1,space,count=10;
for(i=1;i<=5;i++) { for(space=count;space>=1;space--)
/*this is for managing spaces. atfirst there will be 10 spaces
which gradually decreases by 1.In output 2 is at 1
space left of 1.so, we decrease by 1*/
{
printf(" ");
}
count=count-1;
icpy=i;
for(j=1;j<=i;j++)
{
printf("%d ",icpy);
icpy++;
}
printf("\n");
}
}
(j)Write a program to produce the following output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
click here for Answer!!!
Answer:
void main()
{
int a,b,c,line,space,i,j,n,r,ncr,fact_r,fact_n,factn_r;
printf("please enter the number of lines of the pascal triangle: \n");
scanf("%d",&line);
printf("\n");
for(i=0;i<=line;i++)
{
fact_n=1;
fact_r=1;
factn_r=1;
for(space=line;space>=i;space--)
{
printf(" ");
}
for(j=0;j<=i;j++) { n=i; r=j; fact_n=1; fact_r=1; factn_r=1; if(n==0) fact_n=1; else { for(a=n;a>=1;a--)
fact_n=fact_n*a;
}
//factorial of fact_r.
if(r==0)
fact_r=1;
else
{
for(b=r;b>=1;b--)
fact_r=fact_r*b;
}
if((n-r)==0)
factn_r=1;
else
{
for(c=(n-r);c>=1;c--)
factn_r=factn_r*c;
}
ncr=fact_n/(fact_r*factn_r);
c(n,r)
printf("%2d",ncr); //2 space for each ncr
}
printf("\n"); //new line after each row.
}
}
(k)A machine is purchased which will produce earning of Rs. 1000 per year while it lasts. The machine costs Rs. 6000 and will have a salvage of Rs. 2000 when it is condemned. If 12 percent per annum can be earned on alternate investments what would be the minimum life of the machine to make it a more attractive investment compared to alternative investment?
click here for Answer!!!
Answer:
void main()
{
int year=0,inv,altn;
while(altn>inv)
{
year++;
altn=120*year; // 12%of 1000 = 120
inv=(1000*year)-4000;
}
printf("The minimum year is %d",year);
}
(l) When interest compounds q times per year at an annual rate of r % for n years, the principle p compounds to an amount a as per the following formula
a = p ( 1 + r / q ) ^ nq
Write a program to read 10 sets of p, r, n & q and calculate the corresponding as.
click here for Answer!!!
Answer:
void main()
{
int p,r,n,q,a,i,j,product;
float amt=1;
for(i=0;i<10;i++)
{
amt=1;
printf("enter the values of p,r,n and q: "); scanf("%d%d%d%d",&p,&r,&n,&q);
product=n*q;
for(j=1;j<=product;j++)
{
amt = amt * ( 1 +(float) r / q );
}
amt= p *amt;
printf("%f",amt);
}
}
(m)The natural logarithm can be approximated by the following series.
(x-1)/x + 1/2((x-1)/2)^2 + 1/2((x-1)/2)^3 + ...
If x is input through the keyboard, write a program to calculate the sum of first seven terms of this series.
click here for Answer!!!
Answer:
void main()
{
int x,i,j;
float sum=0,power=1;
printf("enter x for sum upto 7th term: ");
scanf("%d",&x);
for(i=1;i<=6;i++)
{
power=1;
for(j=0;j<=i;j++)
{
power = power * ((x-1.0)/2.0);
}
sum = (1.0/2) * power + sum;
}
sum=sum + (float)(x-1.0)/x; printf("%f",sum);
}
write a factorial program in c++ 1-1/1!+2/2!-3/3!+....+n/n! using for loop
ReplyDelete#include
Deleteint i,j;
static float total;
int main(int argc,char ** argv) {
for(i=1;i<=7;i++) {
printf("\n");
int factorialNumber = 1;
static float sum;
//calculating factorial
for (j = 1; j <= i; j++) {
factorialNumber *= j;
}
sum = i/factorialNumber;
printf(" Factorial are : %d",factorialNumber);
printf(" sum is : %f",sum);
total +=sum;
}
printf("\n\n\n");
printf("Total of all 7 is: %f\n",total);
return 0;
}
first question's answer is wrong.it is showing all numbers from 1 to 300
ReplyDeleteanswer is
Delete#include
#include
int main()
{
int i,a,n,pn;
printf("prime number between 1 and 300 is ");
for(i=2;i<=300;i++)
{
for(a=2;a<=i-1;a++)
{
if(i%a==0)
break;
else
continue;
}
if(i==a)
{
printf("%d\t",i);
}
}
return 0;
getch();
}
first question's answer is wrong.it is showing all numbers from 1 to 300
ReplyDelete#include
Delete#include
#include
void main(void)
{clrscr();
int num,i,n=300;
for(num=1;num<=300;num++)
{
i=2;
while(i<n-1)
{
if(num%i==0)
break;
i=i+1;
}
if(num==i)
cout<<num<<" ";
}
getch();
}
This is bullshit.....u should run ur code before pasting it here.
ReplyDeleteMost of them are incorrect.
Appreciation for nice Updates, I found something new and folks can get useful info about BEST ONLINE TRAINING
ReplyDeleteAppreciation for nice Updates, I found something new and folks can get useful info about BEST ONLINE TRAINING
A B C D.......program is wrong
ReplyDelete#include
Delete#include
#include
void main(void)
{clrscr();
int a,x,n=71,o=70,y=1,c;
for(x=1;x<=7;x++)
{
for(a=65;a<=n;a++)
printf("%c",a);
for(c=2;c=65;a--)
printf("%c",a);
printf("\n");
n--;
o--;
y=y+2;
}
getch();
}
#include
ReplyDeletemain( )
{
int i,j,k=0,l;
for(i=1;i<=7;i++) //loop for 7 rows
{
for(j=1;j<=8-i;j++) //ASCII values upto current value of i
printf("%c ",64+j); //chracter + blank space
j--; //at termination of loop,j will be max_value+1(>8-i),so decrement it;
for(k=1;i>1&&k<=13-2*j;k++)//in first row no space is needed,but in later
printf(" "); //rows,13-2*(chracterw printed uptill now in current row)
k--; //same reason as j
for(l=13-j-k;l>=1;l--) //chracters printed in reverse order of characters printed
printf("%c ",64+l); //uptill now in current row(other than blanks)
printf("\n"); //insert a newline
sleep(100);
}
}
i am new to c,can u please explain f part in detail
ReplyDeleteQ. Five numbers are entered from the keyboard into an array. Write a program to find out the sum of all the numbers in the array.
ReplyDelete#include
Delete#include
#include
void main(void)
{
long num,sum;
printf("\ngive any number which contain digit");
scanf("%ld",&num);
sum=num%10+(num%100)/10+(num%1000)/100+(num%10000)/1000+num/10000;
printf("\nsum of number = %ld",sum);
getch();
}
Please check the soln of question [f] & do it correctly
ReplyDeleteThank You and that i have a nifty provide: How Much Should House Renovations Cost brick house exterior makeover
ReplyDelete