Walk Lightly on this PLANET and yet leave such a FOOTPRINT that cannot be erased for thousands of Years..!!!
Visit Codstech for Cyber Security related Posts !

Visitors

Saturday, February 19, 2011

C30 : Sum of n even numbers using user defined functions

Back to Home   &  C


//Program to find sum of n even numbers using user defined functions.

#include<stdio.h>
#include<conio.h>
int sum(int l);
void main()
{
clrscr();
int n,s;
printf("Enter limit value\n");
scanf("%d",&n);
s=sum(n);
printf("Sum of even numbers=%d",s);
getch();
}
int sum(int l)
{
int i,sum=0;
for(i=0;i<l;i++)
{
if(i%2==0)
{
sum=sum+i;
}
}
return(sum);
}
===================================
Output :
Enter limit value
4
Sum of even numbers= 2
=========================================================================
Back to Home   &  C

C29 : Add two numbers(Using user defined functions)

Back to Home   &  C


//Program to add two numbers using user defined functions.

#include<stdio.h>
#include<conio.h>
add();
void main()
{
clrscr();
getch();
}
int add()
{
int a,b,c;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
c=a+b;
printf("addition=%d",c);
return(0);
}
-----------------------------------------------------------
Output
Enter 2 numbers
12
12
Addition=24
===============================================
Back to Home   &  C


C28 : Display a string(Using user defined functions)

Back to Home   &  C


//Program to display a string using user defined functions.


#include<stdio.h>
#include<conio.h>
display();
void main()
{
clrscr();
getch();
}
int display()
{
printf("Welcome");
return(0);
}
---------------------------------------------------------
Output
Welcome
===============================================
Back to Home   &  C

C27 : Manipulate strings(using string library functions)

Back to Home   &  C


//Program to manipulate string using library functions.

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20];
char s2[20];
int l;
clrscr();
printf("Enter two strings\n");
scanf("%s%s",s1,s2);
l=strlen(l);
printf("Length of string=%d\n",l);
strcat(s1,s2);
printf("Combined string=%s",s1);
getch();
}
---------------------------------------------------------------
Output
Enter two strings
Welcome
C
Length of the string=7
Combined string=Welcome C
===============================================

//Error in output
Back to Home   &  C

C26 : Addition of two numbers using pointer

Back to Home   &  C


//Program to print addition of two numbers by using pointer

#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,*p1,*p2,*p3;
clrscr();
printf("Enter first number\n");
scanf("%d",&num1);
printf("Enter second number\n");
scanf("%d",&num2);
p1=&num1;
p2=&num2;
*p3=*p1+*p2;
printf("addition=%d",*p3);
getch();
}
------------------------------------------------------------
Output
Enter first number
40
Enter second number
30
Addition=70
===============================================
Back to Home   &  C

C25 : Print variable value and its address by using pointer

Back to Home   &  C


//Program to print a variable value and its address by using pointer

#include<stdio.h>
#include<conio.h>
void main()
{
int n,*pt;
clrscr();
printf("Enter a number\n");
scanf("%d",&n);
pt=&n;
printf("address of variable=%u\n",pt);
printf("Value of variable=%d",*pt);
getch();
}
----------------------------------------------------
Output
Enter a number
23
Address of variable=65524
Value of variable=23
===============================================
Back to Home   &  C

C24 : Enter a number and print it by using pointer

Back to Home   &  C


//Program to enter a number and print it by using a pointer


#include<stdio.h>
#include<conio.h>
void main()
{
int a,*p;
clrscr();
printf("Enter a number\n");
scanf("%d",&a);
p=&a;
printf("Variable Value by using pointer=%d\n",*p);
getch();
}
--------------------------------------------------
Output
Enter a number
12
Variable value by using pointer=12
===============================================
Back to Home   &  C