While & do while loop in C++

 While in CPP

.while loop statement repeatedly executes a target statement as long as a given condition is trueThis is also a loop Structure , but is an enter-controlled one 

Syntax:

while( condition is true)
{
 action1;
}
action 2;

Program :
Write a program in CPP to count the number of alphabets ,digit and special Symbol             @#$%&)using while loop. 

#include<iostream>
using namespace std;
int main()
{
        char  str[100]; 
        int i=0 ,digit=0,alphabets=0,special=0,characters=0 ,count=0;

cout<<"Enter the string :";
gets(str);
cout<<"Enter string is "<<str<<"\n";
while(str[i]!='\0')
{
count++;
i++;
}
i=0;
while(str[i]!='\0')
{ if(str[i]>='0' && str[i]<='9')
{
digit++;
}
if(str[i]=='@' ||str[i]=='#' ||str[i]=='$' ||str[i]=='%'||str[i]=='^'||str[i]=='&')
{
special++;
}
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A' &&str[i]<='Z'))
{
alphabets++;
}
i++;
}
cout<<"The number of alphabets is:"<<alphabets;
cout<<"\nThe number os special number is :"<<special;
cout<<"\n he nuber of didgt is:"<<digit;
return 0;
}



OUTPUT:

Enter the string :rajanshukla48472@gmail
Enter string is rajanshukla48472@gmail
The number of alphabets is:16
The number os special number is :1
 he nuber of didgt is:5
--------------------------------

Program2://Write a program in C++ to find whether a given number is palindrome or not. using                         while loop.

Code:

#include<iostream>
using namespace std;
int main()
{
//Write a program to find whether a given number is palindrome or not.

int rev,temp,num,digit=0;
cout<<"Enter a number";
cin>>num;
temp=num;
while(num!=0)
{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
if(rev==temp)
{
cout<<"The given  number is palindrome ";
}
else 
cout<<"The given  number is  not  palindrome ";

return 0;
}
OUTPUT:
Enter a number123454321
The given  number is palindrome
--------------------------------


do-While loop in CPP:

using do-while we  can run code at least ones if condition is wrong .Do while loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. 

Syntax:

do
{
action1;
}while(condtion)
action2;

Program: Write a program in CPP to print "hello " at least  ones time using do-while loop.

Code:
#include<iostream>
using namespace std;
int main()
{
int i=0;
do{
cout<<"Hello";
i++;
}while(i>5);
return 0;
}

OUTPUT:
Hello
--------------------------------

Here given condtion is wrong still  hello print.

Program: Write a program in C++ to print the Fibonacci series of N using do-while loop.

Code:#include<iostream>

using namespace std;
int main()
{
int i=1,num,num1=0,num2=1,num3;
cout<<"Enter the number for fibonacci series ";
cin>>num;
cout<<num1<<"\n";
cout<<num2;
do{
num3=num1+num2;
cout<<"\n"<<num3;
num1=num2;
num2=num3;
i++;
}while(i<num);
return 0;
}

OUTPUT:
Enter the number for fibonacci series 10
0
1
1
2
3
5
8
13
21
34
55
--------------------------------For loop in C++



Comments

Popular posts from this blog

CYBER SECURITY & CRACKING PASSWORDS

"The Dark Side of Social Media: How It Fuels Digital Addiction and Impacts Mental Health"

Online Platforms to Learn Coding