Showing posts with label C. Show all posts
Showing posts with label C. Show all posts
Friday, December 9, 2016
Tuesday, November 29, 2016
Placement Practice Set - Logical Small Questions
Prime Number:
A prime
number that doesn’t have factors. Like 2
3 5 7 11 13 17……
Just see the
logic how to check a given number is prime or not
// number is
a variable to be checked
// count is
a integer variable
// flag is
used to check condition initially should be 0
for(count=2; count<=number/2; count++)
{
if( number % count = = 0)
{
flag = 1;
break;
}
}
if (flag = = 0)
printf("prime number");
else
printf("not a prime number");
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Factorial of
a given number
A. Using loop
int main()
{
int count , fact = 1,num;
printf("Enter Number:
");
scanf("%d",&num);
for(count = 1; count < = num; count++)
{
fact = fact * count;
}
printf("Factorial of %d is:
%d",num,fact);
return 0;
}
B. Using Recursion -
Recursion is function which calls itself ,
Calling itself
stops at a particular condition.
int
fact(int n); // Function Declaration
int main()
{
int num, result;
printf("Enter num: ");
scanf("%d", &num);
result
= fact(num); // Function Calling
printf("Factorial of %d is %d", num,
result);
return 0;
}
int fact(int n) // Function Definition
{
if (n <= 0)
{
return 1;
}
else
{
return (n * fact(n - 1));
}
}
Saturday, April 9, 2016
C++ - Bubble Sort Using Pointer
"Jai Saraswati Maa"
Hi ... Dear All ..Today I am uploading a superb example of Bubble Sort using pointer.
In this example we define our own function to sort the Array.
Special Thanks to Aditya Abhishek (Student)
Please check.
Please Use CodeBlock
A. Code is **************
Hi ... Dear All ..Today I am uploading a superb example of Bubble Sort using pointer.
In this example we define our own function to sort the Array.
Special Thanks to Aditya Abhishek (Student)
Please check.
Please Use CodeBlock
A. Code is **************
using namespace std;
void bubble(int *ptr,int s)
{
inti,j;
int temp;
for(i=0;i<s;i++)
{
for(j=0;j<s-i;j++)
{
if(*(ptr+j)>*(ptr+j+1))
{
temp=*(ptr+j);
*(ptr+j)=*(ptr+j+1);
*(ptr+j+1)=temp;
}
-
}
}
}
int main()
{
intarr[10];
inti;
int size;
cout<<"Enter Size OF An Array : ";
cin>>size;
cout<<"Enter The Element OF Array :";
for(i=0;i<size;i++)
cin>>arr[i];
cout<<"Before Sorting :"<<endl;
for(i=0;i<size;i++)
cout<<" "<<arr[i];
bubble(arr,size);
cout<<"After Sorting :";
for(i=0;i<size;i++)
cout<<" "<<arr[i];
return 0;
}
Wednesday, April 6, 2016
C++ - Overloading ++ operator for date change
"Jai Saraswati Maa"
Hi ... Dear All ..Today I am uploading a superb example of operator overloading to check next date.
In this example we use Member function to overload ++ operator.
Special Thanks to Kanika Bhargava (Student)
Please check.
A. Code is **************
#include<conio.h>
#include<iostream.h>
class date
{
int d,m,y;
public:
void read()
{
cout<<"enter date:"<<endl; cin>>d;
cout<<"enter month:"<<endl; cin>>m;
cout<<"enter year:"<<endl; cin>>y;
}
void show()
{
cout<<"next date is:"<<d<<"/"<<m<<"/"<<y<<endl;
}
int operator++();
};
int date::operator++()
{
if(m>12)
{ cout<<"INVALID DATE!!"<<endl<<"ENTER DATE AGAIN"<<endl;
read();
}
else if(d==31&&m==12) {d=1;m=1;y++;}
else if(m==2)
{
if((y%4==0&&d==29)||(y%4!=0&&d==28))
{ d=1;m=3; }
else if((y%4==0)&&(d>29))
{cout<<"INVALID DATE!!"<<endl<<"ENTER DATE AGAIN"<<endl;
read();}
else if((y%4!=0)&&(d>28))
{cout<<"INVALID DATE!!"<<endl<<"ENTER DATE AGAIN"<<endl;
read(); }
else ++d;
}
else if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
{
if(d==31){++m; d=1;}
else if(d>31)
{ cout<<"INVALID DATE!!"<<endl<<"ENTER DATE AGAIN"<<endl;
read();
}
else ++d;
}
else if(m==4||m==6||m==9||m==11)
{
if(d==30){++m; d=1;}
else if(d>30)
{cout<<"INVALID DATE!!"<<endl<<"ENTER DATE AGAIN"<<endl;
read();
}
else ++d;
}
}
int main()
{
clrscr();
date d1;
d1.read();
++d1;
d1.show();
getch();
}
Subscribe to:
Posts (Atom)