Showing posts with label Functions. Show all posts

Write a program in c language to reverse a string by using strrev function or ptrint a string in reverse order

#include <stdio.h>
#include <string.h>
#include <conio.h>

int main()
{
   char str[100];

   printf("Enter a string to reverse: ");
   gets(sttr);

   strrev(str);  //function to reverse a string

   printf("Reverse of entered string is: %s\n", str);

   getch();
}

Write a program in c language to get three numbers from user and print maximum number.

#include<stdio.h>
#include<conio.h>
int maximum(int,int,int);
int main()
{
    int num1,num2,num3;
    printf("enter hte three integer: ");
    scanf("%d %d %d",&num1,&num2,&num3);
    printf("Max is %d",maximum(num1,num2,num3));
    getch();
}
int maximum(int x,int y,int z)
{
   int max;
       if(x>y && x>z)
       {
              max=x;
       }
       if(y>z && y>x)
       {
              max=z;
       }
       if(z>x && z>y)
       {
              max=z;          
       }
       return max;
}
         
                               
                     
                   


Write a program in c language to print counting using function.

#include<stdio.h>
#include<conio.h>
int mul(int);
int main()
{
    int a=10;
    printf("%d", mul(a));
    getch();
}
int mul(int a)
{
       int x;
       for(x=1;x<=a;x++)
       {
              printf("%d\n",x);
       }

}

Write a program in c language to convert weight in grams from kilograms using function

#include<stdio.h>
#include<conio.h>
int grams(int); // function prototype
int main()
{
    int kg,gm;
    printf("Enter the weight in kilogram: ");
    scanf("%d",&kg);
    gm=grams(kg);
    printf("%d Kilograms = %d Grams\n", kg,gm);
    getch();
}
int grams(int weight) //function defination
{
    return weight*1000;
}

Write a function in C++ to add two numbers using function.

#include <iostream>
using namespace::std;
inline
int add(int , int);
int main()
{
    int a, b;
    cin>>a>>b;
    cout<<add(a, b)<<endl;
    system("pause");
}
int add(int a, int b)
{
    return a+b;
}

Conversion of temprature by using a function



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

WRITE A PROGRAM TO ADD THE TWO VALUES THROUGH FUNCTION TO POINTER.



#include<conio.h>
#include<stdio.h>
void add(int*,int*);
int main()
{
    int a=5,b=4;
    int *y,*z;
    y=&a,z=&b;
    add(&a,&b);
    getch();
    }
    void add(int*l,int*m)
    {
          int c=0;
         c=(*l+*m);
         printf("%d",c);
         }



#include <stdio.h>
#include <conio.h>
char reverse()
{
       int counter=0, m=0, i=0;
       char string[1000], ch;
       while(ch!=27)
       {
                   ch=getche();
                   string[i]=ch;
                   if(ch==13)
                   {
                             printf("\n");
                                 for(m=i; m>=0; m--)
                                 {
                                       printf("%c", string[m]);
                                 }
                                 printf("\n");        
                             i=-1;
                   }
                   i++;
       }
}
int main()
{
       printf("Enter data and press \'Esc\' when you complete typing.");
       reverse();
       getch();
}

Write a program to get two numbers from user and print their sum on the screen using function.


Sum Through Function:

#include<stdio.h>
#include<conio.h>
int sum(int x,int y);

int main()
{
 int a,b;
 printf("enter the value of a\n");
 scanf("%d",&a);
  printf("enter the value of b\n");
 scanf("%d",&b);
 printf("the sum is %d\n",sum(a,b));
 getch();
}
int sum(int x,int y)
{
return x+y;
}

Write a programe to print size of variable data types using sizeof() function.



#include<stdio.h>
#include <stdlib.h>
int main()
{
char a;
int b;
float c;
long d;
double e;
bool f;
long int g;
printf("Size of char type variable: %d\n", sizeof(a));
printf("Size of int type variable: %d\n", sizeof(b));
printf("Size of float type variable: %d\n", sizeof(c));
printf("Size of long type variable: %d\n", sizeof(d));
printf("Size of double type variable: %d\n", sizeof(e));
printf("Size of bool type variable: %d\n", sizeof(f));
printf("Size of long int type variable: %d\n", sizeof(g));
system("PAUSE");
return 0;
}

Write A Program Of Sum of Ten Number Through Function:


#include<stdio.h>
#include<conio.h>
int mul(int );//function prototype
int main()
{
    int a,sum=0;
    printf("enter the value of a\n");
       for(a=1;a<=10;a++){
//                      printf("%d\n",mul(a));
                   sum=sum+a;
                                 }
                                 printf("sum is%d", mul(sum));// function call which can pass the value to function definition
     
    getch();
}
int mul(int x)// function definition
{
 return x;
}

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();
}

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();
}

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

#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();
}

Write a program to get two numbers from user and print their sum on the screen using function.


Sum Through Function:

#include<stdio.h>
#include<conio.h>
int sum(int x,int y);

int main()
{
 int a,b;
 printf("enter the value of a\n");
 scanf("%d",&a);
  printf("enter the value of b\n");
 scanf("%d",&b);
 printf("the sum is %d\n",sum(a,b));
 getch();
}
int sum(int x,int y)
{
return x+y;
}

Write A Program Of Sum of Ten Number Through Function.


#include<stdio.h>
#include<conio.h>
int mul(int );//function prototype
int main()
{
    int a,sum=0, num;
    printf("Enter last number: ");
       scanf("%d", &num);
       printf("sum is %d", mul(num));// function call which can pass the value to function definition
   
    getch();
}
int mul(int x)// function definition
{
    int sum=0, a;
    for(a=1;a<=x;a++)
       {
             sum=sum+a;
       }
 return sum;
}


Write a program to print table of given number using function.

#include<stdio.h>
#include<conio.h>
 int table(int,int);// function prototype
int main()
{
   
    int number,a;
    printf("Enter the number of table \n");
    scanf("%d",&number);
    for(a=1;a<=10;a++){
    //printf("%d*%d=%d\n",number,a, table(number,a));} //function call
    table(number,a);}
    getch();
}
int table(int t , int b)// function definition
{     
    printf(" %d*%d=%d\n",t,b,b*t);
    //return b*ta;
}
    

WRITE A Program To Find A Maximum Number Through Function

#include<stdio.h>
#include<conio.h>
 int max (int ,int );// function protype
int main()
{
   
    int a,b;
    scanf("%d %d",&a,&b);
    max(a,b); //function call
    printf("ok");
    getch();
}
int max (int x,int y)// function definition
{  
     if(x>y){
   printf("max is %d",x);
   }
     if(y>x){
          printf("max is %d\n",y);
     }}