Pages

Write a program in C++ to get employee name and its salary and print employee name, salary and grand salary(total salary+10% of salary) [Updated]

Monday 22 April 2013


#include <iostream.h>
class employee
{
     char firstName[50];
     char lastName[50];
     int salary;
     public:
            employee()
            {
                strcpy(firstName, "NULL");
                strcpy(lastName, "NULL");
                salary=0;    
            }
            void setfn()
            {
                 cout<<"Enter First Name of Emplyee: ";
                 cin>>firstName;
            }
            void setln()
            {
                 char ln;
                 cout<<"Enter Last Name of Emplyee: ";
                 cin>>lastName;
            }
            void sets()
            {
                 int s;
                 cout<<"Enter salary of "<<firstName<<" "<<lastName<<": ";
                 cin>>s;
                 if(s<0){s=0;}
                 salary=s;
            }
            void dfn()
            {
                 cout<<"First Name is: "<<firstName<<endl;
            }
            void dln()
            {
                 cout<<"Last Name is: "<<lastName<<endl;
            }
            void dsalary()
            {
                 cout<<"Salary of "<<firstName<<" "<<lastName<<" of one year is: "<<salary*12<<endl;
                 cout<<"Salary with 10% of "<<firstName<<" "<<lastName<<" is: "<<((salary*10)/100)+salary<<endl;
            }
};
int main()
{
    char a;
    employee e1;
    again:
    e1.setfn();
    e1.setln();
    e1.sets();
    e1.dfn();
    e1.dln();
    e1.dsalary();
    cout<<"Would you like to put data of new employee: ";
    cin>>a;
    if(a=='y' || a=='Y'){goto again;}
}

Write a program to in C++ to get student name and age and print it on screen using OOP technique.


#include<iostream>
using namespace std;
class student{
char name;
int age;
public:
void setname(char newname,int newage)
{
name=newname;
age=newage;
}
void display()
{
    cout<<"your name is "<<name<<"\nYour age is "<<age<<endl;
}
};
int main()
{
student data;
data.setname('ammar',12);
data.display();
system("pause");
}

Write a program to print bill in C++ using oop


#include <iostream.h>
class bill
{
    char name[50];
    int unit, p_unit, c_unit;
    float price_unit, tbill, tax;
    public:
           bill()
           {
                price_unit=3.80;
           }
    void setname()
    {
       cout<<"Enter customar name: ";
       gets(name);
    }
    void setunits()
    {
        again:
        cout<<"Enter previous units: ";
        cin>>p_unit;
        cout<<"Enter current units: ";
        cin>>c_unit;
        if(c_unit<p_unit)
        {
             cout<<"Enter Valid data(Units)."<<endl;
             goto again;          
        }
        unit=c_unit-p_unit;
    }
    void make_bill()
    {
         float temp;
        if(unit<=100)
        {
                  temp=price_unit*unit;
                  tax=(temp*8)/100;
                  tbill=temp+35+tax;
        }
        else if(unit>100 && unit<=200)
        {
             temp=unit*(price_unit+3);
             tax=(temp*12)/100;
             tbill=temp+35+tax;
        }
        else
        {
            temp=unit*(price_unit+6);
             tax=(temp*14)/100;
             tbill=temp+35+tax;
        }
    }
    void display_bill()
    {
         int counter=0, i;
         for(i=1; i<=4; i++)
         {
                  cout<<"***********************************"<<endl;
                  counter++;
                  if(counter==2)
                  {
                                cout<<"Name: "<<name<<endl;
                                cout<<"Your total units are: "<<unit<<endl;
                                cout<<"You total bill is: R.s "<<tbill<<endl;
                  }
         }
    }
    void detail()
    {
         if(unit<=100){price_unit=3.80;}
         else if(unit>100 && unit<=200){price_unit=6.80;}
         else{price_unit=9.80;}
         cout<<"Total Units consumed: "<<unit<<endl;
         cout<<"PTV fee: R.s 35"<<endl;
         cout<<"Price Per unit at "<<unit<<" units is"<<price_unit<<endl;
         cout<<"Tax: "<<tax<<endl;
       
    }
};
main()
{
      char a, d;
      bill c;
      next:
      c.setname();
      c.setunits();
      c.make_bill();
      c.display_bill();
   
      cout<<"Press \"D\" or\"d\" to get detail of you bill"<<endl;
         cin>>d;
         if(d=='D' ||d=='d')
         {
                   c.detail();
         }
      cout<<"You want to calculate bill of next customar? Y/N ";
      cin>>a;
      if(a=='y' ||a=='Y')
      {
                cout<<"***********************************"<<endl;
                goto next;
      }
}

