Uploaded by Learn Easy

coding of c++

advertisement
1.add ASCII value:
#include<iostream>
using namespace std;
int main(){
char x='d'+3;
cout<<x;
return 0;
}
2.ASCII value
#include<iostream>
using namespace std;
int main(){
cout<<"ASCII value of d is"<<(int)'d';
return 0;
}
3.Convert into int
#include<iostream>
using namespace std;
int main(){
double x=100.25;
cout<<(int)x;
return 0;
}
4.GCD
#include <iostream>
using namespace std;
int GCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
int gcd = GCD(num1, num2);
cout << "GCD of " << num1 << " and " << num2 << " is: " << gcd <<endl;
return 0;
}
5.Accessing array:
#include <iostream>
using namespace std;
int main()
{
int a[2][2]={{1,2},{3,4}};
cout<<"1st element:"<<a[0][0];
}
6.Linear array
#include<iostream>
using namespace std;
int main()
{
const int size=5;
int linearArray[size];
for(int i=0;i<size;i++){
linearArray[i]=2*i;
}
cout<<"linear Array";
for(int i=0;i<size;i++){
cout<<linearArray[i]<<"";
cout<<"\n";
}
return 0;
}
7.factorial
#include <iostream>
using namespace std;
int Factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; ++i)
{
fact *= i; // fact= fact*i
}
return fact;
}
int main() {
int num;
cout << "Enter a number to calculate its factorial: ";
cin >> num;
int fact = Factorial(num);
cout << "Factorial of " << num << " is: " << fact << endl;
return 0;
}
7.Fibonacci series
#include <iostream>
using namespace std;
void Fibonacci(int n) {
int first = 0, second = 1, next;
cout << "Fibonacci Series: " << first << " " << second << " ";
for (int i = 2; i < n; ++i) {
next = first + second;
cout << next << " ";
first = second;
second = next;
}
}
int main() {
int n;
cout << "Enter the number of terms for the Fibonacci series: ";
cin >> n;
Fibonacci(n);
return 0;
}
8.leap year
#include <iostream>
using namespace std;
int main() {
int leap;
cout << "Enter an leap year: ";
cin >> leap;
if ( leap % 4 == 0)
cout << leap << " is leap year.";
else
cout << leap << " is not year.";
return 0;
}
9.prime number
#include <iostream>
using namespace std;
int main()
{
int n, i, c = 0;
cout << "Enter any number n: ";
cin>>n;
for (i = 1; i <= n; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 2)
{
cout << "n is a Prime number";
}
else
{
cout << "n is not a Prime number";
}
return 0;
}
10.
#include<iostream>
using namespace std;
int main(){
int x;
char y;
double z;
cout<<"enter int,char and double value:";
cin>>x;
cin>>y;
cin>>z;
cout<<"int is"<<int<<"char is"<<char<<"double is"<<double;
return 0;
}
11. palindrome
#include<iostream>
using namespace std;
int main( )
{
int i, j, len, flag =1;
char a [20];
cout<<"Enter a string:";
cin>>a;
for(len=0;a[len]!='\0';++len)
for(i=0,j=len-1;i<len/2;++i,--j)
{
if(a[j]!=a[i])
flag=0;
}
if(flag==1)
cout<<"\n The String is palindrome";
else
cout<<"\n The String is not palindrome";
return 0;
}
12.supermarket using class
#include<iostream>
using namespace std;
class supermarket{
private:
int cust_id;
int item_no;
string cust_name;
string item_name;
public:
void display(){
cout<<"enter the customer id:";
cin>>cust_id;
cout<<"enter the customer name:";
cin>>cust_name;
cout<<"enter the number of items:";
cin>>item_no;
cout<<"enter the item names:";
cin>>item_name;
}
};
class Payment: public supermarket{
private:
float amount;
char pay;
public:
void payment(){
cout<<"enter the amount to pay:";
cin>>amount;
cout<<"enter your choice to pay:";
cin>>pay;
if(pay){
cout<<"you have paid the amount by cash";
}
else{
cout<<"you have paid the amount by UPI";
}
}
};
int main(){
Payment p;
p.display();
p.payment();
cout<<"visit again";
}
13.suoermarket using switch
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
typedef struct market{
int cust_id;
int item_no;
string cust_name;
string item_name;
float amount;
char pay;
}market;
int main(){
int cust_id;
int item_no;
string cust_name;
string item_name;
float amount;
char pay;
int ch;
cout<<"*****SUPER MARKET****";
for(;;)
{
cout<<"\n *****MENU*****";
cout<<"\n 1.DETAILS";
cout<<"\n 2.BUY PRODUCTS";
cout<<"\n 3.PAYMENTS";
cout<<"\n 4.EXIT";
cout<<"\n enter your choice";
cin>>ch;
switch(ch){
case 1:
cout<<"enter the customer id:";
cin>>cust_id;
cout<<"enter the customer name:";
cin>>cust_name;
break;
case 2:
cout<<"enter the number of items:";
cin>>item_no;
cout<<"enter the item names:";
cin>>item_name;
break;
case 3:
cout<<"enter the amount to pay:";
cin>>amount;
cout<<"enter your choice to pay:";
cin>>pay;
if(pay)
{
cout<<"you have paid the amount by cash";
}
else
{
cout<<"you have paid the amount by UPI";
}
case 4:
cout<<"thank you visit again";
}
}
return 0;
}
14.swap
#include <iostream>
using namespace std;
int main()
{
int temp,a,b;
a=10;
b=20;
temp=a;
a=b;
b=temp;
cout<<"before swapping";
cout<<"\n a="<<a;
cout<<"\n b="<<b;
cout<<"\n after swapping";
cout<<"\n a="<<b;
cout<<"\n b="<<a;
}
15.reverse a number
#include <iostream>
using namespace std;
int main() {
int n, reversed_number = 0, remainder;
cout << "Enter an integer: ";
cin >> n;
while(n != 0) {
remainder = n % 10;
reversed_number = reversed_number * 10 + remainder;
n /= 10;
}
cout << "Reversed Number = " << reversed_number;
return 0;
}
16.reverse an array
#include <iostream>
#include <conio.h>
using namespace std ;
int largest ( int arr [ ] , int n )
{
int i ;
int max = arr [ 0 ] ;
for ( i = 1 ; i < n ; i++){
if ( arr [ i ] > max )
max = arr [ i ] ;
}
return max ;
}
int main ( )
{
int arr [ ] = { 10 , 324 , 45 , 90 , 9808 } ;
int n = sizeof ( arr ) / sizeof (arr [ 0 ] ) ;
cout << " Largest in given array is " << largest ( arr , n ) ;
return 0 ;
}
17. ASCII equivalent
#include<iostream>
using namespace std;
int main(){
char c;
for(int i=0;i<=255;i++){
c=i;
cout<<c<<"\n";
}
return 0;
}
18. print a to z
#include<iostream>
using namespace std;
int main(){
int i;
for( i='A';i<='Z';i++){
cout<<(char)i<<"\n"<<"ASCII:"<<i<<"\n";
}
return 0;
}
19. multiply without *
#include <iostream>
int main()
{
long op1, op2, product = 0;
std::cout << "Enter the numbers ";
std::cin >> op1 >> op2;
while(op2 > 0)
{
product = product + op1;
op2--;
}
std::cout << std::endl << "The product of " << op1
<< " and " << op2 << " is " << product;
}
20.goto program
#include <iostream>
using namespace std;
int main () {
int i = 0;
cout << "Printing 1 to 10 without using loop\n";
repeat:
cout << ++i << " ";
if(i != 10)
goto repeat;
cout << "\n";
}
21. electricity bill
#include <stdio.h>
// 1 to 50 units - 30Rs/unit
// 50 to 100 units - 35Rs/unit
// 100 to 150 units - 40Rs/unit
// 150 units and above - 50Rs/unit
int electricity_bill(int units)
{
if (units <= 50)
{
return units * 30;
}
else if (units <= 100)
{
return (50 * 30) + (units - 50) * 35;
}
else if (units <= 150)
{
return (50 * 30) + (50 * 35) + (units - 100) * 40;
}
else
{
return (50 * 30) + (50 * 35) + (50 * 40) + (units - 150) * 50;
}
}
int main()
{
int units;
printf("Enter the units: ");
scanf("%d", &units);
printf("The electricity bill is: %d\n", electricity_bill(units));
return 0;
}
22.pseudo
#include<stdio.h>
#define pseudo main
int pseudo(void){
printf("hello world");
return 0;
}
23.statement without ;
#include <stdio.h>
void main()
{
if(printf("good"))
{
}
}
24. time( 12 hrs to 24 hrs)
#include<stdio.h>
#include<string.h>
int main()
{
int hh, mm, ss;
char a[3];
printf("Enter hours 'hh' \t");
scanf("%d", &hh);
printf("Enter minutes 'mm' \t");
scanf("%d", &mm);
printf("Enter seconds 'ss' \t");
scanf("%d", &ss);
printf("Enter string 'am' or 'pm' \t");
scanf("%s", &a);
/*
* user is allowed to enter time only in 12-hour format
* so that 'hh' cannot be greater than 12.
*/
if(hh <= 12 && mm <= 59 && ss <= 59)
{
if((strcmp(a,"PM") == 0) || (strcmp(a,"pm") == 0)
&& (hh != 0) && (hh != 12))
{
hh = hh + 12;
}
if((strcmp(a,"AM") == 0) || (strcmp(a,"am") == 0) && (hh == 12))
{
hh = 0;
}
printf("The obtained 24-hour format of input is \t");
printf("%02d:%02d:%02d", hh, mm, ss);
printf("\n\n");
}
else
{
printf("Provide the correct inputs.");
}
return 0;
}
25. string concatenate
#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
char str1[50], str2[50];
cout << "Enter string 1 : ";
gets(str1);
cout << "Enter string 2 : ";
gets(str2);
strcat(str1, str2);
cout << "Concatenated String : " << str1;
return 0;
}
26. string copy
#include<iostream>
using namespace std;
int main ()
{
char str[50], cpy[50];
int n;
cout << "Enter a string : ";
gets(str);
cout << "Enter number of characters to be copied : ";
cin >> n;
strncpy(cpy, str, n);
cout << "Copied string : " << cpy;
return 0;
}
27. alphabet or not
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
char c;
cout << "Enter the character : ";
cin >> c;
if (!isalpha(c))
cout << c << " is not an alphabetical character." << endl;
else
{
int case_val;
if (c >= 'a' && c <= 'z')
{
c = c - 'a' + 'A';
case_val = 1;
}
else if (c >= 'A' || c <= 'Z')
{
c = c + 'a' - 'A';
case_val = 0;
}
cout << c << " is the " << ( (case_val == 1) ? "upper" : "lower" )
<< " case of given character " << endl;
}
}
28. REVERSE STRING:
#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str[50], temp;
int i, j;
cout << "Enter a string : ";
gets(str);
j = strlen(str) - 1;
for (i = 0; i < j; i++,j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
cout << "\nReverse string : " << str;
return 0;
}
29.Resultant string
#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
int i;
char str[50];
cout << "Enter string : ";
gets(str);
for (i = 0; str[i] !='\0'; i++)
{
if (str[i] == ' ')
str[i]='%';
}
cout << "Resultant string : " << str;
return 0;
}
Download