Showing posts with label c programs. Show all posts
Showing posts with label c programs. Show all posts

Wednesday, July 22, 2015

Dynamic Memory Allocation - A simple example

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *data;
    printf("hit enter to create memory");
    getch();
    data=(int*)malloc(sizeof(int));

    if(data==NULL)
    {
        printf("\nError! memory not allocated.");
        exit(0);
    }
    printf("\nEnter data to store: ");
    scanf("%d",data);
    printf("data stored is =%d",*data);

    return 0;
}

Monday, June 13, 2011

Greatest Number


#include<stdio.h>
#include<conio.h>
void main()
{float a,b;
clrscr();
printf("enter two nos to find the greatest");
scanf(" %f %f",&a,&b);
if(a>b)
printf("\n%f is greater",a);
else if(b>a)
printf("\n%f is greater",b);
else
printf("both are equal");
getch();
}

Factorial


 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int n,fac =1,i;
 printf("Enter the number for which the factorial is to be found :") ;
 scanf("%d",&n);
 for (i=1;i<=n;i++)
 fac*=i;
 printf("The factorial of %d is %d",n,fac);
 getch();
 }

Finding Roots Of An Equation


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


void main()
{
int a,b,c;
clrscr();
printf("enter three nos to find the result of the equation\n1.a+b+c and 4a+4b-2c");
scanf("%d%d%d",&a,&b,&c);
printf("result 1:%d\nresult 2:%d",a+b+c,(4*a)+(4*b)-(2*c));
getch();
}

Arithmetic Operations


#include<stdio.h>
#include<conio.h>
void main()
{float a,b;
char n;
clrscr();
printf("enter any two numbers to find its sum/multiplication/division/subtracton");
scanf(" %f %f",&a,&b);

Concatenate A Couple Of Strings


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


void main()
{char str1[20],str2[20];
int i;
clrscr();
puts("enter 2 strings (max 20 characters)");

Display Primary Colours In Text


#include<stdio.h>
#include<conio.h>
void main()
{char c;
clrscr();
printf("enter the 1st letter of the colour you want it to b displayed");
scanf(" %c",c);
switch(c)
{case 'r':printf("red");break;
 case 'b':printf("blue");break;
 case 'g':printf("green");break;

Sunday, June 12, 2011

Armstrong number

//Armstrong Number
#include<stdio.h>
#include<conio.h>
void main()
{int n,dupe,sum=0,x;
clrscr();
printf("enter the number");
scanf(" %d",&n);
dupe=n;
while(dupe!=0)
{x=dupe%10;
sum=sum+x*x*x;
dupe=dupe/10;
}
if(sum==n)
printf("\n armstrong no");