C Loop programs while loop includeiostream h class

  • Slides: 7
Download presentation
C++ Loop programs • //while loop • #include<iostream. h> • class sample { •

C++ Loop programs • //while loop • #include<iostream. h> • class sample { • private: • int n; • public: • void nos(int limit) • { n=limit; • int i=1; // initial value of loop counter • while(i<=n)// entry control loop • { • cout<<" "<<i; //body of loop • i++; //increment counter • }; • void main(){ • sample obj; • chat x; • cout<<"Enter LImit : - "; • cin>>x; • obj. nos(x); • } } Ajith G. S: poposir. orgfree. com

C++ Loop programs • • • • • //do while loop print even nos

C++ Loop programs • • • • • //do while loop print even nos • }; class sample { • void main(){ private: • sample obj; int n; • chat x; void even_nos(int limit) • cout<<"Enter LImit : - "; { • cin>>x; • obj. even_nos(x); • } #include<iostream. h> public: n=limit; int i=1; // initial value of loop counter do { if(i%2==0) cout<<" "<<i; //body of loop i++; //increment counter }while(i<=n)// exit control loop } Ajith G. S: poposir. orgfree. com

C++ Loop programs } • //for while loop to print factors of a no

C++ Loop programs } • //for while loop to print factors of a no • • #include<iostream. h> • }; • class sample { • void main(){ • private: • sample obj; • int n; • chat x; • public: • cout<<"Enter a Nos : - "; • void factors(int limit) • cin>>x; • { • obj. factors(x); • } • n=limit; • for(int i=1; i<=n; i++) • { • if(i%2==0) • cout<<" "<<i; • } Ajith G. S: poposir. orgfree. com

C++ Loop programs • //for loop to check prime or not • If(c==2) cout<<“prime”;

C++ Loop programs • //for loop to check prime or not • If(c==2) cout<<“prime”; else cout<<“not prime”; • #include<iostream. h> • } • class sample { • }; • private: • void main(){ • int n, c; • sample obj; • public: • chat x; • void prime(int limit) • cout<<"Enter a Nos : - "; • { c=0; • cin>>x; • obj. prime(x); • } • n=limit; • for(int i=1; i<=n; i++) • { • if(n%i==0) • c++; ; • } Ajith G. S: poposir. orgfree. com

C++ Loop programs • //for loop to print prime nus • If(c==2) cout<<“ “<<I;

C++ Loop programs • //for loop to print prime nus • If(c==2) cout<<“ “<<I; • #include<iostream. h> • } • class prime { • }; • private: • void main(){ • int n, c; • public: • prime obj; • void check_prime(int limit) • {n=limit; • for (int i=1; i<=n; i++) { • c=0; • for(int j=1; j<=i; j++) • { • if(i%j==0) • c++; ; • • chat x; • cout<<"Enter limit : - "; • cin>>x; • obj. check_prime(x); • } } • Ajith G. S: poposir. orgfree. com

C++ Loop programs } • //prog to reverse a no • • #include<iostream. h>

C++ Loop programs } • //prog to reverse a no • • #include<iostream. h> • }; • class rev { • void main(){ • private: • rev obj; • int n, r, b; • chat x; • public: • cout<<"Enter no : - "; • void num(int limit) • { • cin>>x; • obj. num(x); • } • n=limit; r=0; • for(i=n; i>0; i=i/10){ • b=i%10; • r=r 10+b; • } • cout<<“Freverse = “<<r; • Ajith G. S: poposir. orgfree. com

C++ Loop programs } • //prog to check palindrome or not • • #include<iostream.

C++ Loop programs } • //prog to check palindrome or not • • #include<iostream. h> • }; • class rev { • void main(){ • private: • rev obj; • int n, r, b; • public: • chat x; • void num(int limit) • cout<<"Enter no : - "; • { • cin>>x; • obj. num(x); • } • n=limit; r=0; • for(i=n; i>0; i=i/10){ • b=i%10; • r=r 10+b; • } • if(r==n) cout<<“no is palindrome”; • else cout<<“not palindrome”; • Ajith G. S: poposir. orgfree. com