Write a program to get first n prime numbers

#include <stdio.h>

#include <conio.h>
int main()
{
       int a, b, counter=0, limit=0, num;
       printf("Enter number: ");
       scanf("%d", &num);
       for(a=1; a<=100000; a++)
       {
                counter=0;
                for(b=2; b<a; b++)
                {
                         if ((a%b)==0)
                         {
                            counter++;
                         }  
                }
                if(counter==0)
                {
                              printf("%d \n", a);
                              limit++;          
                }
             if(limit==num)
             {
                       break;      
             }    
       }
       getch();
}

Write a program to print hello world on screen in c language

#include <stdio.h> // header file whicj contain printf(); funcion.

#include <conio.h> // header file which contain getch() funcion
int main() // start body of program.
{
       printf("Hello World! This is my first program in C.");  //prompt
       getch(); //function which is mostly use to hold screen while you typing a charachtar on screen
}

Write a program to get a number from user and print on screen in c language

#include <stdio.h> // header file which contain printf(); funcion.

#include <conio.h> // header file which contain getch() funcion
int main() // start body of program.
{
    char num; // declaration of variable which is use to store data during the execution of program
       printf("Enter a number: ");  //prompt
       scanf("%d", &num); // scanf is used to get input from user.
       printf("your have entered %d", num); // %d is formate specifier use to tell about data that is character or number and num is variable name which store our number
       getch(); //function which is mostly use to hold screen while you typing a charachtar on screen
}

Write a program to print your name, car name, and its number through structure in c

#include <stdio.h>
#include <conio.h>
struct cardata{
      char *ownername;
      char *carname;
      int regno;
      }
      info1, info2, info3;
   
    int main()
      {
          struct cardata;
       
          info1.ownername="ammar";
          info2.carname="suzuki";
          info3.regno=1233;
       
          printf("car data is ownername is %s \ncarname is %s \nregno is  %d \n", info1.ownername, info2.carname, info3.regno);
          getch();
          }

Fundamentals of C Language Tutorial#1


History of C Language
C is a general perpus programing language. It is evolved from two previous languages B and BCPL. In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of The C Programming Language by Kernighan & Ritchie caused a revolution in the computing world. In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or "ANSI C", was completed late 1988.
Why C Still Useful?
1:.C provides:


  • Flexibility and power
  • many high-level and low-level operations Ã  middle level
  • Efficiency, high performance and high quality s/ws.
  • Stability and small size code
  • Provide functionality through rich set of function libraries
  • Gateway for other professional languages like C Ã  C++ Ã  Java.

2:.C is used:
  • System software Compilers, Editors, embedded systems
  • data compression, graphics and computational geometry, utility programs
  • databases, operating systems, device drivers, system level routines
  • there are zillions of lines of C legacy code
  • Also used in application programs

Development With C
Four stages
§Editing: Writing the source code by using some IDE or editor
§Preprocessing or libraries: Already available routines   
§compilingtranslates or converts source to object code for a specific platform     source code -> object code
§linking:    resolves external references and produces the executable module


Features of C language
HardWare Independent: It means that we can create applications/programs without knowng about hardware/processor.
Portable Programing: It means a program which we make can be run at everycomputer. A program written in low level language cannot be run in any computer.
High Level Language: C is a High level language. High level languagea are those languages which are near to human language but far tomachine language. Some other examples of high level languages are given: C++, Java, Cobol, LISP, Perl etc.
Case Senstive: C language is case senstive (e,g print is different from PRINT).
Builtin Functions: C language provides many builtin function. (Function that are provided by language developer is called builtin functions).

Different steps to make a simple program in c language
Edit: Write program in editor in a specific language.
Pre-processor: pre-processor directeries has builtin functions.
Compile: Convert source program into machine lannguage.
Link: Link other files to the programs(e,g header files).
Load: Load programs in the memory RAM.
Execute: Run Program. Execute program.

Some Editor for C language 
  1. Turbo C
  2. Mocrosoft C
  3. Dev CPP
  4. Boreland
  5. Cfree

A Simple Program in C

 #include <stdio.h>
void main()
{
           printf("Hello World!");
}

