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

Friday, February 18, 2011

C23 : Print current and next memory address of a variable

Back to Home   &  C


//Print current and next memory address of a variable

#include<stdio.h>
#include<conio.h>
void main()
{
int a,*p;
clrscr();
printf("Enter a number\n");
scanf("%d",&a);
p=&a;
printf("Value in variable a=%d\n",a);
printf("Address of variable a=%u\n",p);
p=p+1;
printf("NEXT MEMORY ADDRESS OF A=%u",p);
getch();
}
-------------------------------------------------------------------
Output
Enter a number
12
Value in variable a=12
Address of variable a=65524
NEXT MEMORY ADDRESS OF A=65526
===============================================
Back to Home   &  C

Thursday, February 17, 2011

C22 : Declare a memory variable and print its address

Back to Home   &  C


//Declare a memory variable and print its address  by using a pointer variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,*p;
clrscr();
p=&n;
printf("Address of variable n=%u",p);
getch();
}
---------------------------------------------------
Output
Address of variable n = 65524
========================================================
Back to Home   &  C

C21 : Enter details using Structures

Back to Home   &  C


A ))) //Program to enter student details by using a structure called stud

#include<stdio.h>
#include<conio.h>
struct stud
{
int rno;
char name[20];
char branch[20];
};
void main()
    {
    clrscr();
    struct stud s;
    printf("Enter roll no\n");
    scanf("%d",&s.rno);
    printf("Enter name\n");
    scanf("%s",s.name);
    printf("Enter branch\n");
    scanf("%s",&s.branch);
       clrscr();
       printf("Details of the student is..\n");
       printf("Roll number-%d\nName-%s\nBranch-%s\n",s.rno,s.name,s.branch);
getch();
}
----------------------------------------------------------------------------
Output :
Enter roll no
1
Enter name
aaa
Enter branch
Computer Science
Details of the student is..
Roll No-1
Name-aaa
Branch-Computer Science
===============================================
//you can write the above printf statement as :
printf("Roll number=%d\n",s.rno);
printf("Name=%s\n",s.name);
printf("Branch=%s\n",s.branch);
=========================================================================

B ))) //Program to enter n number of employee details by using a structure called emp


#include<stdio.h>
#include<conio.h>
struct emp
{
char name[20];
char dept[20];
int code;
};
void main()
{
struct emp e[20];
int i,n;
clrscr();
printf("Enter number of employee\n");
scanf("%d\n",&n);
for(i=0;i<=n;i++)
printf("Enter employee name\n");
scanf("%s",e[i].name);
printf("Enter department\n");
scanf("%s",e[i].dept);
printf("Enter employee code\n");
scanf("%d",&e[i].code);
clrscr();
printf("Name\tDepartment\tcode\n");
for(i=0;i<=n;i++)
{
printf("%s\t%s\t%d\n",e[i].name,e[i].dept,e[i].code);
}
getch();
}
-------------------------------------------------------------------------------
Output
Enter number of employee
2
Enter employee name
aaa
Enter department
Computer Science
Enter employee code
11

Enter employee name
bbb
Enter department
Maths
Enter employee code
22
This will print as :
Name    Dept            code
aaa         cs                11
bbb        maths           22
===============================================

C ))) //Program to enter n number of student details,
enter 3 subject mark, and find total marks (by using a structure called student)


#include<stdio.h>
#include<conio.h>
struct student
{
int rno;
char name[20];
int m1,m2,m3,t;
};
void main()
    {
    clrscr();
    struct student s[20];
    int i,n;
    printf("Enter number of students\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    printf("Enter roll no\n");
    scanf("%d",&s[i].rno);
    printf("Enter name\n");
    scanf("%s",s[i].name);
    printf("Enter 3 subject marks\n");
    scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3);
    s[i].t=s[i].m1+s[i].m2+s[i].m3;
    }
     clrscr();
     printf("Details of the student is..\n");
     printf("Name\tRno\tMark1\tMark2\tMark3\tTotal\n");
     for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t%d\t%d\t%d\n",s[i].name,s[i].rno,s[i].m1,s[i].m2,s[i].m3,s[i].t);
getch();
     }
     }
-------------------------------------------------------------
Output
Enter number of students
2
Enter roll no
1
Enter name
aa
Enter 3 subject marks
21
34
56

Enter roll no
2
Enter name
bb
Enter 3 subject marks
65
65
89
This will print as :
Roll no    name          mark1     mark2    mark3    total
1              aa                  21             34          56        111
2              bb                 65            65           89         219
===============================================
Back to Home   &  C

Wednesday, February 16, 2011

C20 : Matrix addition

Back to Home   &  C


