Completely Solved C, C++ Programs Assignment.




Showing posts with label Sample C program. Show all posts
Showing posts with label Sample C program. Show all posts

C Program to accept data and store the given data into file print the data.

Filed Under:

Problem 92. C Program to accept data and store the given data into file print the data.
#include<conio.h>
#include<stdio.h>
main( )
{
FILE *fp;
char c;
fp=fopen(“data.dat”,”w”);
clrscr();
printf(“enter text”);
while(1)
{
c=getchar( );
if(c==eof( ))
break;
putc(c);
}
fclose(fp);
fp=fopen(“data.dat”,”r”);
while(1)
{
c=getc(fp);
if(c==eof( ))
break;
putchar(c);
}
getch( );
fclose(fp);
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to accept a string and print by trailing spaces.

Filed Under:

Problem 99. C Program to accept a string and print by trailing spaces.
#include<conio.h>
#include<stdio.h>
main( )
{
char n,n1;
clrscr ( );
printf(“enter a string;”);
while((n=getchar( )!=’n’)
if(n>=’a’ && n<=’z’)
putchar(n);
else
if(n>=’a’ && n<=’z’)
putchar(n);
getch( );
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program for example of static variable.

Filed Under:

Problem 98. C Program for example of static variable.
#include<conio.h>
#include<stdio.h>
static int i=1;
main( )
{
int j;
clrscr( );
for (j=1;j<=5;j++);
fun( );
getch( );
}
fun( ){
printf(“n%d”,i);
i=i+1;
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to accept two numbers and interchange two values using functions.

Filed Under:

Problem 97. C Program to accept two numbers and interchange two values using functions.
#include<conio.h>
#include<stdio.h>
void swap (int a, int b);
main( )
{
int a,b;
clrscr( );
printf(“enter value for a;”);
scanf(“%d”,&a);
printf(“enter value for b;”);
scanf(“%d”,&b);
swap(a,b);
getch( );
}
void swap(int a,int b)
}
int c;
c=a;
a=b;
b=c;
printf(“na=%d”,a);
printf(“nb=%d”,b);
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to accept a string in lower case and print first character of each word in upper case

Filed Under:

Problem 96. C Program to accept a string in lower case and print first character of each word in upper case.
#include<conio.h>
#include<stdio.h>
main( )
{
char str1[80];
int length,i;
clrscr( );
printf(“enter a string; “);
length=getline(str1,80);
for(i=0;i<length;i++)
{
str1[0]-=32;
if(str1[i]= =’ ‘)
str1[i+1]-=32;
printf(“%c”.str1[i]);
}
getch();
}
int getline(char line [], int lim)
{
int i;
for(i=0;i<lim && ((line[i]=getchar( ))! =’n’);i++);
if(line[i]= =’n’)
line[i]=’�’;
return i;
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to create a file of numbers and copy odd number into second file and even number into third file

Filed Under:

Problem 95. C Program to create a file of numbers and copy odd number into second file and even number into third file
#include<conio.h>
#include<stdio.h>
main( )
{
FILE *fp1,*fp2,*fp3;
int i;
fp1=open(“data1”,w”);
printf(“enter the number”);
scanf(“%d”,&i);
while(i!=eof)
{
putw(i,fp1);
scanf(“%d”,&i);
}
fcolse(fp1);
fp1=fopen(“data1”,”r”);
fp2=fopen(“data2”,”w”);
fp3=fopen(“data3”,”w”);
while((i=getc(fp1))!=eof)
if(i%2==0)
putc(i,fp3);
else
putw(i,fp2);
fcolse(fp1);
fcolse(fp2);
fcolse(fp3);
getch( );
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to copy contents of one file into another.

Filed Under:

Problem 94. C Program to copy contents of one file into another.
#include<conio.h>
#include<stdio.h>
main( )
{
FILE * fp1,*fp2;
char ch;
fp1=fopen(“text1”,”w”);
printf(“enter the text”);
while((ch=getchar()!=EOF);putc(ch,fp1);
fclose(fp1);
fp1=fopen(“text1”,”r”);
fp2=fopen(“text2”,”w”);
while((ch=getc(fp1))!=EOF)
putc(ch,fp2);
fcolse(fp1);
fcolse(fp2);
getch( );
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to accept data in lower case and store the given data into file into upper case and print the data.

Filed Under:

Problem 93. C Program to accept data in lower case and store the given data into file into upper case and print the data.
#include<conio.h>
#include<stdio.h>
main( )
{
FILE *fp;
Char c;
fp=fopen(“data2.dat”,”w”);
clrscr( );
printf(“enter text”);
while((c=getchar( ))!=eof( ))
{
putc(toupper(c),fp)
}
fclose(fp);
fp=fopen(“data2.dat”,”r”);
while(1)
{
c=getc(fp);
if(c==eof( ))
break;
putchar(c);
}
getch( );
fclose(fp);
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Prongram to accept a string and print each word in reverse

Filed Under:

Problem 90. C Prongram to accept a string and print each word in reverse
#include<conio.h>
#include<stdio.h>
main( )
{
char name[80];
int i,j,start=0,end,len;
clrscr( );
printf(“enter a string”);
scanf(“%s”,name);
for(i=0;i<80 &&((name[i]=getchar( ) )!=’n’);i++);
len=i;
for(i=0;i<len;i++)
if(name[i]==’ ‘|| name[i]==’n’)
{
end=i;
while((end--)>=start)
{
printf(“%c”,name[end]);
}
start=i+1;
}
getch( );
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to print anti diagonal.

Filed Under:

Problem 100. C Program to print anti diagonal.
#include<conio.h>
#include<stdio.h>
main( )
{
int a[4][4],i,j,c;
clrscr( );
printf(“enter which number you want;”);
scanf(“%d”,&c);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(i+j= =3)
a[i]]j]=c;
else
a[i][j]=0
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf(“%d”,a[i][j]);
printf(“n”);
}
getch( );
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to accept elements into single dimensional array and print the array in ascending order by using three different arrays.

Filed Under:

Problem 91. C Program to accept elements into single dimensional array and print the array in ascending order by using three different arrays.
#include<conio.h>
#include<stdio.h>
void read_array(int x[]);
void sort_array(int y[]);
void print_array(int z[]);
main()
{
int a[10];
clrscr( );
read_array(a);
sort_array(a);
print_array(a);
getch( );
}
void read_array(int x[])
{
int i;
for(i=0;i<10;i++)
{
printf(“enter value for a[%d]”,i);
scanf(“%d”,&x[i]);
}
}
void sort_array(int y[])
{
int i,j,k;
for(i=0;i<9;i++)
for(j=i+1;j<=9;j++)
if(y[i]>y[j])
{
k=y[i];
y[i]=y[j];
y[j]=k;
}
}
void print_array(int z[])
{
int i;
for(i=0;i<10;i++)
printf(“%dn”,z[i]);
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to print 4 dimentional matrix with constant number.

Filed Under:

Problem 89. C Program to print 4 dimentional matrix with constant number.
#include<stdio.h>
#include<conio.h>
main( )
{
int a[4][4],i,j,c;
clrscr( );
printf(“enter constant number”);
scanf(“%d”,&c);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
a[i][j]=c;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf(“%d”,a[i][j]);
printf(“n”);
}
getch( );
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to accept two strings and print biggest among them

Filed Under:

Problem 88. C Program to accept two strings and print biggest among them.
#include<stdio.h>
#include<conio.h>
int getline(char line[],int lim);
main( )
{
char str1[80],str2[80];
int len1,len2;
clrscr( );
printf(“enter first string”);
len1=getline(str1,80);
printf(“enter second string”);
len2=getline(str1,80);
if(len1 >len2)
printf(“first string bigger than second string”);
else
if(len1<len2)
printf(“second string bigger than first string”);
else
printf(“both strings are equal”);
getch( );
}
int getline(char line[],int lim)
{
int i;
for(Ii0;i<lim && ((line[i]=getchar( ))!=’n’);i++)
if(line[i]==’n’)
line[i]=’�’;
return i;
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to accept a single dimensional array and print them by using pointers

Filed Under:

Problem 87. C Program to accept a single dimensional array and print them by using pointers
#include<stdio.h>
#include<conio.h>
main( )
{
int a[5],*b,i;
clrscr( );
b=&a[0];
for(i=0;i<=4;i++)
{
printf(“enter the a value for a[%d]”,i)
scanf(“%d”,b);
b++;
}
b=&a[0];
for(i=0;i<=4;i++)
{
printf(“n%d”,*b);
b++;
}
getch( );
}
C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to accept two 3 dimensional array and store subtraction of those two arrays into third array

Filed Under:

Problem 86.C Program to accept two 3 dimensional array and store subtraction of those two arrays into third array.
#include<stdio.h>
#include<conio.h>
main( )
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr( );
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
printf(“enter two values for a[%d][%d] & b[%d][%d]:”,i,j,i,j);
scanf(“%d%d”,&a[i][j],&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]-b[i][j];
printf(“%d”,,c[i][j]);
}
printf(“n”);
}
getch( );
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to accept a string and print reverse of the given string by using functions.

Filed Under:

Problem 85. C Program to accept a string and print reverse of the given string by using functions.
#include<stdio.h>
#include<stdio.h>
int getline (char str[]);
void printline (char str[],int i);
main( )
{
char str[80];
int 1;
clrscr( );
1=getline(str );
printline(str,1);
printline(str,1);
getch ( );
}
int getline(char str[])
{
int 1;
printf(“enter a string;”);
for(i=0;i<80&&((str[i]=getchar())!=’n’);i++);
if(str[i]=’�’;
return i;
}
void printline(char str[],int 1)
{
int j;
for(j=1;j<=0;j--)
printf(“%c”,str[j]);
printf(‘is the revefrse string”);
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to read a string and print the first two characters of each word in the string

Filed Under:

Problem 83. C Program to read a string and print the first two characters of each word in the string.
#include<stdio.h>
#include<conio.h>
main( )
{
char s[100];
int i,l;
clrscr( );
printf(“enter a string”);
gets(s);l=strlen(s);
for(i=0;i<l;i++)
{
if(s[i]!=’ ‘ && s[i]=’ ‘)
{
printf(“%c %c”,s[i],s[i+1])
i=i+2;
while(s[i]!=’ ‘)
i++;
}
}
getch( );
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to accept two numbers and print the sum of given two numbers by using pointers

Filed Under:

Problem 84. C Program to accept two numbers and print the sum of given two numbers by using pointers
#include<stdio.h>
#include<conio.h>
main( )
{
int a, b,c;
clrscr( );
a=10;
b=20;
c=*(&a)+*(&b);
printf(“%d”,c);
getch( );
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to accept a string using pointers and functions

Filed Under:

Problem 82. C Program to accept a string using pointers and functions.
#include<stdio.h>
#include<conio.h>
main( )
{
int ch[20];
clrscr ( );
printf(“enter a string”);
read_array(ch);
printf(“%s”,ch);
getch( );
}
void read_string (char*pt)
{
for(;(*pt=getchar( ))!=’/n’;pt++);
*pt=’�’;
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment



C Program to accept two strings and compare those two strings

Filed Under:

Problem 81. C Program to accept two strings and compare those two strings.
#include<stdio.h>
#include<conio.h>
int strcomp (char *pt1, char *pt2);
void read-string(char*pt);
main( )
{
char line [80],line2[80];
clrscr( );
printf(“enter first string;”);
read-string (line1);
printf(“enter second string”);
read-string(line2);
if(strcomp (line1,line2)>0)
printf(“second string biggest”);
else
if(strcomp (line1,line2)>0)
printf(“ first string biggest;”);
else
printf(“both the strins are equal”);
getch( );
}
void read-string(char*pt)
{
for(;(*pt=getchar( ))!=’n’;pt++);
*pt=’�’;
}
int strcomp (char *pt1, char *pt2)
{
for(;*pt1!=’�’;pt1++;pt2++)
if(*pt1!=*pt2)
break;
return *pt1-*pt2;
}

C compiler: gcc 4.1.2

 Back to main directory: Sample C Program | Software Practical | Solved C Assignment