q#1. to prepare table of any numbermechengr.weebly.com/uploads/1/1/4/2/11422107/assignment.docx ·...

28
5 prorammes of For loop. Q#1. To prepare Table of any Number //C Program to prepare Table of any Number using for loop #include<stdio.h> #include<conio.h> void main() { int n,t,count; clrscr(); printf("Enter any number\n\n"); scanf("%d",&n); for(count=1;count<=10;count++) { t=n*count; printf("\n%d*%d=%d",n,count,t); } getch(); } Q#2. To find sum. #include<stdio.h> #include<conio.h> void main() { int i,sum=0,n; for(i=0;i<5;i++) { printf("Enter number:\n"); scanf("%d", &n); sum+=n; }

Upload: phungtuong

Post on 30-Jul-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

5 prorammes of For loop.

Q#1. To prepare Table of any Number

//C Program to prepare Table of any Number using for loop#include<stdio.h>#include<conio.h>void main(){int n,t,count;clrscr();printf("Enter any number\n\n");scanf("%d",&n);for(count=1;count<=10;count++){t=n*count;printf("\n%d*%d=%d",n,count,t);}getch();}

Q#2. To find sum.

#include<stdio.h>#include<conio.h>

void main(){int i,sum=0,n;for(i=0;i<5;i++){printf("Enter number:\n");scanf("%d", &n);sum+=n;}printf("Sum of 5 numbers is: %d", n);}

Q#3. To fim sum of numbers.

#include<stdio.h>#include<conio.h>

Main()

{

Clrscr();float sum=0,temp;int i;printf("Enter five numbers:");for(i=0;i<5;i++);{scanf("%f",&temp);sum += temp;}printf("Sum is %f.",sum);getch();}

Q#4.Multiplication table.

#include<stdio.h>    #include<conio.h>    void main(void)    {    int i,j;    int n, c;    clrscr();    printf("\n\n ");    for(i=1; i<=5; i++)    {    for(j=1; j<=5; j++)        printf("\t%d", i*j);    printf("\n");    }    printf("\t");    getch();    }

Q#5. To make * form.

#include <stdio.h>#include <conio.h>

  void main(){       for(int i=1;i<=5;i++){           for(int j=1;j<=i;j++){             printf("*");           }           printf("\n");       }

   }

5 programmes of While loop.

Q#1. To prepare Table of any Number //C Program to prepare Table of any Number using while loop#include#includevoid main(){int n,t,count=1;clrscr();printf("Enter any number\n\n");scanf("%d",&n);while(count<=10){t=n*count;printf("\n%d*%d=%d",n,count,t);count++;}getch();}

Q#2. C Program to Calculate Factorial of a Number

//C Program to Calculate Factorial of a Number#include<stdio.h>#include<conio.h>void main(){int n,fact=1;clrscr();printf("Enter any Number:\n\n");scanf("%d",&n);while(n>1){fact=fact*n;n--;}printf("\nFactorial is=%d",fact);getch();}

Q#3. To form asterisk form.

#include<stdio.h>

#include<conio.h>

int count = 5;int i;while (count >= 1)

{i = count- -;while (i >= 1)printf("%d", i- -);printf("\n");}

Q#4. Display numbers

#include <stdio.h>

