loops using c++ excersice soloved qusitions, Exercises of C Sharp Programming

loops introduction and solved qustions of it. it helps sutdents to understood loop structure in c++.

Typology: Exercises

2019/2020

Uploaded on 04/27/2020

ayub-king
ayub-king 🇵🇰

1 document

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
N O T E S
C++
(Loops Programs)
Solved By:Aziz Ejaz
(MCS 1st semester)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download loops using c++ excersice soloved qusitions and more Exercises C Sharp Programming in PDF only on Docsity!

N O T E S

C++

(Loops Programs)

Solved By:Aziz Ejaz

(MCS 1

st

semester)

1. Program that print the sequence of all Even numbers. #include<conio.h> #include<iostream.h> void main() { clrscr(); int a, b; cout<<"Enter Starting Limit: "; cin>>a; cout<<"Enter ending Limit: "; cin>>b; cout<<"All Even Numbers Between "<<a<<" & "<<b<<" is\n"; while (a<=b) { if (a!=0 && a%2==0) cout<<a<<" "; a++; } getch(); } 2. Program that print the sequence of all Odd numbers. #include<conio.h> #include<iostream.h> void main() { clrscr(); int a, b; cout<<"Enter Starting Limit"; cin>>a; cout<<"Enter ending Limit"; cin>>b; cout<<"All Odd Numbers Between "<<a<<" & "<<b<<" is\n"; while (a<=b) { if (a!=0 && a%2!=0) cout<<a<<" "; a++; } getch();

5. Program that print the sequence. [8 12 17 24 28 33 up to 100]. #include<conio.h> #include<iostream.h> void main() { clrscr(); int n=1; cout<<"Required sequence is: "; while (n<97) { n=n+7; cout<<n<<" "; n=n+4; cout<<n<<" "; n=n+5; cout<<n<<" "; } getch(); } 6. Program that Calculate the sum of first n odd numbers. Test the program by taking sum of first 100 odd numbers. #include<conio.h> #include<iostream.h> void main() { clrscr(); int n, i=1, a=0; cout<<"Enter total terms to add: "; cin>>n; cout<<"Sum of first "<<n<<" Odd Numbers = "; n=n*2; while (i<=n) { if (i%2!=0) a=a+i; i++; } cout<<a; getch();

7. Program that read input and print its table up to giver limit. #include<conio.h> #include<iostream.h> void main() { clrscr(); long n, l, i; cout<<"Enter the Number: "; cin>>n; cout<<"Enter limit: "; cin>>l; for (i=1;i<=l;i++) { cout<<n<<""<<i<<"="<<ni<<endl; } getch(); } 8. Program that read +ve number integer and prints its factorial. #include<conio.h> #include<iostream.h> void main() { clrscr(); int n, i; unsigned long f=1; cout<<"Emter the Number: "; cin>>n; if(n<=0) cout<<"Please Enter valid number"; else { for (i=n; i>=1; i--) { f=f*i; } cout<<"Factorial of "<<n<<" = "<<f; } getch();

11. Program that calculates and prints the average of several numbers. Assume that last value read is sentinel 9999. #include<conio.h> #include<iostream.h> void main() { clrscr(); float x=0,n,sum=0; do { cout<<"Enter a value: "; cin>>n; sum=sum+n; x=x+1; } while(n!=9999); cout<<"Average of all numbers is: "<<sum/x; getch(); } 12. Program that determines the sequence of prime numbers within the range input by user. #include<conio.h> #include<iostream.h> void main() { clrscr(); int n,a,i=1,j; cout<<"Enter Limit: "; cin>>n; while (i<=n) { a=0; j=1; while (j<=i) { if (i%j==0) a++; j++; } if (a<=2) cout<<i<<" "; i++; } getch();

13. Program that calculate the 10 input numbers average with counter-controlled Repetition using While loop. #include<conio.h> #include<iostream.h> void main() { clrscr(); int i=1, n; float sum=0; while(i<=10) { cout<<"Enter number: "; cin>>n; sum=sum+n; i++; } cout<<"Average of 10 numbers is: "<<sum/i; getch(); } 14. Program that calculate the 10 input numbers average with Sentinel-controlled Repetition using While loop. #include<conio.h> #include<iostream.h> void main() { clrscr(); int i=10, n; float sum=0; while(i!=0) { cout<<"Enter number: "; cin>>n; sum=sum+n; i--; } cout<<"Average of 10 numbers is: "<<sum/10; getch();

16. Program to generate the histogram (bars). It reads five numbers b/w 1 to 30. For each number read print a line containing that number of adjacent asterisks. #include<conio.h> #include<iostream.h> void main() { clrscr(); int i,n; cout<<"You'll ask to input five integers\n"; cout<<"Please enter b/w 1 to 30\n"; for (i=1;i<=5;i++) { cout<<"Enter number"; cin>>n; if (n>=1 && n<=30) { while (n>=1) { cout<<"*"; n--; } } else cout<<"Invalid Entry"; cout<<endl; } getch(); } 17. Program to generate all possible combinations of 1, 2 & 3. #include<conio.h> #include<iostream.h> void main() { clrscr(); int a,b,c; cout<<”All possible combinations are as: “; for (a=1;a<=3;a++) for (b=1;b<=3;b++) for (c=1;c<=3;c++) { cout<<a<<" "<<b<<" "<<c<<endl; } getch();

18. Program to generate the following output, a square of satiric is printed. i.e. if enter 4, output must be. $$$$ $ $ $ $ $$$$ #include<conio.h> #include<iostream.h> void main() { clrscr(); int n, i, j; cout<<"Enter number: "; cin>>n; for (i=1;i<=n;i++) { for (j=1;j<=n;j++) { if (j==1 || j==n || i==1 || i==n) cout<<"$"; else cout<<" "; } cout<<endl; } getch();

20. Program that display the followings. (a) $ $$$ $$$$$ $$$$$$$ $$$$$ $$$ $ #include<conio.h> #include<iostream.h> void main() { clrscr(); int n,i,j,a=1,b=1; for (n=7;n>=1;n--) { if (n>=4) { for (i=1;i<=n-4;i++) { cout<<" "; } for (j=1;j<=a;j++) { cout<<"$"; } a+=2; } else { for (i=1;i<=4-n;i++) { cout<<" "; } for (j=5;j>=b;j--) { cout<<"$"; } b+=2; } cout<<endl; } getch();

(B)

#include<conio.h> #include<iostream.h> void main() { clrscr(); int i,j; for (i=1;i<=6;i++) { for (j=1; j<=i;j++) { cout<<"#"; } cout<<endl; } getch();

(D) $$$$$$$$$$

#include<conio.h> #include<iostream.h> void main() { clrscr(); int a,b; for (a=5;a>=1;a--) { for (b=1;b<=a;b++) { cout<<"$$"; } cout<<endl; } getch();

21. Program that prints the following.

#include<conio.h> #include<iostream.h> void main() { clrscr(); int i,j; for (i=7;i>=1;i--) { if (i>=4) { for (j=1;j<=11-i;j++) cout<<"* "; } else { for (j=3+i;j>=1;j--) cout<<"* "; } cout<<endl; } getch();