-->

input and output programs in c++ (section 1)

Click the particular line to goto that page.
-------------------------------------------------------------

1. program to print hello world(1)

                                                                                solution
2. program to understand endl(2)
                                                                                solution
3. program to understand escape sequence(3)
                                                                                solution
4.program to understand white space(4)
                                                                               solution
5.program to use header file  with double quote(5)
                                                                                solution
6.program to understand input(6)
                                                                                 solution
7.program to know about cin to input data(7)
                                                                                 solution
8.program to understand string input in c++(8)
                                                                                 solution
9.program to print multiple string using single cout(9)
                                                                                 solution
10.program to understand character variable and constant(10)
                                                                                  solution
11.program to understand definition of variables(11)
                                                                                   solution
12.program to print character constatnt(12)
                                                                                   solution
13.program to understand setw() function(13)
                                                                                     solution
14.program to understand library function(14)
                                                                                      solution
15.program to understand type casting (data type conversion)(15)
                                                                                      solution
16. program to understand assignment operator(16)
                                                                                      solution

17.program to understand const identifier(p17)
                                                                                      solution
18.program to understand define directive or symbolic constant(18)
                                                                                       solution
19.program to understand arithmetic remainder operator(19)
                                                                                         solution
20.program to understand unary operator(prefix and postfix)(20)
                                                                                           solution
21. program to generate given table using single cout(21)
                                                                                          solution
22.program to generate following output(22)
                                                                                          solution
23. program to understand relational operator(23)
                                                                                          solution
24.program to understand logical operator(24)
                                                                                           solution
25.program to understand conditional operator(25)
                                                                                            solution
26.program to clear the screen using system(cls)(26)
                                                                                             solution
27.program to add two numbers without using namespace(27)
                                                                                              solution
28.program to print hello world without namespace(28)
                                                                                               solution

WAP to count number of vowels and consonants given in a text.

//WAP to count number of vowels and consonants given in a text.
#include<stdio.h>
include<conio.h>
#include<ctype.h>void main()
{                                                           
char name[30];                                                    
int i,vowel=0,cnst=0;
printf("enter string\n");
gets(name);

for(i=0;name[i]!='\0';i++)
{
if(name[i]==toupper(a)  || name[i]==toupper(e) || name[i]==toupper(i) || name[i]==toupper(o) || name[i]==toupper(u))
{
  vowel++;
}
else
{
  cnst++;
}
}
printf("total vowel present=%d\n",vowel);
printf("total consonant present=%d\n",cnst);
getch();
}
        

WAP to count total number of particular character present in a string.

//WAP to count total number of particular character present in a string.
#include<stdio.h>
include<conio.h>
void main()
{                                                           
char name[30], c;                                                    
int i,count=0;
printf("enter string\n");
gets(name);
printf("enter a character \n");
c=getchar();
for(i=0;name[i]!='\0';i++)
{
if(name[i]==c)
{
  count++;
}
}
printf("total character present=%d\n",count);
getch();
}
        

WAP to know a number /string is Palindrome or not.

// WAP to know a number /string is Palindrome or not.
#include<stdio.h>
include<conio.h>
void main()
{                                                           
char name[30];                                                    
char temp[30];
printf("enter string\n");
gets(name);
strcpy(temp,name);
if((strcmp(strrev(name),temp))==0)
{
  print("it is palindrome\n");
}
else
{
printf("it is not\n");
}
getch();
}
        

WAP to store book's name,price and author's name in a file named 'book.dat' and then read and display on monitor.

//WAP to store book's name,price and author's name in a file named 'book.dat' and then read and display on //monitor.
#include<stdio.h>
include<conio.h>
void main()
{
FILE *k;                                                                    //  pointer for file declaration
char book_name[30],,author[100],choice;                                                    //identifier declaration
int book_price;                                             //       "             "
k=fopen("book.dat","w");
do
{
printf("enter book's name\n");                                       // getting inuts
gets(book_name);
printf("enter book's price\n");
scanf("%d",&book_price);
printf("enter author\n");
scanf("%s",author);

fprintf(k,"%s %d %s",book_name,book_price,author);   //writing data to data file
printf("want to continue y/n\n");
choice=getche();                                                        //getting a character from user  
}while(choice!='n');                                                      // validating the input
printf("writing process completed successfully\n");
fclose(k);                                                                                   //closing data file
printf("now reading the records\n");
k=fopen("book.dat","r");
while((fscanf(k,"%s %d %s",book_name,&book_price,author)!=EOF))//reading data from data file
{
printf("book name=%s , price=%d", and author name=%s",book_name,book_price,author_name); //displaying data
}
fclose(k);
getch();
}

WAp to enter student's name,his/her address and their roll number using struct. Then display them

//WAp to enter student's name,his/her address and their roll number using struct. Then display them
#include<stdio.h>
#include<conio.h>
struct detail
{
int roll;
char name[100];
char address[100];
}var[100];

void main()
{
int i,j,n;
printf("enter total number of records (less or than '100')\n");
scanf("%d",&n);
printf("now enter records of students\n");
for(i=0;i<n;i++)
{
printf("enter student's roll no.\n");
scanf("%d",&var[i].roll);
printf("enter student's name\n");
scanf("%s",var[i].name);
printf("enter student's address\n");
scanf("%s",var[i].address);
}
printf("entered records are=\n");
  for(i=0;i<n;i++)
{
printf("student's roll=%d, name=%s,student's address=%s \n",var[i].roll,var[i].name,var[i].address);
}
getch();
}

WAP to get factorial value of a number.

//WAP to get factorial value of a number.
#include<stdio.h>
include<conio.h>
void main()
{
   int number,fact=1,i;
   printf("enter any positive number\n");
  scanf("%d",&number); 
 for(i=1;i<=number;i++)
    {
     fact=fact*i;
   }
 printf("the factorial value for entered number=%d is =%d\n",number,fact);
getch();
}
-----------------------------------------------------------------------------------
Or
-------------------------------------------------------------------------------
//program to get factorial value of a positive number
#include<stdio.h>
include<conio.h>
void main()
{
   int number,fact=1,i;
   printf("enter any positive number\n");
  scanf("%d",&number);
if(number>0)

 for(i=1;i<=number;i++)
    {
     fact=fact*i;
   }
}
else
{
printf("the number is not +ve\n");
}
 printf("the factorial value for entered number=%d is =%d\n",number,fact);
getch();
}