Different parts of this program:
#include <stdio.h> This is method to include header file in our program. Here we include stdio.h header file which contain function like printf, scanf and others.
void main() is a function which is used to start our program. Execution of program is always start through main. and Void is data type of function main. We learn about data types in next tutorials.
start body of main function.
printf("Hello World!"); It is statement that prints Hello World! on the screan we can write any thing in (" ") that is print on the screan as it is.
end of main body.

An easy code to get reverse of given number in c Language

Saturday 20 April 2013


#include <stdio.h>
#include <conio.h>
int main()
{
          int num,mod,rem;
          printf("Enter the Number to Find it's Reverse\n");
          scanf("%d",&num);
          while(num!=0)
          {
                     mod=num%10;
                     rem=num/10;
                     printf("%d",mod);
                     num=rem;
          }
          getch();
}

Write a program to Get Reverse number of Given Number

#include<stdio.h>

#include <conio.h>
int main(){
    int num,r,reverse=0;

    printf("Enter any number: ");
    scanf("%d",&num);

    while(num){
         r=num%10;
         reverse=reverse*10+r;
         num=num/10;
    }

    printf("Reversed of number: %d",reverse);
    getch();
}

Write a program to pass a string to function that display the string on the screen

#include <stdio.h>

#include <conio.h>
#include <string.h>
char* display(char *string)
{
     int i=0;
     char ch;
     printf("%s", string);
}
main()
{
      char str[]="I Like C!";
      display(str);
      getch();
}

Write a program to pass a string to function that display the string on the screen

#include <stdio.h>

#include <conio.h>
#include <string.h>
char* display(char *string)
{
     int i=0;
     char ch;
     printf("%s", string);
}
main()
{
      char str[]="I Like C!";
      display(str);
      getch();
}

Write a program calculate tomorrow's date.

Friday 19 April 2013


#include <stdio.h>
#include <conio.h>
struct date
{
       int day;
       int month;
       int year;
 
} today, tomorrow;

int main()
{
    char again;
    again:
     struct date;
     printf("Enter today's date: ");
     scanf("%d", &today.day);
     printf("Enter month: ");
     scanf("%d", &today.month);
     printf("Enter year:  ");
     scanf("%d", &today.year);

     switch(today.month)
     {
              case 1:
                   if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else
                   printf("Enter a valid date, month and year");              
              break;
              case 2:
                                      if(today.day==28)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<28 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                else
                   printf("Enter a valid date, month and year");
              break;
              case 3:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else
                   printf("Enter a valid date, month and year");
              break;
              case 4:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                     else
                   printf("Enter a valid date, month and year");
              break;
              case 5:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                               
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 6:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 7:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 8:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 9:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 10:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                  else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 11:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                    else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 12:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=1;
                                             tomorrow.day=1;
                                             tomorrow.year=today.year+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, tomorrow.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
         
              default:
                      printf("Enter a valid date, month and year");              
                   
     }
     printf("\nWould you like to input again? y/n ");
     again=getche();
     printf("\n");
     if(again=='y' || again=='Y')
     {
                   goto again;        
     }

     getch();
}

Print Counting From 1 to 100 without Loop

#include <stdio.h>

#include <conio.h>
int counting(int num);
int main()
{
    int num = 1;

    counting(num);
       getch();
}
int counting(int num){
    if(num<=100){
         printf("%d ",num);
         counting(num+1); // this is main thing which execute loop again and again while num is less than 100.
    }
}

The Expression getchar()!=EOF is 0 or 1.

#include <stdio.h>

#include <conio.h>
main()
{
      printf("%d", getchar()!=EOF);
      getch();  
}

NOTE:
It always print 1 when i enter any key axcept ctrl+z. It is a boolean expression so that its value is 0 or 1.

Programe to print the value of EOF

#include <stdio.h>

#include <conio.h>
main()
{
      printf("%d", EOF);
      getch();  
}

Weather Temperature Conversion Calculator with heading

#include <stdio.h>

#include <conio.h>
main()
{
      int farh, cels;
      int lower=0, upper=300, step=20;
      farh=lower;
      printf("\"Weather Temperature Conversion Calculator\"\n\n");
      printf("Fahrenhiet\tCelsius\n");
      while(farh<=upper)
      {
                        cels=5*(farh-32)/9;
                        printf("%d\t\t%d\n", farh, cels);
                        farh=farh+step;                
      }
      getch();
}

Convert temprature from Farnhieght to CELSIES and Celcies to farnhiegh

#include <stdio.h>

#include <conio.h>

