Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Thursday, 11 December 2014

Program to print student information
void name(void);
void age(void);
studentdata()
{
printf("Student information\n");
name();
age();
printf("we are in main function\n");
exit(0);
}
void name(void)
{
 char ch;
 printf("enter any character between A-Z\n");
 ch=getchar();
 putchar(ch);
 if((ch>='a')&&(ch<='n'))
 {
  printf("name is MADANREDDY\n");
 }
 else
 {
  printf("name is SURESH\n");
 }
}
void age(void)
{
int n;
printf("enter value for n:\n");
scanf("%d",&n);
if((n>0)&&(n<14))
{
printf("child\n");
}
else if((n>14)&&(n<30))
{
printf("young age\n");
}
else if((n>30)&&(n<60))
{
printf("middle age\n");
}
else if(n>60)
{
printf("old age");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi studentinfo.c
madan@madan-Lenovo-G570:~/madan$ cc -nostartfiles studentinfo.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
Student information
enter any character between A-Z
e
ename is MADANREDDY
enter value for n:
56
middle age
we are in main function

Read More

Tuesday, 9 December 2014

C program to print array elements with references

program to print array elements with references
#include<stdio.h>
main()
{
int a[5]={1,2,3,4,5};
printf("%d\n",a[3]/a[1]);
printf("%d\n",a[6]);
printf("%d\n",a[-2]);
printf("%d\n",a[0]);
printf("%d\n",a[2]);
printf("%d\n",2[a]);
printf("%d\n",2[a]);
printf("%d\n",*(a+1));
printf("%d\n",*a+1);
}
OutPut:
2
0
4195718
1
3
3
3
2
2

Read More

C program to find sum of two arrays

program to find sum of two arrays
#include<stdio.h>
main()
{
int i,n;
int a[5]={12,23,42,11,12},b[5]={65,34,22,1,22},c[5]={0};
n=sizeof(a)/sizeof(a[0]);
//a[5]={12,23,42,11,12}
//b[4]={65,34,22,1,22}
for(i=0;i<n;i++)
{
c[i]=a[i]+b[i];
printf("a[%d]=%d b[%d]=%d c[%d]=%d\n",i,a[i],i,b[i],i,c[i]);
}
printf("\n");
}
OutPut:
adan@madan-Lenovo-G570:~/madan$ ./a.out
a[0]=12 b[0]=65 c[0]=77
a[1]=23 b[1]=34 c[1]=57
a[2]=42 b[2]=22 c[2]=64
a[3]=11 b[3]=1 c[3]=12
a[4]=12 b[4]=22 c[4]=34

Read More
program to find memory locations of array elements
#include<stdio.h>
main()
{
int a[5]={12,32,22,14,54},i,n;
int bit,num;
n=sizeof(a)/sizeof(a[0]);
for(i=0;i<n;i++)
{
printf("a[%d]=%d at %u\n",i,a[i],&a[i]);
num=a[i];
bit=31;
while(bit>=0)
{
num&(1<<bit)?printf("1"):printf("0");
bit--;
}
printf("\n");
//printf("a[%d]=%d at %u\n",i,a[i],&a[i]);
}
}
OutPut:
a[0]=12 at 4080586848
00000000000000000000000000001100
a[1]=32 at 4080586852
00000000000000000000000000100000
a[2]=22 at 4080586856
00000000000000000000000000010110
a[3]=14 at 4080586860
00000000000000000000000000001110
a[4]=54 at 4080586864
00000000000000000000000000110110

Read More

C Program to pass 2D_Array using malloc function

Program to pass 1D_Array using malloc function
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void array1d(int **ptr,int m,int n)
{
int i,j;
printf("The resultant array is..\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",ptr[i][j]);
}
printf("\n");
}
}
main()
{
int **a,rows,cols,i,j;
printf("entyer size of 2d array...\n");
scanf("%d%d",&rows,&cols);
a=malloc(rows*sizeof(int*));
for(i=0;i<rows;i++)
a[i]=malloc(cols*sizeof(int));
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
scanf("%d",&a[i][j]);
array1d(a,rows,cols);
}
OutPut:
entyer size of 2d array...
3
3
9
8
7
6
5
4
3
2
1
The resultant array is..
 9  8  7 
 6  5  4 
 3  2  1

