|
You think you are the best programmer out there? Well here are some challenges!
Program to Get Personal Details In C with Cprintf:
#include<stdio.h>
#include<conio.h>
void
main()
{
char
name[10],Add1[15],Add2[15],Phone[11];
clrscr();
textattr(10);
gotoxy(13,3);
cprintf("Name
: ");
cscanf("%s",&name);
fflush(stdin);
gotoxy(10,5);
cprintf("Address
: ");
gets(Add1);
fflush(stdin);
gotoxy(20,6);
gets(Add2);
gotoxy(12,7);
cprintf("Phone
: ");
cscanf("%s",Phone);
gotoxy(15,10);
cprintf("%s %s
%s",name,Add2,Phone);
getch();
}
Output:
Program:Arrange the strings in descending order by their ascii value using Pointers
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,temp,b;
char *anb,a[30];
clrscr();
printf("Enter the string:");
scanf("%s",anb);
printf("\nString:%s",anb);
for(i=0;(*anb)!=NULL;*(++anb))
{
a[i]=*anb;
i++;
}
a[i]=NULL;
printf("\nShow:%s",a);
j=i--;
for(b=0;b<=j;b++)
for(i=0;i<=j;i++)
{
if(a[i]>a[i+1])
a[i]=a[i];
else
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
a[--i]=NULL;
printf("\nOutput:%s",a);
getch();
}
Sample Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,temp,b;
char *anb,a[30];
clrscr();
printf("Enter the string:");
scanf("%s",anb);
printf("\nString:%s",anb);
for(i=0;(*anb)!=NULL;*(++anb))
{
a[i]=*anb;
i++;
}
a[i]=NULL;
printf("\nShow:%s",a);
j=i--;
for(b=0;b<=j;b++)
for(i=0;i<=j;i++)
{
if(a[i]>a[i+1])
a[i]=a[i];
else
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
a[--i]=NULL;
printf("\nOutput:%s",a);
getch();
}
Sample Output:
"Anna University Semester Credit Calculator"- Programming using C
Program:
#include<stdio.h>
#include<conio.h>
int mark(int,int);
void main()
{
int four,three,two,five,m4,m3,m2,m1,tot,totm;
float gpa;
clrscr();
printf("\nHow many Sub has 'credit 5': ");
scanf("%d",&five);
printf("\nHow many Sub has 'credit 4': ");
scanf("%d",&four);
printf("\nHow many Sub has 'credit 3': ");
scanf("%d",&three);
printf("\nHow many Lab Subjects: ");
scanf("%d",&two);
tot=(4*four)+(3*three)+(2*two)+(5*five);
m4=mark(4,four);
m3=mark(3,three);
m2=mark(2,two);
m1=mark(5,five);
totm=m4+m3+m2+m1;
gpa=(float)totm/tot;
printf("\n\n *********************** \n");
printf("* *\n");
printf("* Your GPA is: %f *\n",gpa);
printf("* *\n");
printf(" ***********************");
getch();
}
int mark(int grade,int num)
{
int j=1,n,c;
float i=0;
char ma;
c=0;
while(c<num)
{
fflush(0);
clrscr();
label_a:
printf("\nEnter the Grade you scored in the \n");
printf(" 'subject Number-%d' of credit '%d'= ",j,grade);
scanf("%c",&ma);
fflush(0);
switch(ma)
{
case 'S': { n=10; break; }
case 's': { n=10; break; }
case 'A': { n=9; break; }
case 'a': { n=9; break; }
case 'B': { n=8; break; }
case 'b': { n=8; break; }
case 'C': { n=7; break; }
case 'c': { n=7; break; }
case 'D': { n=6; break; }
case 'd': { n=6; break; }
case 'E': { n=5; break; }
case 'e': { n=5; break; }
default: { printf("\nEnter a valid grade\n");
goto label_a;
}
}
i=i+(grade*n);
c++;
j++;
}
return i;
}
Sample Output:
#include<stdio.h>
#include<conio.h>
int mark(int,int);
void main()
{
int four,three,two,five,m4,m3,m2,m1,tot,totm;
float gpa;
clrscr();
printf("\nHow many Sub has 'credit 5': ");
scanf("%d",&five);
printf("\nHow many Sub has 'credit 4': ");
scanf("%d",&four);
printf("\nHow many Sub has 'credit 3': ");
scanf("%d",&three);
printf("\nHow many Lab Subjects: ");
scanf("%d",&two);
tot=(4*four)+(3*three)+(2*two)+(5*five);
m4=mark(4,four);
m3=mark(3,three);
m2=mark(2,two);
m1=mark(5,five);
totm=m4+m3+m2+m1;
gpa=(float)totm/tot;
printf("\n\n *********************** \n");
printf("* *\n");
printf("* Your GPA is: %f *\n",gpa);
printf("* *\n");
printf(" ***********************");
getch();
}
int mark(int grade,int num)
{
int j=1,n,c;
float i=0;
char ma;
c=0;
while(c<num)
{
fflush(0);
clrscr();
label_a:
printf("\nEnter the Grade you scored in the \n");
printf(" 'subject Number-%d' of credit '%d'= ",j,grade);
scanf("%c",&ma);
fflush(0);
switch(ma)
{
case 'S': { n=10; break; }
case 's': { n=10; break; }
case 'A': { n=9; break; }
case 'a': { n=9; break; }
case 'B': { n=8; break; }
case 'b': { n=8; break; }
case 'C': { n=7; break; }
case 'c': { n=7; break; }
case 'D': { n=6; break; }
case 'd': { n=6; break; }
case 'E': { n=5; break; }
case 'e': { n=5; break; }
default: { printf("\nEnter a valid grade\n");
goto label_a;
}
}
i=i+(grade*n);
c++;
j++;
}
return i;
}
Sample Output:
Printf() Functions and its different Types with example
- printf sends formatted output to stdin
- cprintf sends formatted output to the text window on the screen
- fprintf sends formatted output to a stream
- sprintf sends formatted output to a string
- vfprintf sends formatted output to a stream, using an argument list
- vprintf sends formatted output to stdin, using an argument list
- vsprintf sends formatted output to a string, using an argument list
- int cprintf ( const char *format [, argument, ...]);
- int fprintf (FILE *stream, const char *format [, argument, ...]);
- int printf ( const char *format [, argument, ...]);
- int sprintf (char *buffer, const char *format [, argument, ...]);
- int vfprintf(FILE *stream, const char *format, va_list arglist);
- int vprintf ( const char *format, va_list arglist);
- int vsprintf(char *buffer, const char *format, va_list arglist);
- All these functions are declared in STDIO.H, except cprintf, which is declared in CONIO.H.
- Accept a series of arguments
- Apply to each argument a format specifier contained in the format string *format
- Output the formatted data (to the screen,a stream, stdin, or a string)
- the first format specifier to the first argument,
- thesecond specifier to the second argument,
- the third to the third, etc., to the end of the format.
On success,
- the ...printf functions return the number of bytes output
- cprintf returns the number of characters output
- sprintf does not include the terminating null byte in the count
- these functions return EOF
#include<stdio.h>
#include<conio.h>
void main()
{
int l;Sample Output:
clrscr();
printf("Enter the value:");
scanf("%d",&l);
printf("The Stored Value: %d",l);
getch();
}
Enter the Value: 10
The Stored Value: 10
The Stored Value: 10
Palindrome using pointer
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int c=0,d;
char str[30];char *a,*b;
clrscr();
printf("Enter the string:");
scanf("%s",str);
a=str;
for(a=str; *a!='\0';a++);
--a;
for(b=str; *b==*a; b++)
{
c=c+1;
--a;
}
d=strlen(str);
if(c==d)
printf("\nGiven String is Palindrome");
else
printf("\nGiven string is not palindrome");
getch();
}
Sample Output:
#include<conio.h>
#include<string.h>
void main()
{
int c=0,d;
char str[30];char *a,*b;
clrscr();
printf("Enter the string:");
scanf("%s",str);
a=str;
for(a=str; *a!='\0';a++);
--a;
for(b=str; *b==*a; b++)
{
c=c+1;
--a;
}
d=strlen(str);
if(c==d)
printf("\nGiven String is Palindrome");
else
printf("\nGiven string is not palindrome");
getch();
}
Sample Output:
Write a program to get two different array values , merge them and finally produce the ascending order sorting of the result. (array Concept)
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[25],b[25],n,g=0,m,i,j,c,temp;
clrscr();
printf("Enter the number of a: ");
scanf("%d",&n);
printf("\nEnter the no of b: ");
scanf("%d",&m);
printf("\nEnter a values\n");
for(i=1; i<=n ; i++)
scanf("%d",&a[i]);
printf("\nEnter b values\n");
for(j=1; j<=m ; j++)
{
scanf("%d",&b[j]);
a[i]=b[j];
i++;
}
printf("\nMerged values:\n");
c=n+m;
for(i=1; i<=c; i++)
printf("%d\t",a[i]);
while(g < c)
{
for(i=1; i<=c; i++)
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
else
a[i]=a[i];
++g;
}
printf("\nSorted Values:\n");
for(i=1; i<=c; i++)
printf("%d\t",a[i]);
getch();
}
Sample Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[25],b[25],n,g=0,m,i,j,c,temp;
clrscr();
printf("Enter the number of a: ");
scanf("%d",&n);
printf("\nEnter the no of b: ");
scanf("%d",&m);
printf("\nEnter a values\n");
for(i=1; i<=n ; i++)
scanf("%d",&a[i]);
printf("\nEnter b values\n");
for(j=1; j<=m ; j++)
{
scanf("%d",&b[j]);
a[i]=b[j];
i++;
}
printf("\nMerged values:\n");
c=n+m;
for(i=1; i<=c; i++)
printf("%d\t",a[i]);
while(g < c)
{
for(i=1; i<=c; i++)
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
else
a[i]=a[i];
++g;
}
printf("\nSorted Values:\n");
for(i=1; i<=c; i++)
printf("%d\t",a[i]);
getch();
}
Sample Output:
Subscribe to:
Posts (Atom)