{

Clrscr();

int main()

{

 int num = 0;

printf("Using while loop\n");

  while(1)

{

   num = num + 1;printf("%d\n", num);

   if(num >= 5)

   break;

 }

Q#5. How to multiplying.

#include<stdio.h>

#include<conio.h>

void main (void)            {                float f1,f2;                float total;                int status1,status2;                printf("Enter first number to multiply:'n' to quit.\n ");                status1=scanf("%f",&f1);                printf("Enter another number to be multiply:'n' to quit.\n ");                status2=scanf("%f",&f2);                while (status1==1 && status2==1)                    {                        total=f1*f2;                        status1=scanf("%1.0f",&f1);                        status2=scanf("%1.0f",&f2);                    }                printf("Multiplition Total = %1.0f",total);                getch();            }

5programmes of Do-while loop

Q#1. To find temperature in celcius.

1. #include <stdio.h>2. #include <conio.h>3. #include <stdlib.h>4.5. main ()6.7. {8. int temp;9. float cel;10. char repeat;11. clrscr();12. do13. {14. printf ("Enter the temperature:");15. scanf ("%d",temp);16. cel=(5.0/9.0)*(temp-32);17. printf ("%d degree F is %f degree celsius\n",temp,cel);18. printf ("Do you have other temperature?");19. repeat=getchar();20. putchar('\n');21. }22. while (repeat=='y');23.24. getch();25. return 0;26.27. }

Q#2. Display line.

#include<stdio.h>#include<conio.h>void main()

{int i=0;clrscr();do{i=i+1;printf("%d This will be repeated 5 times\n", i);}while(i!=5); printf("End of the program");getch();}

Q#3. The do-while Loop / How to restart the program?

#include<stdio.h>#include<conio.h>void main(){ int num; char ch; clrscr(); do{  printf("Enter a number"); scanf("%d",&num); printf("\n\n"); printf("You entered %d",num); printf("Press 'y' to restart again?"); ch=getche(); }  while(ch=='y'); getch();

Q#4. Display numbers

#include <stdio.h>#include <conio.h>

Main()

{Clrscr();

printf("Using do while loop\n");

  int num = 0;

    do

    { num = num + 1;        printf("%d\n", num);}

while(num < 5);

getch();

}

Q#5. Display numbers from 0 to 9.#include<stdio.h>#include<conio.h>void main()

{int count=0;

Int total=0;

Do

{

Total=total +count;

Printf(“count=%d,total=%d\n”,count++,total);

}

While (count<10);

Getch();

}

2 programmes of Nested For loop programmes.

Q#1.

/*write a Program using nested for loop*/#include<stdio.h>#include<conio.h>void main(){ int r,c,sum; clrscr(); for (r=1;r<=3;r++) { for (c=1;c<=2;c++) { sum=r+c; printf("\nr=%d, c=%d, sum=%d",r,c,sum); } } getch(); }

Q#2. make a nested loop of equilateral triangle (using *)

int MAX ,i,j,k;for(int i = 0; i > MAX; i++) { for(int j = MAX; j < 0; j--) { //output a space } for(int k = 0; k>=i; k++) { //output a * }

getch();}

2 programmes of Nested while loop.

Q#1. To form asterisk form.

#include<stdio.h>

#include<conio.h>

int count = 5;int i;while (count >= 1)

{ i = count- -; while (i >= 1) ("%d", i- -); printf("\n");

Getch();}

Q#2. Find factorial of number typed.#include<stdio.h>

#include<conio.h>

Void main(void)

{

int number=1;

Int answer;

While(number!=0)

{

Printf(“\nenter a number:”);

Scanf(“%ld”,&number);

Answer=1;

While(number=1)

Answer=answer*number--;

Printf(“factorial is:%d\n,answer”);

}

Getch();

}

2 programmes of Nested do-while loop.

Q#1. To form multiplication table.

# include <stdio.h># include <conio.h>void main(){int r, c, y ;clrscr() ;r = 1 ;printf("The multiplication table is :\n\n") ;do{c = 1 ;do{y = r * c ;printf("%5d", y) ;c = c + 1 ;} while(c <= 10) ;printf("\n\n") ;r = r + 1 ;} while(r <= 10) ;getch() ;}

Q#2. Game guess a letter.

# include <stdio.h># include <conio.h>Void main(void)

Char ch;

Do

{

Printf(“\n\nType in a digit from ‘a’ to ‘e’:\n”);

While((ch=getche())!=’c’)

{

Printf(“\nSorry, %c is incorrect.\n”,ch);

Printf(“try again.\n”);

}

Printf(“\nThat’s it\n”);

Printf(“\nPlay again? (type ‘y’ or ‘n’):”);

}

While(getche()==’y’);

Printf(“\nThanks for playing!”);

}

5 programmes of If statement.

Q#1. Num find.

#include<stdio.h>

#include<conio.h>

Main()

{

Clrscr();

Int num;

Printf(“enter a number”\n);

Scanf(“%d”,&num);

If(num>0)

Printf(“given number is positive”);

getch();

}

Q#2. Count characters and wors in a phrase.

#include<stdio.h>

#include<conio.h>

Void main(void)

{

Int charcnt=0;

Int wordcnt=0;

Char ch;

Printf(“type in a phrase:\n”);

While((ch=getche())!= ‘\r’)

{

Charcnt++ ;

If (ch==’ ‘)

Wordcnt++

}

Printf(“\n Character count is %d”, charcnt);

Printf(“\nWord count is %d”, wordcnt+1);

}

Q#3. Multiple statements using if.

#include<stdio.h>

#include<conio.h>

Void main(void)

{

Char ch;

If (ch==’y’)

{

Printf(“\n You typed y.”);

Printf(“\nNot some other letter .”);

}

}

Q#4. Using if statement.

#include<stdio.h>

#include<conio.h>

Void main(void)

{

Char ch;

Ch= getche();

If (ch=’y’)

Printf(“you pressed y”);

Getch();

}

Q#5. Using if statement.

#include<stdio.h>#include<conio.h>void main(){

If (getche() ==’n’)

If (getche()== ‘o’ )

Printf(“\nYou typed no.”);

Getch();

}

5 programmes of If-Else statment .

Q#1. To find out the greater number.#include<stdio.h> void main() { int a=10,b=15; clrscr(); if(a>b) printf("%d is the large number",a); else printf("%d is the large number",b); getch(); }

Q#2. To find larger number if-else statement.#include<stdio.h>int main(){ int x,y; printf("Enter value for x :"); scanf("%d",&x); printf("Enter value for y :"); scanf("%d",&y); if ( x > y ){ printf("X is large number - %d\n",x); } Else{ printf("Y is large number - %d\n",y); } getch( );}

Q#3. Comparison programme.

#include <stdio.h> main(){ int x = 1;  if ( x = = 1 ) printf("x is equal to one.\n"); else printf("For comparison use = = as = is the assignment operator.\n");  getch();}

Q#4. If –else statement.

#include <stdio.h> main(){ Char ch;

Ch= getche();

If (ch= = ‘y’)

Printf (“ \nYou typed y.”);

Else

Printf(“\nyou did’t type y.”);

}