Read More

C program to sort a 2D array

Sorting a 2D-array

#include<stdio.h>
#include<string.h>
main()
{
int a[3][3],i,j,k,n,temp;
n=sizeof(a)/sizeof(a[0]);
printf("enter 2D array....\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf(" %d",&a[i][j]);
//printf("\n");
printf("array before sorting\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf(" %d ",a[i][j]);
printf("\n");
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=j+1;k<n;k++)
{
if(a[i][j]>a[i][k])
{
temp=a[i][j];
a[i][j]=a[i][k];
a[i][k]=temp;
}
}
printf("array after sorting\n");
for(i=0;i<n;i++)
{
for(k=0;k<n;k++)
printf("%d ",a[i][k]);
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi sort_2D_Array.c
madan@madan-Lenovo-G570:~/madan$ cc sort_2D_Array.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter 2D array(3x3)....
9
8
7
6
5
4
3
2
1
array before sorting
 9  8  7 
 6  5  4 
 3  2  1 
array after sorting
7 8 9 
4 5 6 
1 2 3

Read More

C program to Sort an array without using strcmp

Sorting an array without using strcmp

#include<stdio.h>
#include<string.h>
main()
{
int a[5],i,j,n,temp;
n=sizeof(a)/sizeof(a[0]);
printf("enter an array of 5 elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("array before sorting\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("array after sorting\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
OutPut:
enter an array of 5 elements:
43
32
33
12
22
array before sorting
43 32 33 12 22 array after sorting
12 22 32 33 43

Read More

C program to Sort an array by using strcmp

Sorting an array by using strcmp


#include<stdio.h>
#include<string.h>
#include<string.h>
main()
{
char a[4][10],temp[10];
int i,j,k;
printf("enter 4 strings\n");
for(i=0;i<4;i++)
scanf("%s",a[i]);
for(i=0;i<4;i++)
for(j=i+1;j<4;j++)
{
if(strcmp(a[i],a[j])>0)
{
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
}
printf("sorted array is:\n");
for(i=0;i<4;i++)
printf(".....%s....\n",a[i]);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi arr_sort.c
madan@madan-Lenovo-G570:~/madan$ cc arr_sort.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter 4 strings
welcome
madan
tech
world
sorted array is:
.....madan....
.....tech....
.....welcome....
.....world....

Read More
fseek,ftell importance through program
#include"myHeader.h"
main()
{
int pos;
char str[20]="abcdefgh";
FILE *fp;
fp=fopen("madan","r");
if(fp==NULL)
{
printf("no data\n");
return;
}
fseek(fp,4,0);
pos=ftell(fp);
printf("%c ",str[pos]);
fseek(fp,3,1);
pos=ftell(fp);
printf("%c ",str[pos]);
fseek(fp,-1,1);
pos=ftell(fp);
printf("%c ",str[pos]);
//fseek(fp,0,0);
//pos=ftell(fp);
rewind(fp);
printf("%c ",str[pos]);
fclose(fp);
}
OutPut:
e h g g

Read More

C Program to read a source file by using fgets

Program to read a file by using fgets
#include"myHeader.h"
main(int argc,char **argv)
{
char ch[20];
FILE *fp;
if(argc!=2)
printf("enter correct no of arguments\n");

fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("no file exists\n");
return;
}
while((fgets(ch,20,fp)))
printf("%s",ch);
fclose(fp);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter correct no of arguments
no file exists
madan@madan-Lenovo-G570:~/madan$ ./a.out 1.c
#include<stdio.h>
main()
{
 int ch;
printf("enter any character:");
ch=getchar();
printf("entered character:");
putchar(ch);
}

Read More

C program for Pointers with address references

Pointers with address references example
#include"myHeader.h"
main()
{
int a=10;
char *p=&a;
int *p1=&a;
float *p2=&a;
printf("%d\n",*p);
printf("%d\n",*p1);
printf("%d\n",*p2);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
10
10
2147483645

Read More

C Program to print strings through command line

Program to print strings through command line
#include"myHeader.h"
main(int argc,char **argv)
{
int i;
for(i=0;i<argc;i++)
printf("%s\n",argv[i]);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out 1223 1223
1223
1223

Read More

C Program to print Binary Equivalent of a number

Program to print Binary Equivalent of a number
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int num,bit=0;
printf("enter a number\n");
scanf("%d",&num);
l1:
num&(1<<bit)?printf("1"):printf("0");
bit++;
if(bit<=31)
goto l1;
printf("\n");
}
OutPut:
enter a number
12
00110000000000000000000000000000
(for big endian machine)

Read More

C Program to print Binary Equivalent of a number

Program to print Binary Equivalent of a number
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int num,bit=0;
printf("enter a number\n");
scanf("%d",&num);
l1:
num&(1<<bit)?printf("1"):printf("0");
bit++;
if(bit<=31)
goto l1;
printf("\n");
}
OutPut:
enter a number
12
00110000000000000000000000000000
(for big endian machine)

Read More

C program for the Implementation of atoi function

Implementation of atoi function
#include<stdio.h>
int myatoi(const char *string);
int main(int argc, char* argv[])
{
int i;
for(i=1;i<argc;i++)
  printf("\n%d\n", myatoi(argv[i]));
//  getch();
 // return(0);
}
int myatoi(const char *string)
{
    int i;
    i=0;
    while(*string)
    {
         i=(i<<3) + (i<<1) + (*string - '0');
         string++;
         // Dont increment i!
    }
    return(i);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out 1234

1234

Read More
Dynamic Memory allocation concept Related to 1D array
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int a[4]={0},*p,i;
p=malloc(4*sizeof(int));
for(i=0;i<4;i++)
scanf("%d",&a[i]);
for(i=0;i<4;i++)
printf(" %d ",a[i]);
}
OutPut:
1
2
3
4
 1  2  3  4

Read More

C Program to print integer equivalent of character

Program to print integer equivalent of character
#include<stdio.h>
main()
{
int n;
printf("enter any integer\n");
scanf("%d",&n);
printf("n=%d n=%c\n",n,n);
}
OutPut:
enter any integer
123
n=123 n={
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any integer
45
n=45 n=-
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any integer
56
n=56 n=8
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any integer
98
n=98 n=b

Read More
Program to print octal,decimal,hexa decimal formats
#include<stdio.h>
main()
{
int n;
printf("enter value for n\n");
scanf("%d",&n);
printf("n=%d n=%x n=%o\n",n,n,n);
}
OutPut:
enter value for n
45
n=45 n=2d n=55

Read More

C Program to find Area and Perimeter of a circle

Program to find Area & Perimeter of a circle
//#define pi=3.14;
#include<stdio.h>
main()
{
float l,b,r,arear,perr,areac,perc,pi=3.14;
printf("enter length, breadth of rectangle\n");
scanf("%f%f",&l,&b);
arear=(l*b);
perr=2*(l+b);
printf("area=%f perimeter=%f\n",arear,perr);
printf("enter radius of circle\n");
scanf("%f",&r);
areac=(pi*r*r);
perc=2*pi*r;
printf("area of circle=%f perimeter of circle=%f\n",areac,perc);
}
OutPut:
enter length, breadth of rectangle
1
2
area=2.000000 perimeter=6.000000
enter radius of circle
4
area of circle=50.240002 perimeter of circle=25.120001

Read More

C Program to find Average of numbers

Program to find Average of numbers
#include<stdio.h>
main()
{
int m1,m2,m3,m4,m5,total=0;
float average;
printf("enter marks in 5 subjects\n");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
if(m1&&m2&&m3&&m4&&m5<=50)
{
total=(m1+m2+m3+m4+m5);
average=((m1+m2+m3+m4+m5)/5);
printf("Total:%d Average:%f\n",total,average);
}
else
printf("enter currect marks\n");
}
OutPut:
enter marks in 5 subjects
43
54
32
33
22
Total:184  Average:36.000000

Read More