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

Monday, July 31, 2017

Tic tac toe game in C

#include<stdio.h> #include<conio.h> #include<stdlib.h> #include <windows.h> int board[10] = {2,2,2,2,2,2,2,2,2,2}; int turn = 1,flag = 0; int player,comp; void menu(); void go(int n); void start_game(); void check_draw(); void draw_board(); void player_first(); void put_X_O(char ch,int pos);  COORD coord={0,0}; // this is global variable                      ...

Simple quiz in C

#include<stdio.h> #include<conio.h> #include <windows.h> #include<ctype.h> #include<stdlib.h> #include<time.h> #include<dos.h> void displayscore()  {  char name[20];  float s;  FILE *f;  system("cls");  f=fopen("score.txt","r");  fscanf(f,"%s%f",&name,&s);  printf("\n\n\t\t ");  printf("\n\n\t\t %s has secured the Highest Score %.2f",name,s);  printf("\n\n\t\t...

Graphics in C

#include <windows.h> #include <stdio.h> void SetColorAndBackground(int ForgC, int BackC); void main() {     int f,b;         system("cls");         printf("Enter fore and background color code : ");         scanf("%d",&f);         SetColorAndBackground(f,16);   //color value range 0 up-to 256         printf("what is...

Check if a string is palindrome or not in C ?

#include<stdio.h> #include<string.h> void main() {     char word[50];     int i,j,l,c=0;     printf("Enter a word : ");     gets(word);     l=strlen(word);     for(i=0,j=l-1;i<l/2;i++,j--)     {         if(word[i]-word[j]>0)         {             c=1;            ...

Check if 3rd digit of a no. is prime or not in C ?

#include<stdio.h> int digitcount(int x) {     int r,c=0,i=0,y=x;     while(x!=0)     {         r=x%10;         x=x/10;         c++;     }     while(y!=0)     {         r=y%10;         y=y/10;         i++;         if(i==c-2)      ...

Application of struct in C

#include<stdio.h> #include<string.h> struct student {     char name[20];     int roll; }; void main() {   struct student s[10],t;   int i,n,j;   printf("Enter no. of students : ");   scanf("%d",&n);   for(i=0;i<n;i++)   {       printf("Enter %dth student's name and roll : ",i+1);       scanf("%s%d",s[i].name,&s[i].roll);   }   for(i=0;i<=n-2;i++)  ...

Check if a no. is prime or not in C ?

#include<stdio.h> int testprime(int ,int); void main() {     int n,prime;     printf("Enter a positive number : ");     scanf("%d",&n);     prime=testprime(n,n/2);     if(prime==1)         printf("%d is prime number.",n);     else         printf("%d is not a prime number.",n);     getch(); } int testprime(int num, int i) {  ...

Find HCF in C

#include<stdio.h> int hcf(int a,int b,int c) {     if(a%c==0 && b%c==0)     return c;     hcf(a,b,c-1); } void main() {     int h,x,y;     printf("Enter two numbers x and y : ");     scanf("%d%d",&x,&y);     if(x>y)     {         h=hcf(x,y,y);     }     else     {         h=hcf(y,x,x);  ...

Find fibonacci series in C

#include<stdio.h> void fibonacci(int); void main() {     int n,first=0,second=1;     printf("Enter the number of fibonacci series to be printed : ");     scanf("%d",&n);     printf("%d\t%d\t",first,second);     fibonacci(n);     getch(); } void fibonacci(int n) {     static int first=0,second=1,sum=0;     if(n-2>0)     {        ...

Find no. of vowels in a string in C

#include<stdio.h> void strcount(char []); void main() {   char str[50];   char c;   while(1)   {     printf("enter string\n");     //gets(str);     scanf("%s",str);     strcount(str);     printf("want to run again? press y/n \n");     scanf("%s",&c);     if(c == 'n' || c == 'N')         break;   } } void strcount(char str[50]) { int...

Find triangular no. in C

#include<stdio.h> void main() {     int n1,n2,i,t;     printf("Enter two numbers : ");     scanf("%d%d",&n1,&n2);     for(i=1;i<=n2;i++)     {         t = i*(i+1)/2;         if(t>=n1 && t<=n2)         {             printf("%d ",t);         }     }  ...

Find highest paid employee in C

#include<stdio.h> struct emp {     char name[50];     int salary; }; void main() {     int n,i,sum=0,avg;     struct emp e[20],ls,hs;     printf("Enter the number of employee : ");     scanf("%d",&n);     for(i=0;i<n;i++)     {         printf("Enter the name and salary of employees : ");         scanf("%s%d",e[i].name,&e[i].salary);  ...

Operations on complex numbers in C

#include<stdio.h> struct cmplx {     float real;     float img; }; struct cmplx add(struct cmplx x, struct cmplx y) {     struct cmplx z;     z.real = x.real + y.real;     z.img = x.img + y.img;     return(z); } struct cmplx sub(struct cmplx x, struct cmplx y) {     struct cmplx z;     z.real = x.real - y.real;     z.img = x.img - y.img;    ...

Arranging numbers in ascending and descending order in C

#include<stdio.h> void main() {     int x[5],i,j,temp;     for(i=0;i<=4;i++)     {         printf("Enter x[%d]th number : ",i);         scanf("%d",&x[i]);     }     for(i=0;i<=5-2;i++)     {         for(j=i+1;j<=5-1;j++)         {             if(x[i]>x[j])  ...

Find largest and smallest no. in C

#include<stdio.h> void main() {     int x[10],i,j,temp;     for(i=0;i<=9;i++)     {         printf("Enter %dth number : ",i+1);         scanf("%d",&x[i]);     }     for(i=0;i<=8;i++)     {         for(j=i+1;j<=9;j++)         {            if(x[i]<x[j])  ...

Display string after deleting vowels in it in C

#include<stdio.h> void main() {     char str[50],i;     printf("Enter a string : ");     gets(str);     printf("\nAfter deleting vowels :\t");     for(i=0;str[i]!='\0';i++)     {         if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u'||str[i]=='U')         {    ...

Count no. of words in a sentence in C

#include<stdio.h> void main() {     char str[50],i,wordcount=0;     printf("Enter a line of text : ");     gets(str);     for(i=0;str[i]!='\0';i++)     {         if(str[i]==32)         {            wordcount++;         }         if(str[i]==' ' && str[i-1]==' ')    ...

Sorting names in C

#include<stdio.h> //#include<string.h> void copystring(char txt1[], char txt2[]) {     int k;     for(k=0;txt2[k]!='\0';k++)     {         txt1[k]=txt2[k];     }     txt1[k]='\0'; } int comparestring(char str1[], char str2[]) {     int p,difference;     for(p=0;str1[p]!='\0' || str2[p]!='\0';p++)     {         difference=str1[p]-str2[p];  ...

Copying string 2 to string 1

#include<stdio.h> void copystr(char [],char []); void main() {     char str1[50],str2[50];     printf("Enter 1st string,str1 : ");     gets(str1);     printf("\nEnter 2nd string,str2 : ");     gets(str2);     copystr(str1,str2);     printf("\n\nAfter copying str2 in str1\n\nstr1 is %s ",str1);     getch(); } void copystr(char txt1[], char txt2[]) {    ...

Concatenation of two string in C

#include<stdio.h> void catstr(char [],char []); void main() {     char str1[50],str2[50];     printf("Enter 1st string (str1) :");     gets(str1);     printf("\nEnter 2nd string (str2) :");     gets(str2);     catstr(str1,str2);     printf("\n\nAfter concatenating(joining) these strings\n\nFinal string is ");     puts(str1);     getch(); } void catstr(char...