main()
{
      char scale;
      int temp=0;
      float ans;
      printf("Enter F to convert Farnhieght to CELSIES.\nEnter C to convert Celcies to farnhiegh.");
      scale=getche();
      printf("\n");
      if(scale=='F' || scale=='f')
      {
                    printf("Enter temperature in farnhieght: ");
                    scanf("%d", &temp);
                    ans=(temp-32)*5/9;
                    printf("%dF celsies is = %fC \n", temp, ans);                    
      }
      else
      {
                         printf("Enter temperature in celsies: ");
                    scanf("%d", &temp);
                    ans=temp*(9/5)+32;
                 
                    printf("%dC celsies is = %fF",temp, ans);
                    }
 getch();  
}

Write a program to get data from user and write into a file in c language.

#include <stdio.h>

#include <conio.h>
int main()
{
         again:
         FILE *fp;
         fp=fopen("g.txt", "a+");
         char a;
         a=getche();    
         while(a!='.')
         {
                      fputc(a, fp);
                       a=getche();        
                 
         }
         fprintf(fp, "\n");
         printf("\nPrograme is ended, would you like to input again? y/n: ");
         a=getch();
         if(a == 'y' || a == 'Y')
         {
              printf("\n");
                goto again;  
         }
}

Write a program to read data from a file.


#include <stdio.h>
#include <conio.h>
int main()
{
    FILE *fp;
    char a;
    fp=fopen("file_name.txt", "r");
    while(a!=EOF)
    {
                  a=fgetc(fp);
                  printf("%c", a);          
             
    }

    getch();
}

Get two integers and operator from user and apply given operator on given numbers and display answer

#include<stdio.h>

#include<conio.h>
int cal(int,char,int);
int main()
{
int n1, n2;
char sign;
printf("enter 1st value & 2nd value\n");
printf("and press enter\n");
scanf("%d %d", &n1, &n2);
printf("entetr operator +, -, x, /, %\n");
sign = getch();
cal(n1, sign, n2);

getch();
}
int cal(int x,char aop, int y)
{
switch(aop)
{
case '+':
return printf(" the sum is %d\n",x+y);
break;

case '-':
return printf(" the subtract is %d\n",x-y);
break;

case '*':
return printf("the muliply is %d\n",x*y);
break;

case '/':
return printf("the division is %d\n",x/y);
break;

case '%':
return printf("the reamainder is %d\n",x%y);
break;
default:
return printf("invalid input");
}}

Get a Number from user and print its table using function

#include<stdio.h>

#include<conio.h>
int table(int ta, int b);
int main()
{

    int number,a;
    printf("Enter a number: ");
    scanf("%d",&number);
    for(a=1;a<=10;a++){
    printf("%d * %d = %d \n", number, a, table(a, number));
    } //function call
    getch();
}
int table(int ta, int b)
{
    //int a;
    //for(a=1;a<=10;a++)
    return b*ta;
}

Get Maximum number from given numbers using function

#include <stdio.h>

#include <conio.h>
// Define of function
int max_return(int a, int b, int c, int d, int e)
{
int max;

max = a;

if(max < b) max = b;
if(max < c) max = c;
if(max < d) max = d;
if(max < e) max = e;

return (max);

}

int main(void)
{
int a[5];
int max, i;


for(i=1; i<=5; i++)
{
         printf("Enter number %d :", i);
         scanf("%d", &a[i]);
}
// Recall function
max = max_return(a[0], a[1], a[2], a[3], a[4]);
printf("Maximum = %d\n", max);


getch();
}

Get Element in 2 D Array and print orignal metrix and its transpose

#include<stdio.h>
#include<conio.h>
int main()
{
int a[4][4],c,r;
for(r=1;r<4;r++)
{
for(c=1;c<4;c++)
{
printf("Enter %d Row adn %d column: ",r,c);
scanf("%d",&a[r][c]);
}// inner loop clossed
} // outer loop clossed
printf("Before Transpose\n\n");
for(r=1;r<4;r++)
{
for(c=1;c<4;c++)
{
printf("%d",a[r][c]);
printf("\t");
} // inner loop clossed
printf("\n");
} // outer loop clossed
printf("\n\nAfter Transpose\n\n");
for(r=1;r<4;r++)
{
for(c=1;c<4;c++)
{
printf("%d",a[c][r]);
printf("\t");
} // inner loop clossed
printf("\n");
} // outer loop clossed

getch();
}