Q#5. To make diagonal lines on screen.

#include <stdio.h> main(){ Int x,y;

For (y=1:y<24;y++)

{

For (x=1;x<24;x++)

If ( x= = y)

Printf(“\xDB”);

Else

Printf(“\xBD”);

Printf(“\n”);

}

}

2 programmes of nested if statement.

Q#1. Using if nested statement.

#include<stdio.h>#include<conio.h>void main(){

If (getche() ==’n’)

If (getche()== ‘o’ )

Printf(“\nYou typed no.”);

Getch();

}

Q#2. Using if nested statement.

#include<stdio.h>#include<conio.h>void main(){

Int x,y;

Printf(“ enter a digit “);

Printf(“enter 2nd digit”);

{

If(scanf(“%d”,&x)

If ( scanf (“%d”,&y)

Printf(“you typed xy”);

Getch();

}

2 programmes od nested if-else statement.

Q#1. To find Maximum of 3 no.s

/*C Program to find Maximum of 3 nos. using Nested if*/#include<stdio.h>#include<conio.h>void main(){ int a,b,c; // clrscr(); printf("Enter three number\n\n"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if(a>c) { printf("\n a is maximum"); } else { printf("\n c is maximum"); } } else { if(b>c) { printf("\n b is maximum"); } else { printf("\n c is maximum"); } } getch(); }

Q#2. Using nested if statement.

#include<stdio.h>#include<conio.h>void main(){

If (getche() ==’n’)

If (getche()== ‘o’ )

Printf(“\nYou typed no.”);

}