//Program to enter a matrix and print its addition

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5][5],b[5][5],c[5][5],i,j,r,cl;
printf("Enter row and column\n");
scanf("%d%d",&r,&cl);
printf("Enter elements of first matrix\n");
for(i=0;i<r;i++)
{
for(j=0;i<cl;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Matrix elements of second matrix");
for(i=0;i<r;i++)
{
for(j=0;i<cl;j++)
{
 scanf("%d",&b[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;i<cl;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;i<cl;j++)
{
printf("%d\t",c[i][j]);
printf("\n");
}
getch();
}
}
-------------------------------------------------------------------
Output
Enter row and column
2
2
Enter elements of first matrix
1
1
1
1
Enter elements of second matrix
2
2
2
2
----------------
3   3
3   3
(May be some error in output)
================================
Back to Home   &  C

Saturday, February 12, 2011

C19 : Enter a matrix and print it

Back to Home   &  C


//Program to enter a matrix and print it

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5][5],i,j,r,c;
printf("Enter row and column size\n");
scanf("%d%d",&r,&c);
printf("Enter elements of matrix\n");
for(i=0;i<r;i++)
{
for(j=0;i<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Matrix elements...\n");
for(i=0;i<r;i++)
{
for(j=0;i<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
getch();
}
-------------------------------------------------------------
Output ( May be some error in output)
Enter row and column size
2
2
Enter elements of matrix
34
65
78
98
Matrix elements...
34        65
78        98
(Will print in matrix format)
================================
Back to Home   &  C

Friday, February 11, 2011

C18 : Sort n numbers of array elements in ascending order

Back to Home   &  C


//Enter n numbers into an array and sort it in ascending order.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,n,a[20],t;
printf("Enter limit value\n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
 }
printf("Array elements in ascending order...\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
-------------------------------------------------------------------
Output
Enter limit value
3
Enter array elements
3
1
6
Array elements in ascending order...
1
3
6
===============================================
Back to Home   &  C

C17 : Enter n numbers into an Array and print it

Back to Home   &  C


//Program to enter n numbers into an array and print it

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,n;
printf("Enter limit value\n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Array elements are...\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
-----------------------------------------------------------------
Output
Enter limit value
2
Enter array elements
16
76
Array elements are...

16
76
===============================================
Back to Home   &  C

C16 : Array Declaration

Back to Home   &  C


//Program to declare an Array and initialize value to it

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5]={20,30,10,40,60};
int i;
for(i=0;i<5;i++)
 {
 printf("\n%d\n",a[i]);
 }
getch();
}
---------------------------------------------------------
Output
20
30
10
40
60
===============================================
Back to Home   &  C

C15 : Print numbers that is divisible by 7

Back to Home   &  C


//Program to print numbers that is completely divisible by 7 between 1 and n,
also count total number,and find its sum

#include<stdio.h>
#include<conio.h>
void main()
 {
 clrscr();
 int i=0,ct=0,sum=0,n;
 printf("Enter limit value\n");
 scanf("%d",&n);
 printf("The numbers are...\n");
 while(i<=n)
 {
 if(i%7==0)
 {
 printf("%d\n",i);
 ct++;
 sum=sum+i;
 }
 i++;
 }
 printf("sum=%d\n",sum);
 printf("count=%d",ct);
 getch();
 }
-----------------------------------------------------------------------------------
Output
Enter Limit
50
Numbers are...
0
7
14
21
28
35
42
49
sum=196
count=8
====================================
Back to Home   &  C


Thursday, February 10, 2011

C14 : Print numbers upto a given limit

Back to Home   &  C


A ))) //Program to print numbers upto 20 using While loop

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i=0;
while(i<=20)
{
printf("%d\n\n",i);
i++;
}
getch();
}
---------------------------------------------------------
Output
0
1
2
..
..
20
===============================================

B ))) //Program to print numbers from  20 to 1 using While loop

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i=20;
while(i>0)
{
printf("%d\n\n",i);
i--;
}
getch();
}
---------------------------------------------------------
Output
20
19
18
..
..
2
1
===============================================
C ))) //Program to print numbers from  100 to 60 using Do-While loop



//Program to print the numbers from 100 to 60 using do-while loop
#include<stdio.h>
#include<conio.h>
void main()
 {
 clrscr();
 int i=100;
 do
 {
 printf("%d\n",i);
 i--;
 }
 while(i>=60);
 getch();
 }

---------------------------------------------------------
Output
100
99
98
..
..
61
60
===============================================
Back to Home   &  C



C13 : Read temperature and convert into celcious

Back to Home   &  C


//Program to read temperature and convert into celcious

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
float f,c;
printf("Enter temperature\n");
scanf("%f",&f);
c=f-32/1.8;
printf("Temperature in Celcious=%f",c);
getch();
}
--------------------------------------------------------
Output
Enter temperature
12.34
Temperature in Celcious=5.437778
==========================================
Back to Home   &  C