Output
Before transpose: 
2            3            4
1            8            0
3            6            9

After transpose:
2            1            3
3            8            6
4            0            9


Get a string from user and convert it into upper case

#include <stdio.h>

#include <conio.h>
char* upper(char *word);
int main()
{
    char word[100];
    printf("Enter a string: ");
    gets(word);
    printf("\nThe uppercase equivalent is: %s\n",upper(word));
    getch();
}

char* upper(char *word)
{
    int i;
    for (i=0;i<strlen(word);i++) word[i]=(word[i]>96&&word[i]<123)?word[i]-32:word[i];
    return word;

}

Lenght of string by evaluating the element in the character array one by one. And use of strlen() function


#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
      int i;
      char ch;
      char str[]="This is my 1st assignment of 2nd smester.";
      i=0;
      while(ch!='.')
      {
                    ch=str[i];
                    i++;          
      }
      printf("size of array by eveluating: %d\n", i);
      printf("size of array by Function: %d\n", strlen(str));
      getch();
}

Write a function to raise a floating point number to an integer power. [Updated]

#include <stdio.h>

#include <conio.h>
float raise_to_power(float num, int power)
{
      float ans=1, i;
        for(i=1; i<=power; i++)
        {
                 ans=ans*num;
        }
        return ans;
}
main()
{
      int power=0;
      float num=0;
      printf("Enter a number: ");
      scanf("%f", &num);
      printf("Enter Power: ");
      scanf("%d", &power);
      printf("Answer is: %f", raise_to_power(num, power));
      getch();
}

Write a Function to Check Number is +ve or -ve



#include <stdio.h>
#include <conio.h>
char _positive(int num)
{
     if(num<0)
     {
              printf("Number is -ve.");    
     }
     else if(num>0)
     {
              printf("Number is +ve.");      
     }
     else
 
     {
              printf("Number is -ve nor +ve.");      
     }
}
main()
{
      int num;
  printf("Enter a number: ");
  scanf("%d", &num);
  printf("%c", _positive(num));
  getch();
}

Run the "Hello, World" program on your system. Experiment with leaving out parts of the program, to see what errer messages you get




// Run the "Hello, World" program on your system
#include <stdio.h>
#include <conio.h>
main()
{
      printf("Hello, World");
      getch();
}
<!--------END--------!>
// without header files errors
main()
{
      printf("Hello, World");
      getch();
}
errors:
1  'printf' undeclared function
2  'getch' undeclared function

<!--------END--------!>
// without main()
#include <stdio.h>
#include <conio.h>

{
      printf("Hello, World"); 
      getch();    
}
errors:
1  expected unqualified-id before '{' token 
2  expected `,' or `;' before '{' token 

<!--------END--------!>

// without semi colan (;)
#include <stdio.h>
#include <conio.h>
main()
{
      printf("Hello, World")
      getch() 
}
errors:
1    In function `int main()': 
2    expected `;' before "getch" 

Get today's date from user and print date of tomorrow



#include <stdio.h>
#include <conio.h>
struct date
{
       int day;
       int month;
       int year;  
   
} today, tomorrow;

int main()
{
    char again;
    again:
     struct date;
     printf("Enter today's date: ");
     scanf("%d", &today.day);
     printf("Enter month: ");
     scanf("%d", &today.month);
     printf("Enter year:  ");
     scanf("%d", &today.year);
 
     switch(today.month)
     {
              case 1:
                   if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                   else
                   printf("Enter a valid date, month and year");                
              break;
              case 2:
                                      if(today.day==28)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                   else if(today.day<28 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                else
                   printf("Enter a valid date, month and year");
              break;
              case 3:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                   else
                   printf("Enter a valid date, month and year");
              break;
              case 4:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                   else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                     else
                   printf("Enter a valid date, month and year");
              break;
              case 5:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                                 
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 6:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                   else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 7:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 8:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 9:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                   else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 10:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                  else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 11:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                    else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 12:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=1;
                                             tomorrow.day=1;
                                             tomorrow.year=today.year+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, tomorrow.year);      
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%00d", tomorrow.day, tomorrow.month, today.year);      
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
           
              default:
                      printf("Enter a valid date, month and year");                
                     
     }
     printf("\nWould you like to input again? y/n ");
     again=getche();
     printf("\n");
     if(again=='y' || again=='Y')
     {
                   goto again;          
     }
 
     getch();
}



 

Search Box

Most Reading

Contact Form

Name

Email *

Message *