Tuesday, 15 October 2019
Thursday, 29 August 2019
sorting by array(descending)
#include<stdio.h>
void sort(int m,int x[]);
main()
{
int i,marks[5]={13,3,54,554,300};
sort(5,marks);
printf("Marks after sorting\n");
for(i=0;i<5;i++)
printf("%4d",marks[i]);
printf("\n");
}
void sort(int m,int x[])
{
int i,j,t;
for(i=1;i<=m-1;i++)
for(j=1;j<=m-i;j++)
if(x[j-1]<=x[j])
{
t=x[j-1];
x[j-1]=x[j];
x[j]=t;
}
}
void sort(int m,int x[]);
main()
{
int i,marks[5]={13,3,54,554,300};
sort(5,marks);
printf("Marks after sorting\n");
for(i=0;i<5;i++)
printf("%4d",marks[i]);
printf("\n");
}
void sort(int m,int x[])
{
int i,j,t;
for(i=1;i<=m-1;i++)
for(j=1;j<=m-i;j++)
if(x[j-1]<=x[j])
{
t=x[j-1];
x[j-1]=x[j];
x[j]=t;
}
}
sorting by array(ascending)
#include<stdio.h>
void sort(int m,int x[]);
main()
{
int i,marks[5]={13,3,54,554,300};
sort(5,marks);
printf("Marks after sorting\n");
for(i=0;i<5;i++)
printf("%4d",marks[i]);
printf("\n");
}
void sort(int m,int x[])
{
int i,j,t;
for(i=1;i<=m-1;i++)
for(j=1;j<=m-i;j++)
if(x[j-1]>=x[j])
{
t=x[j-1];
x[j-1]=x[j];
x[j]=t;
}
}
void sort(int m,int x[]);
main()
{
int i,marks[5]={13,3,54,554,300};
sort(5,marks);
printf("Marks after sorting\n");
for(i=0;i<5;i++)
printf("%4d",marks[i]);
printf("\n");
}
void sort(int m,int x[])
{
int i,j,t;
for(i=1;i<=m-1;i++)
for(j=1;j<=m-i;j++)
if(x[j-1]>=x[j])
{
t=x[j-1];
x[j-1]=x[j];
x[j]=t;
}
}
c code for reverse an integer
#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
printf("Reversed Number = %d", reversedNumber);
return 0;
}
Wednesday, 10 July 2019
write a programe to find the number of vowels and consonants in a text string
#include<stdio.h>
#include<string.h>
int main()
{
char str[30];
int vowl=0,cons=0,i=0;
clrscr();
printf("Enter a string: ");
gets(str);
while(str[i]!='\0')
{
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')
vowl++;
else
cons++;
i++;
}
printf("\nNumber of Vowels=%d",vowl);
printf("\nNumber of Consonants=%d",cons);
}
#include<string.h>
int main()
{
char str[30];
int vowl=0,cons=0,i=0;
clrscr();
printf("Enter a string: ");
gets(str);
while(str[i]!='\0')
{
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')
vowl++;
else
cons++;
i++;
}
printf("\nNumber of Vowels=%d",vowl);
printf("\nNumber of Consonants=%d",cons);
}
write a programe to copy one string into another and count the number of characters copied
#include<stdio.h>
main()
{
char string1[80],string2[80];
int i;
printf("Enter a string:");
scanf("%s",string2);
for(i=0;string2[i];i++)
string1[i]=string2[i];
string1[i]='\0';
printf("%s\n",string1);
printf("The number of character=%d\n",i);
}
main()
{
char string1[80],string2[80];
int i;
printf("Enter a string:");
scanf("%s",string2);
for(i=0;string2[i];i++)
string1[i]=string2[i];
string1[i]='\0';
printf("%s\n",string1);
printf("The number of character=%d\n",i);
}
write a programe to read a line of text containing a series of words from the terminal
#include<stdio.h>
main()
{
char line[81],character;
int c;
c=0;
printf("Enter text:");
do
{
character=getchar();
line[c]=character;
c++;
}
while(character!= '\n');
c=c-1;
line[c]='\n';
printf("\n%s\n",line);
}
main()
{
char line[81],character;
int c;
c=0;
printf("Enter text:");
do
{
character=getchar();
line[c]=character;
c++;
}
while(character!= '\n');
c=c-1;
line[c]='\n';
printf("\n%s\n",line);
}
Tuesday, 9 July 2019
write a program to read a series of words from a terminal using scanf function
#include<stdio.h>
main()
{
char word1[40],word2[40],word3[40],word4[40];
printf("Enter text:\n");
scanf("%s %s %s %s",word1,word2,word3,word4);
printf("\n");
printf("word1=%s\n word2=%s\n word3=%s\n word4=%s",word1,word2,word3,word4);
}
main()
{
char word1[40],word2[40],word3[40],word4[40];
printf("Enter text:\n");
scanf("%s %s %s %s",word1,word2,word3,word4);
printf("\n");
printf("word1=%s\n word2=%s\n word3=%s\n word4=%s",word1,word2,word3,word4);
}
Monday, 22 April 2019
c program to read age of 15 person and total baby age,school age and adult age
#include<stdio.h>
main()
{
int n,i,s1=0,s2=0,s3=0;
printf("Enter the age of 15 person:");
do
{
scanf("%d",&n);
if(n>=0&&n<=5)
s1=s1+n;
else if(n>=6&&n<=17)
s2=s2+n;
else
s3=s3+n;
i++;
}
while(i<=15);
printf("Total Babys age=%d\n",s1);
printf("Total school students age=%d\n",s2);
printf("Total adult age=%d",s3);
}
main()
{
int n,i,s1=0,s2=0,s3=0;
printf("Enter the age of 15 person:");
do
{
scanf("%d",&n);
if(n>=0&&n<=5)
s1=s1+n;
else if(n>=6&&n<=17)
s2=s2+n;
else
s3=s3+n;
i++;
}
while(i<=15);
printf("Total Babys age=%d\n",s1);
printf("Total school students age=%d\n",s2);
printf("Total adult age=%d",s3);
}
Tuesday, 16 April 2019
Average for 5 numbers
#include<stdio.h>
#define N 5
main()
{
float number,sum,count,avg;
sum=0;
count=0;
while(count<N)
{
scanf("%f",&number);
sum=sum+number;
count=count+1;
}
avg=sum/count;
printf("Average=%f",avg);
}
#define N 5
main()
{
float number,sum,count,avg;
sum=0;
count=0;
while(count<N)
{
scanf("%f",&number);
sum=sum+number;
count=count+1;
}
avg=sum/count;
printf("Average=%f",avg);
}
Multification table by using array
#include<stdio.h>
#define ROWS 5
#define COLUMN 5
main()
{
int i,j,row,column,product[ROWS][COLUMN];
printf("MULTIFICATION TABLE\n\n");
printf(" ");
for(j=1;j<=COLUMN;j++)
printf("%4d",j);
printf("\n");
printf("-------------------------------------------\n");
for(i=1;i<=ROWS;i++)
{
row=i+1;
printf("%2d |",row);
for(j=i;j<=COLUMN;j++)
{
column=j;
product[i][j]=row*column;
printf("%4d",product[i][j]);
}
printf("\n");
}
}
#define ROWS 5
#define COLUMN 5
main()
{
int i,j,row,column,product[ROWS][COLUMN];
printf("MULTIFICATION TABLE\n\n");
printf(" ");
for(j=1;j<=COLUMN;j++)
printf("%4d",j);
printf("\n");
printf("-------------------------------------------\n");
for(i=1;i<=ROWS;i++)
{
row=i+1;
printf("%2d |",row);
for(j=i;j<=COLUMN;j++)
{
column=j;
product[i][j]=row*column;
printf("%4d",product[i][j]);
}
printf("\n");
}
}
Saturday, 30 March 2019
print the number which existence
#include<stdio.h>
main()
{
int i,n,number[5];
for(i=0;i<5;i++)
scanf("%d",&number[i]);
for(i=0;i<5;i++)
{
if(n==number[i])
printf("%d",number[i]);
}
}
main()
{
int i,n,number[5];
for(i=0;i<5;i++)
scanf("%d",&number[i]);
for(i=0;i<5;i++)
{
if(n==number[i])
printf("%d",number[i]);
}
}
arry2
#include<stdio.h>
main()
{
int i,number[5];
for(i=0;i<5;i++)
scanf("%d",&number[i]);
for(i=4;i>=0;i--)
printf("%d",number[i]);
}
main()
{
int i,number[5];
for(i=0;i<5;i++)
scanf("%d",&number[i]);
for(i=4;i>=0;i--)
printf("%d",number[i]);
}
arry1
#include<stdio.h>
main()
{
int i,number[5];
for(i=0;i<5;i++)
scanf("%d",&number[i]);
for(i=0;i<5;i++)
printf("%d",number[i]);
}
main()
{
int i,number[5];
for(i=0;i<5;i++)
scanf("%d",&number[i]);
for(i=0;i<5;i++)
printf("%d",number[i]);
}
print even number by arry
#include<stdio.h>
main()
{
int i,number[5];
for(i=0;i<5;i++)
scanf("%d",&number[i]);
for(i=0;i<5;i++)
{
if(number[i]%2==0)
printf("%d",number[i]);
}
}
main()
{
int i,number[5];
for(i=0;i<5;i++)
scanf("%d",&number[i]);
for(i=0;i<5;i++)
{
if(number[i]%2==0)
printf("%d",number[i]);
}
}
Wednesday, 27 March 2019
Write a C program to read weekday number and print weekday name using switch
#include<stdio.h>
main()
{
int Weekday_Number;
printf("Enter Weekday Number:");
scanf("%d",&Weekday_Number);
switch(Weekday_Number)
{
case 1:
printf("Saturday");
break;
case 2:
printf("Sunday");
break;
case 3:
printf("Monday");
break;
case 4:
printf("Tuesday");
break;
case 5:
printf("Wednesday");
break;
case 6:
printf("Thusday");
break;
case 7:
printf("Friday");
break;
default:
printf("error");
}
}
main()
{
int Weekday_Number;
printf("Enter Weekday Number:");
scanf("%d",&Weekday_Number);
switch(Weekday_Number)
{
case 1:
printf("Saturday");
break;
case 2:
printf("Sunday");
break;
case 3:
printf("Monday");
break;
case 4:
printf("Tuesday");
break;
case 5:
printf("Wednesday");
break;
case 6:
printf("Thusday");
break;
case 7:
printf("Friday");
break;
default:
printf("error");
}
}
Tuesday, 19 March 2019
* * * * * *
#include<stdio.h>
main()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
}
main()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
}
1 2 2 3 3 3
#include<stdio.h>
main()
{
int i=1,j;
while(i<=3)
{
j=1;
while(j<=i)
{
printf("%d",i);
j++;
}
printf("\n");
i++;
}
}
main()
{
int i=1,j;
while(i<=3)
{
j=1;
while(j<=i)
{
printf("%d",i);
j++;
}
printf("\n");
i++;
}
}
1 1 2 1 2 3
#include<stdio.h>
main()
{
int i=1,j;
while(i<=3)
{
j=1;
while(j<=i)
{
printf("%d",j);
j++;
}
printf("\n");
i++;
}
}
main()
{
int i=1,j;
while(i<=3)
{
j=1;
while(j<=i)
{
printf("%d",j);
j++;
}
printf("\n");
i++;
}
}
Factorial
#include<stdio.h>
main()
{
int i,n,fact=1;
printf("Enter an integer:");
scanf("%d",&n);
for(i=1;i<=n;i++)
fact=fact*i;
printf("Factorial of %d=%d",n,fact);
}
main()
{
int i,n,fact=1;
printf("Enter an integer:");
scanf("%d",&n);
for(i=1;i<=n;i++)
fact=fact*i;
printf("Factorial of %d=%d",n,fact);
}
LCM of twonumbers
#include<stdio.h>
main()
{
int n1,n2,lcm;
printf("Enter two integer:");
scanf("%d%d",&n1,&n2);
lcm=(n1>n2)?n1:n2;
while(1)
{
if(lcm%n1==0 && lcm%n2==0)
{
printf("LCM of %d and %d is %d",n1,n2,lcm);
break;
}
++lcm;
}
}
main()
{
int n1,n2,lcm;
printf("Enter two integer:");
scanf("%d%d",&n1,&n2);
lcm=(n1>n2)?n1:n2;
while(1)
{
if(lcm%n1==0 && lcm%n2==0)
{
printf("LCM of %d and %d is %d",n1,n2,lcm);
break;
}
++lcm;
}
}
GCD of two numbers
#include<stdio.h>
main()
{
int n1,n2,i,gcd;
printf("Enter two integer:");
scanf("%d%d",&n1,&n2);
for(i=1;i<=n1&&i<=n2;++i)
{
if(n1%i==0 && n2%i==0)
gcd=i;
}
printf("GCD of %d and %d is %d",n1,n2,gcd);
}
main()
{
int n1,n2,i,gcd;
printf("Enter two integer:");
scanf("%d%d",&n1,&n2);
for(i=1;i<=n1&&i<=n2;++i)
{
if(n1%i==0 && n2%i==0)
gcd=i;
}
printf("GCD of %d and %d is %d",n1,n2,gcd);
}
Fibonacci Series
#include <stdio.h>
main()
{
int fib1=0,fib2=1,fib3,limit,count=0;
printf("Enter the limit to generate the Fibonacci Series:");
scanf("%d",&limit);
printf("Fibonacci Series is ...\n");
printf(" %d",fib1);
printf(" %d",fib2);
count=2;
while(count<limit)
{
fib3=fib1+fib2;
count++;
printf(" %d",fib3);
fib1=fib2;
fib2=fib3;
}
}
main()
{
int fib1=0,fib2=1,fib3,limit,count=0;
printf("Enter the limit to generate the Fibonacci Series:");
scanf("%d",&limit);
printf("Fibonacci Series is ...\n");
printf(" %d",fib1);
printf(" %d",fib2);
count=2;
while(count<limit)
{
fib3=fib1+fib2;
count++;
printf(" %d",fib3);
fib1=fib2;
fib2=fib3;
}
}
sum of the series 1^3+2^3+3^3+-----+n^3
#include<stdio.h>
main()
{
int i,n,sum=0;
printf("Enter the max. value of series:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(i*i*i);
}
printf("Sum of the series=%d",sum);
}
main()
{
int i,n,sum=0;
printf("Enter the max. value of series:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(i*i*i);
}
printf("Sum of the series=%d",sum);
}
sum of the series 1^2+2^2+3^2+----+n^2
#include<stdio.h>
main()
{
int i,n,sum=0;
printf("Enter the max. value of series:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(i*i);
}
printf("Sum of the series=%d",sum);
}
main()
{
int i,n,sum=0;
printf("Enter the max. value of series:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(i*i);
}
printf("Sum of the series=%d",sum);
}
sum of the series 1+2+3+-----+n
#include<stdio.h>
main()
{
int i,n,sum=0;
printf("Enter the max. value of series:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(i);
}
printf("Sum of the series=%d",sum);
}
main()
{
int i,n,sum=0;
printf("Enter the max. value of series:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(i);
}
printf("Sum of the series=%d",sum);
}
prime number
#include <stdio.h>
main()
{
int i,n,p=0;
printf("Enter an integer:");
scanf("%d",&n);
for(i=1; i<=n;i++)
{
if(n%i==0)
p++;
}
if(p==2)
printf("%d is a prime number",n);
else
printf("%d is not a number",n);
}
main()
{
int i,n,p=0;
printf("Enter an integer:");
scanf("%d",&n);
for(i=1; i<=n;i++)
{
if(n%i==0)
p++;
}
if(p==2)
printf("%d is a prime number",n);
else
printf("%d is not a number",n);
}
Monday, 18 March 2019
Number of digits
#include<stdio.h>
main()
{
int n,count=0;
printf("Enter an integer:");
scanf("%d",&n);
while(n>0)
{
n=n/10;
count=count+1;
}
printf("Number of digits of %d=%d",n,count);
}
main()
{
int n,count=0;
printf("Enter an integer:");
scanf("%d",&n);
while(n>0)
{
n=n/10;
count=count+1;
}
printf("Number of digits of %d=%d",n,count);
}
sum of digits
#include <stdio.h>
main()
{
int p,q,r,sum=0;
printf("Enter an integer:");
scanf("%d",&p);
q=p;
while(q!=0)
{
r=q%10;
sum=sum+r;
q=q/10;
}
printf("Sum of digits of %d = %d\n",p,sum);
}
main()
{
int p,q,r,sum=0;
printf("Enter an integer:");
scanf("%d",&p);
q=p;
while(q!=0)
{
r=q%10;
sum=sum+r;
q=q/10;
}
printf("Sum of digits of %d = %d\n",p,sum);
}
Monday, 4 March 2019
write a program using switch and if statements to coma customerpute the net amount to be paid by customer
#include<stdio.h>
main()
{
float a;
char ch;
printf("Enter amount: ");
scanf("%f",&a);
printf("Enter item name for Mill 'm' for Handloom 'h': ");
scanf("%s",&ch);
if(a>0&&a<=100)
{
switch(ch)
{
case 'm':
{
printf("Amount =%.2f",a);
break;
}
case 'h':
{
printf("Amount = %.2f",(a-=(a*0.05)));
break;
}
}
}
else if(a>=101&&a<=200)
{
switch(ch)
{
case 'm':
{
printf("Amount = %.2f",(a-=(a*0.05)));
break;
}
case 'h':
{
printf("Amount = %.2f",(a-=(a*0.075)));
break;
}
}
}
else if(a>=201&&a<=300)
{
switch(ch)
{
case 'm':
{
printf("Amount = %.2f",(a-=(a*0.075)));
break;
}
case 'h':
{
printf("Amount = %.2f",(a-=(a*0.1)));
break;
}
}
}
else if(a>300)
{
switch(ch)
{
case 'm':
{
printf("Amount = %.2f",(a=(a*0.1)));
break;
}
case 'h':
{
printf("Amount = %.2f",(a=(a*0.15)));
break;
}
}
}
}
main()
{
float a;
char ch;
printf("Enter amount: ");
scanf("%f",&a);
printf("Enter item name for Mill 'm' for Handloom 'h': ");
scanf("%s",&ch);
if(a>0&&a<=100)
{
switch(ch)
{
case 'm':
{
printf("Amount =%.2f",a);
break;
}
case 'h':
{
printf("Amount = %.2f",(a-=(a*0.05)));
break;
}
}
}
else if(a>=101&&a<=200)
{
switch(ch)
{
case 'm':
{
printf("Amount = %.2f",(a-=(a*0.05)));
break;
}
case 'h':
{
printf("Amount = %.2f",(a-=(a*0.075)));
break;
}
}
}
else if(a>=201&&a<=300)
{
switch(ch)
{
case 'm':
{
printf("Amount = %.2f",(a-=(a*0.075)));
break;
}
case 'h':
{
printf("Amount = %.2f",(a-=(a*0.1)));
break;
}
}
}
else if(a>300)
{
switch(ch)
{
case 'm':
{
printf("Amount = %.2f",(a=(a*0.1)));
break;
}
case 'h':
{
printf("Amount = %.2f",(a=(a*0.15)));
break;
}
}
}
}
Sunday, 3 March 2019
sqrt of any number from 0 to 9.9
#include<stdio.h>
#include<math.h>
main()
{
float sq,sum,i,j;
printf("Number\t");
j=0.1;
loop3:
printf("%.2f\t",j);
j=j+0.1;
if(j<1)
goto loop3;
printf("\n");
i=1;
loop1:
printf("%.2f\t",i);
j=0.1;
loop:
sum=i+j;
sq=sqrt(sum);
printf("%.2f\t",sq);
j=j+0.1;
if(j<1)
goto loop;
i=i+1;
if(i<=9)
{
printf("\n");
goto loop1;
}
}
#include<math.h>
main()
{
float sq,sum,i,j;
printf("Number\t");
j=0.1;
loop3:
printf("%.2f\t",j);
j=j+0.1;
if(j<1)
goto loop3;
printf("\n");
i=1;
loop1:
printf("%.2f\t",i);
j=0.1;
loop:
sum=i+j;
sq=sqrt(sum);
printf("%.2f\t",sq);
j=j+0.1;
if(j<1)
goto loop;
i=i+1;
if(i<=9)
{
printf("\n");
goto loop1;
}
}
Saturday, 2 March 2019
eligible for the admission
#include<stdio.h>
main()
{
int Math,Phy,Chem,Total,Total_MP;
printf("Enter the number of Math:");
scanf("%d",&Math);
printf("Enter the number of Phy:");
scanf("%d",&Phy);
printf("Enter the number of Chem:");
scanf("%d",&Chem);
Total=Math+Phy+Chem;
Total_MP=Math+Phy;
if(Math>=60&&Phy>=50&&Chem>=40&&Total>=200)
printf("The candidate is eligible for the admission");
else
{
if(Total_MP>=150)
printf("The candidate is eligible for the admission");
else
printf("The candidate is not eligible for the admission");
}
}
main()
{
int Math,Phy,Chem,Total,Total_MP;
printf("Enter the number of Math:");
scanf("%d",&Math);
printf("Enter the number of Phy:");
scanf("%d",&Phy);
printf("Enter the number of Chem:");
scanf("%d",&Chem);
Total=Math+Phy+Chem;
Total_MP=Math+Phy;
if(Math>=60&&Phy>=50&&Chem>=40&&Total>=200)
printf("The candidate is eligible for the admission");
else
{
if(Total_MP>=150)
printf("The candidate is eligible for the admission");
else
printf("The candidate is not eligible for the admission");
}
}
A set of two linear eqn and find x1 and x2.
#include<stdio.h>
main()
{
int a,b,c,d,m,n;
float x1,x2;
printf("Enter the value of a,b,c,d,m,n:");
scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&m,&n);
if((a*d-c*b)!=0)
{
x1=(m*d-b*n)/(a*d-c*b);
x2=(n*a-m*c)/(a*d-c*b);
printf("\n The value of x1=%f\n The value of x2=%f",x1,x2);
}
else
printf("The division is not possible and result is an abrupt value");
}
main()
{
int a,b,c,d,m,n;
float x1,x2;
printf("Enter the value of a,b,c,d,m,n:");
scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&m,&n);
if((a*d-c*b)!=0)
{
x1=(m*d-b*n)/(a*d-c*b);
x2=(n*a-m*c)/(a*d-c*b);
printf("\n The value of x1=%f\n The value of x2=%f",x1,x2);
}
else
printf("The division is not possible and result is an abrupt value");
}
the programe evaluates the sqrt for 5 number using goto statement
#include<stdio.h>
#include<math.h>
main()
{
double x,y;
int count;
count=1;
printf("Enter FIVE real value in a line\n");
read:
scanf("%lf",&x);
printf("\n");
if(x<0)
printf("value %d is negative\n",count);
else
{
y=sqrt(x);
printf("%lf\t%lf\n",x,y);
}
count=count+1;
if(count<=5)
goto read;
printf("End of computation");
}
#include<math.h>
main()
{
double x,y;
int count;
count=1;
printf("Enter FIVE real value in a line\n");
read:
scanf("%lf",&x);
printf("\n");
if(x<0)
printf("value %d is negative\n",count);
else
{
y=sqrt(x);
printf("%lf\t%lf\n",x,y);
}
count=count+1;
if(count<=5)
goto read;
printf("End of computation");
}
Wednesday, 27 February 2019
find the number of and sum of all integer greater than 100 less than 200 are dibisible by 7
#include<stdio.h>
main()
{
int Num,Sum,Count;
Num=100;
Sum=Count=0;
Loop:
if(Num%7==0)
{
Sum=Sum+Num;
Count=Count+1;
}
Num=Num+1;
if(Num<=200)
goto Loop;
printf("Count:%d\n",Count);
printf("Sum:%d",Sum);
}
main()
{
int Num,Sum,Count;
Num=100;
Sum=Count=0;
Loop:
if(Num%7==0)
{
Sum=Sum+Num;
Count=Count+1;
}
Num=Num+1;
if(Num<=200)
goto Loop;
printf("Count:%d\n",Count);
printf("Sum:%d",Sum);
}
Another ways(by for loop):
#include<stdio.h>
main()
{
int i,sum,count;
sum=count=0;
for(i=100;i<=200;i++)
{
if(i%7==0)
{
sum=sum+i;
count=count+1;
}
}
printf("count=%d\n",count);
printf("sum=%d\n",sum);
}
#include<stdio.h>
main()
{
int i,sum,count;
sum=count=0;
for(i=100;i<=200;i++)
{
if(i%7==0)
{
sum=sum+i;
count=count+1;
}
}
printf("count=%d\n",count);
printf("sum=%d\n",sum);
}
determine odd or even with using else
#include<stdio.h>
main()
{
int x;
printf("Enter an integer:");
scanf("%d",&x);
if(x%2==0)
printf("The number entered is even");
else
printf("The number entered is odd");
}
main()
{
int x;
printf("Enter an integer:");
scanf("%d",&x);
if(x%2==0)
printf("The number entered is even");
else
printf("The number entered is odd");
}
determine odd or even without using else
#include<stdio.h>
main()
{
int x;
printf("Enter an integer:");
scanf("%d",&x);
if(x%2==0)
{
printf("The number entered is even");
exit(0);
}
printf("The number entered is odd");
}
main()
{
int x;
printf("Enter an integer:");
scanf("%d",&x);
if(x%2==0)
{
printf("The number entered is even");
exit(0);
}
printf("The number entered is odd");
}
Tuesday, 26 February 2019
c for x=a/(b-c)
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c,x;
printf("Enter the value of a & b & c:\n");
scanf("%f%f%f",&a,&b,&c);
x=a/(b-c);
printf("x=%f",x);
getch();
}
#include<conio.h>
void main()
{
float a,b,c,x;
printf("Enter the value of a & b & c:\n");
scanf("%f%f%f",&a,&b,&c);
x=a/(b-c);
printf("x=%f",x);
getch();
}
c fo div
#include<stdio.h>
main()
{
float a,b,div;
printf("Enter the value of a & b:\n");
scanf("%f%f",&a,&b);
div=a/b;
printf("div=%f",div);
getch();
}
main()
{
float a,b,div;
printf("Enter the value of a & b:\n");
scanf("%f%f",&a,&b);
div=a/b;
printf("div=%f",div);
getch();
}
c for smallest number
#include<stdio.h>
#include<conio.h>
main()
{
float a,b,c;
printf("Enter three values(separeted by space):");
scanf("%f%f%f",&a,&b,&c);
printf("The smallest value is:");
if(a<b)
{
if(b<c)
printf("%f",a);
else
printf("%f",c);
}
else
{
if(b<c)
printf("%f",b);
else
printf("%f",c);
}
getch();
}
#include<conio.h>
main()
{
float a,b,c;
printf("Enter three values(separeted by space):");
scanf("%f%f%f",&a,&b,&c);
printf("The smallest value is:");
if(a<b)
{
if(b<c)
printf("%f",a);
else
printf("%f",c);
}
else
{
if(b<c)
printf("%f",b);
else
printf("%f",c);
}
getch();
}
c for middle number
#include<stdio.h>
#include<conio.h>
main()
{
float a,b,c;
printf("Enter three values(separated by space):");
scanf("%f%f%f",&a,&b,&c);
printf("The Middle value is:");
if(a>b)
{
if(a>c)
{
if(b>c)
printf("%f",b);
else
printf("%f",c);
}
else
printf("%f",a);
}
else
{
if(b>c)
{
if(a>c)
printf("%f",a);
else
printf("%f",c);
}
else
printf("%f",b);
}
getch();
}
#include<conio.h>
main()
{
float a,b,c;
printf("Enter three values(separated by space):");
scanf("%f%f%f",&a,&b,&c);
printf("The Middle value is:");
if(a>b)
{
if(a>c)
{
if(b>c)
printf("%f",b);
else
printf("%f",c);
}
else
printf("%f",a);
}
else
{
if(b>c)
{
if(a>c)
printf("%f",a);
else
printf("%f",c);
}
else
printf("%f",b);
}
getch();
}
c for largest number
#include<stdio.h>
#include<conio.h>
main()
{
float a,b,c;
printf("Enter three values(separeted by space):");
scanf("%f%f%f",&a,&b,&c);
printf("The smallest value is:");
if(a<b)
{
if(a<c)
printf("%f",a);
else
printf("%f",c);
}
else
{
if(b<c)
printf("%f",b);
else
printf("%f",c);
}
getch();
}
#include<conio.h>
main()
{
float a,b,c;
printf("Enter three values(separeted by space):");
scanf("%f%f%f",&a,&b,&c);
printf("The smallest value is:");
if(a<b)
{
if(a<c)
printf("%f",a);
else
printf("%f",c);
}
else
{
if(b<c)
printf("%f",b);
else
printf("%f",c);
}
getch();
}
c code for else if ladder
#include<stdio.h>
main()
{
int units,custnum;
float charges;
printf("Enter Customer NO. & Units consumed\n");
scanf("%d%d",&custnum,&units);
if(units<=200)
charges=0.5*units;
else if(units<=400)
charges=100+0.65*(units-200);
else if(units<=600)
charges=230+0.40*(units-400);
else
charges=390+(units-600);
printf("\n\nCustomer No:%d & Charges=%.2f\n",custnum,charges);
}
main()
{
int units,custnum;
float charges;
printf("Enter Customer NO. & Units consumed\n");
scanf("%d%d",&custnum,&units);
if(units<=200)
charges=0.5*units;
else if(units<=400)
charges=100+0.65*(units-200);
else if(units<=600)
charges=230+0.40*(units-400);
else
charges=390+(units-600);
printf("\n\nCustomer No:%d & Charges=%.2f\n",custnum,charges);
}
Friday, 15 February 2019
c code for snake game
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include<time.h>
#include<ctype.h>
#include <time.h>
#include <windows.h>
#include <process.h>
#include <unistd.h>
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
int length;
int bend_no;
int len;
char key;
void record();
void load();
int life;
void Delay(long double);
void Move();
void Food();
int Score();
void Print();
void gotoxy(int x, int y);
void GotoXY(int x,int y);
void Bend();
void Boarder();
void Down();
void Left();
void Up();
void Right();
void ExitGame();
int Scoreonly();
struct coordinate{
int x;
int y;
int direction;
};
typedef struct coordinate coordinate;
coordinate head, bend[500],food,body[30];
int main()
{
char key;
Print();
system("cls");
load();
length=5;
head.x=25;
head.y=20;
head.direction=RIGHT;
Boarder();
Food(); //to generate food coordinates initially
life=3; //number of extra lives
bend[0]=head;
Move(); //initialing initial bend coordinate
return 0;
}
void Move()
{
int a,i;
do{
Food();
fflush(stdin);
len=0;
for(i=0;i<30;i++)
{
body[i].x=0;
body[i].y=0;
if(i==length)
break;
}
Delay(length);
Boarder();
if(head.direction==RIGHT)
Right();
else if(head.direction==LEFT)
Left();
else if(head.direction==DOWN)
Down();
else if(head.direction==UP)
Up();
ExitGame();
}while(!kbhit());
a=getch();
if(a==27)
{
system("cls");
exit(0);
}
key=getch();
if((key==RIGHT&&head.direction!=LEFT&&head.direction!=RIGHT)||(key==LEFT&&head.direction!=RIGHT&&head.direction!=LEFT)||(key==UP&&head.direction!=DOWN&&head.direction!=UP)||(key==DOWN&&head.direction!=UP&&head.direction!=DOWN))
{
bend_no++;
bend[bend_no]=head;
head.direction=key;
if(key==UP)
head.y--;
if(key==DOWN)
head.y++;
if(key==RIGHT)
head.x++;
if(key==LEFT)
head.x--;
Move();
}
else if(key==27)
{
system("cls");
exit(0);
}
else
{
printf("\a");
Move();
}
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void GotoXY(int x, int y)
{
HANDLE a;
COORD b;
fflush(stdout);
b.X = x;
b.Y = y;
a = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(a,b);
}
void sleep(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
void load(){
int row,col,r,c,q;
gotoxy(36,14);
printf("loading...");
gotoxy(30,15);
for(r=1;r<=20;r++){
sleep(200);//to display the character slowly
printf("%c",177);}
getch();
}
void Down()
{
int i;
for(i=0;i<=(head.y-bend[bend_no].y)&&len<length;i++)
{
GotoXY(head.x,head.y-i);
{
if(len==0)
printf("v");
else
printf("*");
}
body[len].x=head.x;
body[len].y=head.y-i;
len++;
}
Bend();
if(!kbhit())
head.y++;
}
void Delay(long double k)
{
Score();
long double i;
for(i=0;i<=(10000000);i++);
}
void ExitGame()
{
int i,check=0;
for(i=4;i<length;i++) //starts with 4 because it needs minimum 4 element to touch its own body
{
if(body[0].x==body[i].x&&body[0].y==body[i].y)
{
check++; //check's value increases as the coordinates of head is equal to any other body coordinate
}
if(i==length||check!=0)
break;
}
if(head.x<=10||head.x>=70||head.y<=10||head.y>=30||check!=0)
{
life--;
if(life>=0)
{
head.x=25;
head.y=20;
bend_no=0;
head.direction=RIGHT;
Move();
}
else
{
system("cls");
printf("All lives completed\nBetter Luck Next Time!!!\nPress any key to quit the game\n");
record();
exit(0);
}
}
}
void Food()
{
if(head.x==food.x&&head.y==food.y)
{
length++;
time_t a;
a=time(0);
srand(a);
food.x=rand()%70;
if(food.x<=10)
food.x+=11;
food.y=rand()%30;
if(food.y<=10)
food.y+=11;
}
else if(food.x==0)/*to create food for the first time coz global variable are initialized with 0*/
{
food.x=rand()%70;
if(food.x<=10)
food.x+=11;
food.y=rand()%30;
if(food.y<=10)
food.y+=11;
}
}
void Left()
{
int i;
for(i=0;i<=(bend[bend_no].x-head.x)&&len<length;i++)
{
GotoXY((head.x+i),head.y);
{
if(len==0)
printf("<");
else
printf("*");
}
body[len].x=head.x+i;
body[len].y=head.y;
len++;
}
Bend();
if(!kbhit())
head.x--;
}
void Right()
{
int i;
for(i=0;i<=(head.x-bend[bend_no].x)&&len<length;i++)
{
//GotoXY((head.x-i),head.y);
body[len].x=head.x-i;
body[len].y=head.y;
GotoXY(body[len].x,body[len].y);
{
if(len==0)
printf(">");
else
printf("*");
}
/*body[len].x=head.x-i;
body[len].y=head.y;*/
len++;
}
Bend();
if(!kbhit())
head.x++;
}
void Bend()
{
int i,j,diff;
for(i=bend_no;i>=0&&len<length;i--)
{
if(bend[i].x==bend[i-1].x)
{
diff=bend[i].y-bend[i-1].y;
if(diff<0)
for(j=1;j<=(-diff);j++)
{
body[len].x=bend[i].x;
body[len].y=bend[i].y+j;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if(len==length)
break;
}
else if(diff>0)
for(j=1;j<=diff;j++)
{
/*GotoXY(bend[i].x,(bend[i].y-j));
printf("*");*/
body[len].x=bend[i].x;
body[len].y=bend[i].y-j;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if(len==length)
break;
}
}
else if(bend[i].y==bend[i-1].y)
{
diff=bend[i].x-bend[i-1].x;
if(diff<0)
for(j=1;j<=(-diff)&&len<length;j++)
{
/*GotoXY((bend[i].x+j),bend[i].y);
printf("*");*/
body[len].x=bend[i].x+j;
body[len].y=bend[i].y;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if(len==length)
break;
}
else if(diff>0)
for(j=1;j<=diff&&len<length;j++)
{
/*GotoXY((bend[i].x-j),bend[i].y);
printf("*");*/
body[len].x=bend[i].x-j;
body[len].y=bend[i].y;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if(len==length)
break;
}
}
}
}
void Boarder()
{
system("cls");
int i;
GotoXY(food.x,food.y); /*displaying food*/
printf("F");
for(i=10;i<71;i++)
{
GotoXY(i,10);
printf("!");
GotoXY(i,30);
printf("!");
}
for(i=10;i<31;i++)
{
GotoXY(10,i);
printf("!");
GotoXY(70,i);
printf("!");
}
}
void Print()
{
//GotoXY(10,12);
printf("\tWelcome to the mini Snake game.(press any key to continue)\n");
getch();
system("cls");
printf("\tGame instructions:\n");
printf("\n-> Use arrow keys to move the snake.\n\n-> You will be provided foods at the several coordinates of the screen which you have to eat. Everytime you eat a food the length of the snake will be increased by 1 element and thus the score.\n\n-> Here you are provided with three lives. Your life will decrease as you hit the wall or snake's body.\n\n-> YOu can pause the game in its middle by pressing any key. To continue the paused game press any other key once again\n\n-> If you want to exit press esc. \n");
printf("\n\nPress any key to play game...");
if(getch()==27)
exit(0);
}
void record(){
char plname[20],nplname[20],cha,c;
int i,j,px;
FILE *info;
info=fopen("record.txt","a+");
getch();
system("cls");
printf("Enter your name\n");
scanf("%[^\n]",plname);
//************************
for(j=0;plname[j]!='\0';j++){ //to convert the first letter after space to capital
nplname[0]=toupper(plname[0]);
if(plname[j-1]==' '){
nplname[j]=toupper(plname[j]);
nplname[j-1]=plname[j-1];}
else nplname[j]=plname[j];
}
nplname[j]='\0';
//*****************************
//sdfprintf(info,"\t\t\tPlayers List\n");
fprintf(info,"Player Name :%s\n",nplname);
//for date and time
time_t mytime;
mytime = time(NULL);
fprintf(info,"Played Date:%s",ctime(&mytime));
//**************************
fprintf(info,"Score:%d\n",px=Scoreonly());//call score to display score
//fprintf(info,"\nLevel:%d\n",10);//call level to display level
for(i=0;i<=50;i++)
fprintf(info,"%c",'_');
fprintf(info,"\n");
fclose(info);
printf("wanna see past records press 'y'\n");
cha=getch();
system("cls");
if(cha=='y'){
info=fopen("record.txt","r");
do{
putchar(c=getc(info));
}while(c!=EOF);}
fclose(info);
}
int Score()
{
int score;
GotoXY(20,8);
score=length-5;
printf("SCORE : %d",(length-5));
score=length-5;
GotoXY(50,8);
printf("Life : %d",life);
return score;
}
int Scoreonly()
{
int score=Score();
system("cls");
return score;
}
void Up()
{
int i;
for(i=0;i<=(bend[bend_no].y-head.y)&&len<length;i++)
{
GotoXY(head.x,head.y+i);
{
if(len==0)
printf("^");
else
printf("*");
}
body[len].x=head.x;
body[len].y=head.y+i;
len++;
}
Bend();
if(!kbhit())
head.y--;
}
Subscribe to:
Posts (Atom)