WAP to input salary of 'n'number of employees then count total number of employees getting salary in different range

//WAP to input salary of 'n'number of employees then count total number of employees getting salary in different //range
/*
-> less than 5000
 > >=5000 and < 10000
 >  >10000
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,count=0,count1=0,count2=0;
in salary[1000];
printf("enter total number of employees below 1000\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter salary\n");
scanf("%d",&salary[i]);
  if(salary[i]<5000)
   {
     count++;
    }
   else if(salary[i]>=5000 && salary[i]<=10000)
              {
                count1++;
              }
   else
     {
          count2++;
      }
}
printf("total emplyoees getting less than 5000=%d\n",count);
printf("total employees getting more or equals to 5000= and less than 10000=%d\n",count1);
printf("total emplyoees getting more than 10000=%d\n",count2);
getch();
}

WAP to display the name of day on the basis of entered number 1 to 7 using switch. For example, 1 for Sunday.

//WAP to display the name of day on the basis of entered number 1 to 7 using switch. For example, 1 //for  Sunday.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int choice;
int number,output;
printf("'we have following menu\n");
printf( "1.for Sunday\n");
printf("2. for Monday\n");
printf("3 for Tuesday\n");
printf("4 for Wednesday\n");
printf("5 for Thrusday\n");
printf("6 for Friday\n");
printf("7 for Saturday\n");
printf("enter your choice 1 or 2 or 3 or 4 or 5 or 6 or 7 or any other number to exit\n");
scanf("%d",&choice);
 switch(choice)
          {
                  case 1:
                            printf("It's Sunday\n");
                            
                break;
               case 2:
                            printf("It's Monday\n");
                 break;

               case 3:
                           printf("IT's Tuesday\n");                            
                 break;
             case 4:
                       printf("It's Wednesday\n");
            break;
          case 5:
                      printf("It's Thrusday\n");
          break;
           case 6:
                     printf("It's Friday\n");
            break;  
            case 7:
                        printf("It's Saturday\n");
           break; 
           default:
                            printf("sorry, not a valid input\n");
                            printf("thank u, terminating....\n");
          }
getch();
}

WAP to find sum,difference, and product of two numbers using switch.

//WAP to find sum,difference, and product of two numbers using switch.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int choice;
int a,b,output;
printf("please enter your two numbers for a and b for operation\n");
scanf("%d%d",&a,&b);
printf("'now,we have following menu\n");
printf( "1.to get sum\n");
printf("2. to get difference\n");
printf("3 to get product\n");
printf(" any other number to exit");
printf("enter your choice 1 or 2 or 3 or any other number\n");
scanf("%d",&choice);
 switch(choice)
          {
                  case 1:
                            printf("we are going to get sum\n");
                           printf("the sum=%d",a+b);
                 break;
               case 2:
                            printf("we are going to get difference \n");
                           printf("the difference=%d",a-b);
                 break;

               case 3:
                            printf("we are going to get product,\n");
                           printf("the product=%d",a*b);
                 break;
             
              default:
                            printf("sorry, not a valid input\n");
                            printf("thank u, terminating....\n");
          }
getch();
}

//program to get following pattern

/*program to get following pattern
                *
                * *
                * *  *
                * *  *  *
                * *  *  *  * 
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
  {
     for(j=1;j<=i;j++)
          {
                printf("* ");
          }
       printf("\n");
 }
getch();
}

Using pointer, write a program to show addition,subtraction and division (arithmetic calculation ).

//Using pointer, write a program to show addition,subtraction and division (arithmetic                    calculation ).
#include<stdio.h>
#include<conio.h>
void main()
{
int *p,*p1;
int number,number1;
printf("enter a numbers\n");
scanf("%d%d",&number,&number1);
p=&number;
p1=&number1;
printf("the sum is=%d\n",*p+*p1);
printf("the difference is=%d\n",*p-*p1);
printf("the quotient is=%d\n",*p/*p1);
getch();
}

Program to input a value/number and display its address with value using pointer.

//WAP to input a value/number and display its address with value using pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
int *p;
int number;
printf("enter a number\n");
scanf("%d",&number);
p=&number;
printf("the address is=%d\n",p);
printf("the value is=%d\n",*p);
getch();
}

program to print 'n' integers and their factorial value.

//to print 'n' integers and their factorial value.
#include<stdio.h>
include<conio.h>
void main()
{
   int number,i,k,fact;
   printf("enter value(upper) for  number 'number'\n");
  scanf("%d",&number);
  for(i=1;i<=number;i++)
    {
        fact=1;
        for(k=1;k<=i;k++)
           {
                    fact=fact*k;
            }
            printf("the factorial value for %d number=%d\n",i,fact);
    }
getch();
}