Saturday, 25 August 2018

SIMPLE PROGRAMS


//1.  STRUCTURES 10 ACCEPTING STUDENT DETAILS
#include <stdio.h>
struct student{
char name[50];
int roll;
float marks;
};
int main(){
struct student s[10];
int i;
printf("Enter information of students:\n");
for(i=0;i<10;++i)
{
s[i].roll=i+1;
printf("\nFor roll number %d\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
printf("Displaying information of students:\n\n");
for(i=0;i<10;++i)
{
printf("\nInformation for roll number %d:\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
}
return 0;
}

//2. C PROGRAM THAT ACCEPTS A CHARECTER AND DISPLAYS IT
#include<stdio.h>
void main(){
char a;
printf("\n Enter any Charecter ");
scanf("%c",&a);
if(a=='A'||a=='B'||a=='C'||a=='D'||a=='E'||a=='F'||a=='G'||a=='H'||a=='I'||a=='J'||a=='K'||a=='L'||a=='M'||a=='N'||a=='O'||a=='P'||a=='Q'||a=='R'||a=='S'||a=='T'||a=='U'||a=='V'||a=='W'||a=='X'||a=='Y'||a=='Z')
printf("ABCDEFGHIJKLMNOPQRSTUVWXYZ \n");
else if(a=='a'||a=='b'||a=='c'||a=='d'||a=='e'||a=='f'||a=='g'||a=='h'||a=='i'||a=='j'||a=='k'||a=='l'||a=='m'||a=='n'||a=='o'||a=='p'||a=='q'||a=='r'||a=='s'||a=='t'||a=='u'||a=='v'||a=='w'||a=='x'||a=='y'||a=='z')
printf("abcdefghijklmnopqrstuvwxyz \n");
else if(a=='1'||a=='2'||a=='3'||a=='4'||a=='5'||a=='6'||a=='7'||a=='8'||a=='9'||a=='0')
printf("1234567890 \n");
else printf(" special charecter is selected \n");
}

// 3. C PROGRAM THAT ACCEPTS A CHARECTER AND DISPLYS A-Z CHARECTERS
#include<stdio.h>
void main(){
char a;
printf("\n Enter any Charecter ");
scanf("%c",&a);
(a=='A'||a=='B'||a=='C'||a=='D'||a=='E'||a=='F'||a=='G'||a=='H'||a=='I'||a=='J'||a=='K'||a=='L'||a=='M'||a=='N'||a=='O'||a=='P'||a=='Q'||a=='R'||a=='S'||a=='T'||a=='U'||a=='V'||a=='W'||a=='X'||a=='Y'||a=='Z')?
printf("ABCDEFGHIJKLMNOPQRSTUVWXYZ \n"):printf("abcdefghijklmnopqrstuvwxyz \n"); }

// 4. PROGRAM THAT SCANS AND PRINTS A CHARECTER
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c",&c);

if( (c>='a'&& c<='z') || (c>='A'&& c<='Z'))
printf("%c is an alphabet.",c);
else
printf("%c is not an alphabet.",c);

return 0;
}

// 5. PROGRAM WHICH CHECKS AMSTRONG NUMBER OR NOT
#include <stdio.h>
int main()
{
int number, originalNumber, remainder, result = 0;

printf("Enter a three digit integer: ");
scanf("%d", &number);

originalNumber = number;

while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}

if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);

return 0;
}

// 6. AMSTRONG NUMBERS BETWEEN N NUMBERS
#include <stdio.h>
#include <math.h>

int main()
{
int n1, n2, i, temp1, temp2, remainder, n = 0, result = 0;

printf("Enter two numbers(intervals): ");
scanf("%d %d", &n1, &n2);
printf("Armstrong numbers between %d an %d are: ", n1, n2);

for(i = n1+1; i<n2; ++i)
{
temp2 = i;
temp1 = i;

// number of digits calculation
while (temp1 != 0)
{
temp1 /= 10;
++n;
}

// result contains sum of nth power of its digits
while (temp2 != 0)
{
remainder = temp2%10;
result += pow(remainder, n);
temp2 /= 10;
}

// checks if number i is equal to the sum of nth power of its digits
if (result == i) {
printf("%d ", i);
}

// resetting values to check Armstrong number for next iteration
n = 0;
result = 0;

}
return 0;
}

// 7. ASCII (AMERICAN STANDARD CODE FOR INFORMATION INTERCHANGE ) VALUE
#include<stdio.h>
void main(){
            char c;
            printf("Enter any chareter : ");
            scanf("%c",&c);
            printf(" ASCII value of your charecter is %c= %d ",c,c);
           
}

// 8. TO PRINT A TO Z CHARECTERS
#include <stdio.h>
int main()
{
char c;

for(c='A'; c<='Z'; ++c)
printf("%c ",c);

return 0;
}

// 9. AVERAGE FINDING
#include <stdio.h>
int main(){
int n, i;
float num[100], sum=0.0, average;
printf("Enter the numbers of data: ");
scanf("%d",&n);
while (n>100 || n<=0)
{
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d",&n);
}
for(i=0; i<n; ++i)
{
printf("%d. Enter number: ",i+1);
scanf("%f",&num[i]);
sum+=num[i];
}
average=sum/n;
printf("Average = %.2f",average);
return 0;
}

// 10. CONVERTING BINARY TO DECIMAL
#include <stdio.h>
#include <math.h>
int ConvertBinaryToDecimal(long long n);

int main()
{
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, ConvertBinaryToDecimal(n));
return 0;
}

int ConvertBinaryToDecimal(long long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
++i;
}
return decimalNumber;
}

//11. PRINT CHARECTER
#include <stdio.h>
void main ( ) {
            char a;
            printf ("\t enter any character \n");
            scanf ("%c",&a);
            printf ("\t the char is :%c \n", a);
            return 0;
}

// 12. SCAN AND PRINT CHARECTER
#include<stdio.h>
#include<string.h>
void main(){
char gender;
printf("ENTER 'M' AND 'F'\n" );
scanf("%c", &gender);
printf("YOU HAVE ENTERED : \n");
putchar(gender);

}


// 13. COUNTES VOWELS, CONSONANTS, NUMBERS AND WHITESPACES
#include<stdio.h>
int main(){
char line[150];
int i,v,c,ch,d,s,o;
o=v=c=ch=d=s=0;
printf("Enter a line of string:\n");
gets(line);
for(i=0;line[i]!='\0';++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
++v;
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
++c;
else if(line[i]>='0'&&c<='9')
++d;
else if (line[i]=='')
++s;
}
printf("Vowels: %d",v);
printf("\nConsonants: %d",c);
printf("\nDigits: %d",d);
printf("\nWhite spaces: %d",s);
return 0;
}

// 14. C PROGRAM TO COUNT THE NUMBER OF DIGITS
#include<stdio.h>
int main(){
int num,count=0;

printf("Enter a number: ");
scanf("%d",&num);

while(num){
num=num/10;
count++;
}
printf("Total digits is:  %d",count);
return 0;
}

// 15. CHECK EVEN NUMBER OR ODD NUMBER
#include <stdio.h>
int main()
{
int number;

printf("Enter an integer: ");
scanf("%d",&number);

// True if the number is perfectly divisible by 2
if(number%2 == 0)
printf("%d is even.", number);
else
printf("%d is odd.", number);

return 0;
}

// 16. FACTORIAL OF A GIVEN NUMBER
#include <stdio.h>
int main()
{
int n, i;
unsigned long long factorial = 1;

printf("Enter an integer: ");
scanf("%d",&n);

// show error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");

else
{
for(i=1; i<=n; ++i)
{
factorial *= i;              // factorial = factorial*i;
}
printf("Factorial of %d = %llu", n, factorial);
}

return 0;
}

// 17.  FIND FACTORS OF GIVEN NUMBER
#include <stdio.h>
int main()
{
int number,i;

printf("Enter a positive integer: ");
scanf("%d",&number);
printf("Factors of %d are: ", number);

for(i=1; i <= number; ++i)
{
if (number%i == 0)
{
printf("%d ",i);
}
}

return 0;
}

// 18.  FACTORIAL USING RECURSION (FUNCTION)
#include <stdio.h>
long int multiplyNumbers(int n);

int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n)
{
if (n >= 1)
return n*multiplyNumbers(n-1);
else
return 1;
}

// 19.  PRINT FIBONACCI SERIES
#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm = 0;

printf("Enter the number of terms: ");
scanf("%d",&n);

// displays the first two terms which is always 0 and 1
printf("Fibonacci Series: %d, %d, ", t1, t2);

// i = 3 because the first two terms are already dislpayed
for (i=3; i <= n; ++i)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
printf("%d, ",nextTerm);
}
return 0;
}

// 20. SOURCE CODE TO READ A TEXT OF STRING FROM A FILE

#include <stdio.h>
#include <stdlib.h> /* For exit() function*/
int main()
{
char c[1000];
FILE *fptr;
if ((fptr=fopen("AtoZ.c","r"))==NULL){
printf("Error! opening file");
exit(1);         /* Program exits if file pointer returns NULL. */
}
fscanf(fptr,"%[^\n]",c);
printf("Data from file:\n%s",c);
fclose(fptr);
return 0;
}

// 21. FLOAT VALUE SCAN AND PRINT
#include <stdio.h>
void main ( ) {
            float f;
            printf ("enter float value");
            scanf ("%f",&f);
            printf ("the result is %f", f);
            return 0;
}

// 22. PRINTS FLOYDS TRIANGEL
#include <stdio.h>
int main()
{
int rows, i, j, number= 1;

printf("Enter the number of rows: ");
scanf("%d",&rows);

for(i=1; i <= rows; i++)
{
for(j=1; j <= i; ++j)
{
printf("%d ", number);
++number;
}

printf("\n");
}

return 0;
}

// 23. FINDS FREQUENCY OCCURENCE OF CHARECTER
#include <stdio.h>
int main(){
char c[1000],ch;
int i,count=0;
printf("Enter a string: ");
gets(c);
printf("Enter a character to find frequency: ");
scanf("%c",&ch);
for(i=0;c[i]!='\0';++i)
{
if(ch==c[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
}

// 24.  FIND GCD (GREATEST COMMON DIVISIOR ) PROGRAM
#include <stdio.h>
int main()
{
int n1, n2, i, gcd;

printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);

for(i=1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
}

printf("G.C.D of %d and %d is %d", n1, n2, gcd);

return 0;
}

// 25. PRINTS HELLO
main (){
            printf ( " Hello! world welcome to c");
}

// 26. PRINTS LARGEST VALUE IN AN ARRAY
#include <stdio.h>
int main(){
int i,n;
float arr[100];
printf("Enter total number of elements(1 to 100): ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;++i)  /* Stores number entered by user. */
{
printf("Enter Number %d: ",i+1);
scanf("%f",&arr[i]);
}
for(i=1;i<n;++i)  /* Loop to store largest number to arr[0] */
{
if(arr[0]<arr[i]) /* Change < to > if you want to find smallest element*/
arr[0]=arr[i];
}
printf("Largest element = %.2f",arr[0]);
return 0;
}

// 27. PRINTS LARGEST VALUE IN A DYNAMIC ARRAY
#include <stdio.h>
#include <stdlib.h>
int main(){
int i,n;
float *data;
printf("Enter total number of elements(1 to 100): ");
scanf("%d",&n);
data=(float*)calloc(n,sizeof(float));  /* Allocates the memory for 'n' elements */
if(data==NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}
printf("\n");
for(i=0;i<n;++i)  /* Stores number entered by user. */
{
printf("Enter Number %d: ",i+1);
scanf("%f",data+i);
}
for(i=1;i<n;++i)  /* Loop to store largest number at address data */
{
if(*data<*(data+i)) /* Change < to > if you want to find smallest number */
*data=*(data+i);
}
printf("Largest element = %.2f",*data);
return 0;
}

// 28. FIND LCM OF A GIVEN NUMBER
#include <stdio.h>
int main()
{
int n1, n2, minMultiple;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);

// maximum number between n1 and n2 is stored in minMultiple
minMultiple = (n1>n2) ? n1 : n2;

// Always true
while(1)
{
if( minMultiple%n1==0 && minMultiple%n2==0 )
{
printf("The LCM of %d and %d is %d.", n1, n2,minMultiple);
break;
}
++minMultiple;
}
return 0;
}

// 29.  PROGRAM TO CHECK LEAP YEAR OR NOT
int main()
{
int year;

printf("Enter a year: ");
scanf("%d",&year);

if(year%4 == 0)
{
if( year%100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);

return 0;
}

// 30.  PRINTS SIZES OF INT, LONG, LONG LONG, DOUBLE, LONG DOUBLE
#include <stdio.h>
int main()
{
int a;
long b;
long long c;

double e;
long double f;


printf("Size of int = %ld bytes \n", sizeof(a));
printf("Size of long = %ld bytes\n", sizeof(b));
printf("Size of long long = %ld bytes\n", sizeof(c));

printf("Size of double = %ld bytes\n", sizeof(e));
printf("Size of long double = %ld bytes\n", sizeof(f));

return 0;
}

//31. MATRIX ADDITION
#include <stdio.h>
int main(){
int r,c,a[100][100],b[100][100],sum[100][100],i,j;
printf("Enter number of rows (between 1 and 100): ");
scanf("%d",&r);
printf("Enter number of columns (between 1 and 100): ");
scanf("%d",&c);
printf("\nEnter elements of 1st matrix:\n");

/* Storing elements of first matrix entered by user. */

for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}

/* Storing elements of second matrix entered by user. */

printf("Enter elements of 2nd matrix:\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}

/*Adding Two matrices */

for(i=0;i<r;++i)
for(j=0;j<c;++j)
sum[i][j]=a[i][j]+b[i][j];

/* Displaying the resultant sum matrix. */

printf("\nSum of two matrix is: \n\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d   ",sum[i][j]);
if(j==c-1)
printf("\n\n");
}

return 0;
}

// 32. MATRIX MULTIPLICATION

#include <stdio.h>
int main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&r2, &c2);

/* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */
while (c1!=r2)
{
printf("Error! column of first matrix not equal to row of second.\n\n");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&r2, &c2);
}

/* Storing elements of first matrix. */
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}

/* Storing elements of second matrix. */
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}

/* Initializing elements of matrix mult to 0.*/
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
mult[i][j]=0;
}

/* Multiplying matrix a and b and storing in array mult. */
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
mult[i][j]+=a[i][k]*b[k][j];
}

/* Displaying the multiplication of two matrix. */
printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d  ",mult[i][j]);
if(j==c2-1)
printf("\n\n");
}
return 0;
}

// 33. PRINTS MULTIPLICATION TABLE

#include <stdio.h>
int main()
{
int n, i;

printf("Enter an integer: ");
scanf("%d",&n);

for(i=1; i<=10; ++i)
{
printf("%d * %d = %d \n", n, i, n*i);
}

return 0;
}

// 34. Number Count In A Given Number
#include <stdio.h>
int main()
{
long long n;
int count=0;

printf("Enter an integer: ");
scanf("%lld", &n);

while(n!=0)
{
n /= 10;             // n = n/10
++count;
}

printf("Number of digits: %d",count);
}



/* 35. C PROGRAMMING SOURCE CODE TO CONVERT EITHER OCTAL TO DECIMAL OR DECIMAL TO OCTAL ACCORDING TO DATA ENTERED BY USER. */

#include <stdio.h>
#include <math.h>
int decimal_octal(int n);
int octal_deciaml(int n);
int main()
{
int n;
char c;
printf("Instructions:\n");
printf("1. Enter alphabet 'o' to convert decimal to octal.\n");
printf("2. Enter alphabet 'd' to convert octal to decimal.\n");
scanf("%c",&c);
if (c =='d' || c == 'D')
{
printf("Enter an octal number: ");
scanf("%d", &n);
printf("%d in octal = %d in decimal", n, octal_decimal(n));
}
if (c =='o' || c == 'O')
{
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %d in octal", n, decimal_octal(n));
}
return 0;
}

int decimal_octal(int n) /* Function to convert decimal to octal */
{
int rem, i=1, octal=0;
while (n!=0)
{
rem=n%8;
n/=8;
octal+=rem*i;
i*=10;
}
return octal;
}

int octal_decimal(int n) /* Function to convert octal to decimal */
{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(8,i);
++i;
}
return decimal;
}

// 36. PRINTS OWN FILE CONTENT

#include <stdio.h>
int main(){
printf("%s",__FILE__);
}

// 37. CHECK PALINDROME NUMBER
#include <stdio.h>
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;

printf("Enter an integer: ");
scanf("%d", &n);

originalInteger = n;

// reversed integer is stored in variable
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}

// palindrome if orignalInteger and reversedInteger is equal
if(originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%d is not a palindrome.", originalInteger);

return 0;
}

// 38. POWER VALUES COUNT
#include <stdio.h>
int main()
{
int base, exponent;

long long result = 1;

printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter an exponent: ");
scanf("%d", &exponent);

while (exponent != 0)
{
result *= base;
--exponent;
}

printf("Answer = %lld", result);

return 0;
}



// 39. PRIME BETWEEN NUMBERS

#include <stdio.h>
int main()
{
int n1, n2, i, flag;
printf("Enter two numbers(intevals): ");
scanf("%d %d", &n1, &n2);

printf("Prime numbers between %d and %d are: ", n1, n2);

while (n1 < n2)
{
flag=0;

for(i=2; i<=n1/2; ++i)
{
if(n1%i == 0)
{
flag=1;
break;
}
}

if (flag == 0)
printf("%d ",n1);

++n1;
}

return 0;
}

// 40. PRIME NUMBER CHECK

#include <stdio.h>
int main()
{
int n, i, flag = 0;

printf("Enter a positive integer: ");
scanf("%d",&n);

for(i=2; i<=n/2; ++i)
{
// condition for nonprime number
if(n%i==0)
{
flag=1;
break;
}
}

if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);

return 0;
}

// 41.  PROGRAM TO PRINT HALF PYRAMID USING *
*
* *
* * *
* * * *
* * * * *

#include <stdio.h>
int main()
{
int i, j, rows;

printf("Enter the number of rows: ");
scanf("%d",&rows);

for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}

// 42. PROGRAM TO PRINT HALF PYRAMID A USING NUMBERS

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include <stdio.h>
int main()
{
int i, j, rows;

printf("Enter the number of rows: ");
scanf("%d",&rows);

for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}

// 43. PROGRAM TO PRINT HALF PYRAMID USING ALPHABETS
A
B B
C C C
D D D D
E E E E E
#include <stdio.h>
int main()
{
int i, j;
char input, alphabet = 'A';

printf("Enter the uppercase character you want to print in last row: ");
scanf("%c",&input);

for(i=1; i <= (input-'A'+1); ++i)
{
for(j=1;j<=i;++j)
{
printf("%c", alphabet);
}
++alphabet;

printf("\n");
}
return 0;
}

// 44.  INVERTED HALF PYRAMID USING *

* * * * *
* * * *
* * *
* *
*

#include <stdio.h>
int main()
{
int i, j, rows;

printf("Enter the number of rows: ");
scanf("%d",&rows);

for(i=rows; i>=1; --i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}

return 0;
}


// 45. INVERTED HALF PYRAMID USING NUMBERS

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

#include <stdio.h>
int main()
{
int i, j, rows;

printf("Enter the number of rows: ");
scanf("%d",&rows);

for(i=rows; i>=1; --i)
{
for(j=1; j<=i; ++j)
{
printf("%d ",j);
}
printf("\n");
}

return 0;
}

// 46. PROGRAM TO PRINT FULL PYRAMID USING *

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

#include <stdio.h>
int main()
{
int i, space, rows, k=0;

printf("Enter the number of rows: ");
scanf("%d",&rows);

for(i=1; i<=rows; ++i, k=0)
{
for(space=1; space<=rows-i; ++space)
{
printf("");
}

while(k != 2*i-1)
{
printf("* ");
++k;
}

printf("\n");
}

return 0;
}

// 47. PROGRAM TO PRINT PYRAMID USING NUMBERS

1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

#include <stdio.h>
int main()
{
int i, space, rows, k=0, count = 0, count1 = 0;

printf("Enter the number of rows: ");
scanf("%d",&rows);

for(i=1; i<=rows; ++i)
{
for(space=1; space <= rows-i; ++space)
{
printf("");
++count;
}

while(k != 2*i-1)
{
if (count <= rows-1)
{
printf("%d ", i+k);
++count;
}
else
{
++count1;
printf("%d ", (i+k-2*count1));
}
++k;
}
count1 = count = k = 0;

printf("\n");
}
return 0;
}

// 48. INVERTED FULL PYRAMID USING *

* * * * * * * * *
* * * * * * *
* * * * *
* * *
*

#include<stdio.h>
int main()
{
int rows, i, j, space;

printf("Enter the number of rows: ");
scanf("%d",&rows);

for(i=rows; i>=1; --i)
{
for(space=0; space < rows-i; ++space)
printf("");

for(j=i; j <= 2*i-1; ++j)
printf("* ");

for(j=0; j < i-1; ++j)
printf("* ");

printf("\n");
}

return 0;
}

// 49. PRINT PASCAL'S TRIANGLE

1
1   1
1   2   1
1   3   3    1
1  4    6   4   1
1  5   10   10  5   1

#include <stdio.h>
int main()
{
int rows, coef = 1, space, i, j;

printf("Enter number of rows: ");
scanf("%d",&rows);

for(i=0; i<rows; i++)
{
for(space=1; space <= rows-i; space++)
printf("");

for(j=0; j <= i; j++)
{
if (j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j;

printf("%4d", coef);
}
printf("\n");
}

return 0;
}

// 50. QUADRATIC ROOTS OF A GIVEN EQUATION

#include <stdio.h>
#include <math.h>

int main()
{
double a, b, c, determinant, root1,root2, realPart, imaginaryPart;

printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);

determinant = b*b-4*a*c;

// condition for real and different roots
if (determinant > 0)
{
// sqrt() function returns square root
root1 = (-b+sqrt(determinant))/(2*a);
root2 = (-b-sqrt(determinant))/(2*a);

printf("root1 = %.2lf and root2 = %.2lf",root1 , root2);
}

//condition for real and equal roots
else if (determinant == 0)
{
root1 = root2 = -b/(2*a);

printf("root1 = root2 = %.2lf;", root1);
}

// if roots are not real
else
{
realPart = -b/(2*a);
imaginaryPart = sqrt(-determinant)/(2*a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imaginaryPart, realPart, imaginaryPart);
}

return 0;
}  

// 51. QUOTIENT AND REMAINDER
#include <stdio.h>
int main(){

int dividend, divisor, quotient, remainder;

printf("Enter dividend: ");
scanf("%d", &dividend);

printf("Enter divisor: ");
scanf("%d", &divisor);

// Computes quotient
quotient = dividend/divisor;

// Computes remainder
remainder = dividend%divisor;

printf("Quotient = %d\n",quotient);
printf("Remainder = %d",remainder);

return 0;
}
// 52. FIND REVERSE OF A GIVEN NUMBER

#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;
}


/* 53. EXAMPLE TO REVERSE A SENTENCE ENTERED BY USER WITHOUT USING STRINGS */

#include <stdio.h>
void Reverse();
int main()
{
printf("Enter a sentence: ");
Reverse();
return 0;
}
void Reverse()
{
char c;
scanf("%c",&c);
if( c != '\n')
{
Reverse();
printf("%c",c);
}
}

// 54. SIZES OF DATA TYPES
#include <stdio.h>
#include <limits.h>
int main()
{
printf("Storage size for int : %d \n", sizeof(int));
printf("Storage size for float : %d \n", sizeof(float));
printf("Storage size for char : %d \n", sizeof(char));
printf("Storage size for short : %d \n", sizeof(short));
printf("Storage size for long : %d \n", sizeof(long));
printf("Storage size for double : %d \n", sizeof(double));
return 0;
}

// 55. SIZE OF DATATYPES
#include <stdio.h>
int main()
{
int integerType;
float floatType;
double doubleType;
char charType;

// Sizeof operator is used to evaluate the size of a variable
printf("Size of int: %ld bytes\n",sizeof(integerType));
printf("Size of float: %ld bytes\n",sizeof(floatType));
printf("Size of double: %ld bytes\n",sizeof(doubleType));
printf("Size of char: %ld byte\n",sizeof(charType));

return 0;
}



/* 56.  SOURCE CODE TO CALCULATE STANDARD DEVIATION. */
#include <stdio.h>
#include <math.h>
float standard_deviation(float data[], int n);
int main()
{
int n, i;
float data[100];
printf("Enter number of datas( should be less than 100): ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0; i<n; ++i)
scanf("%f",&data[i]);
printf("\n");
printf("Standard Deviation = %.2f", standard_deviation(data,n));
return 0;
}
float standard_deviation(float data[], int n)
{
float mean=0.0, sum_deviation=0.0;
int i;
for(i=0; i<n;++i)
{
mean+=data[i];
}
mean=mean/n;
for(i=0; i<n;++i)
sum_deviation+=(data[i]-mean)*(data[i]-mean);
return sqrt(sum_deviation/n);          
}

// 57.  STUDENT STRUCTURES
#include <stdio.h>
struct student{
char name[50];
int roll;
float marks;
};
int main(){
struct student s;
printf("Enter information of students:\n\n");
printf("Enter name: ");
scanf("%s",s.name);
printf("Enter roll number: ");
scanf("%d",&s.roll);
printf("Enter marks: ");
scanf("%f",&s.marks);
printf("\nDisplaying Information\n");
printf("Name: %s\n",s.name);
printf("Roll: %d\n",s.roll);
printf("Marks: %.2f\n",s.marks);
return 0;
}

// 58. STRING PRINT
#include<stdio.h>
void main ( ) {
            char s[20];
            printf ("\t enter any string:\n");
            scanf ("%s",s);
            printf ("\t the string is :%s", s);
            return 0;
}

// 59. STRING SORTING
#include<stdio.h>
#include <string.h>
int main(){
int i,j;
char str[10][50],temp[50];
printf("Enter 10 words:\n");
for(i=0;i<10;++i)
gets(str[i]);
for(i=0;i<9;++i)
for(j=i+1;j<10 ;++j){
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("In lexicographical order: \n");
for(i=0;i<10;++i){
puts(str[i]);
}
return 0;
}

// 60. STUDENT DETAILS IN C (DIFFERENT DATA TYPES)
#include<stdio.h>
void main()
{
int no;
char name[20];
float marks;
char gender;

printf("\n Enter the number");
scanf("%d",&no);
printf("\n Enter the name ");
scanf("%s", name);
printf("\n Enter the gender ");
scanf("%c",&gender);
scanf("%c",&gender);
printf("\n Enter the marks");
scanf("%f", &marks);
printf("\t\t\t *************** STUDENT DETAILS ARE *********** \n");
printf("\n \t\t\t NUMBER IS %d", no);
printf("\n \t\t\t NAME IS %s ", name);
printf("\n \t\t\t GENDER IS %c", gender);
printf("\n \t\t\t MARKS ARE %f", marks);
}

// 61.  PRINTS SUM OF NUMBERS UP TO N
#include <stdio.h>
int main()
{
int n, i, sum = 0;

printf("Enter a positive integer: ");
scanf("%d",&n);

for(i=1; i <= n; ++i)
{
sum += i;   // sum = sum+i;
}

printf("Sum = %d",sum);

return 0;
}

// 62. INTER CHANGING VALUES
#include <stdio.h>
int main()
{
double firstNumber, secondNumber, temporaryVariable;

printf("Enter first number: ");
scanf("%lf", &firstNumber);

printf("Enter second number: ");
scanf("%lf",&secondNumber);

// Value of firstNumber is assigned to temporaryVariable
temporaryVariable = firstNumber;

// Value of secondNumber is assigned to firstNumber
firstNumber = secondNumber;

// Value of temporaryVariable (which contains the initial value of firstNumber) is assigned to secondNumber
secondNumber = temporaryVariable;

printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);
printf("After swapping, secondNumber = %.2lf", secondNumber);

return 0;
}


// 62. Performs addition, subtraction, multiplication or division depending the input from user
# include <stdio.h>
int main() {
char operator;
double firstNumber,secondNumber;

printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);

printf("Enter two operands: ");
scanf("%lf %lf",&firstNumber, &secondNumber);

switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber);
break;

case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber);
break;

case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber);
break;

case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);
break;

// operator doesn't match any case constant (+, -, *, /)
default:
printf("Error! operator is not correct");
}

return 0;
}

// 63. TRANSPOSE OF A MATRIX
#include <stdio.h>
int main()
{
int a[10][10], trans[10][10], r, c, i, j;
printf("Enter rows and column of matrix: ");
scanf("%d %d", &r, &c);

/* Storing element of matrix entered by user in array a[][]. */
printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
/* Displaying the matrix a[][] */
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d  ",a[i][j]);
if(j==c-1)
printf("\n\n");
}

/* Finding transpose of matrix a[][] and storing it in array trans[][]. */
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
trans[j][i]=a[i][j];
}

/* Displaying the transpose,i.e, Displaying array trans[][]. */
printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d  ",trans[i][j]);
if(j==r-1)
printf("\n\n");
}
return 0;
}

// 64. VOWELS AND CONSONANTS
#include <stdio.h>
int main()
{
char c;
int isLowercaseVowel, isUppercaseVowel;

printf("Enter an alphabet: ");
scanf("%c",&c);

// evaluates to 1 (true) if c is a lowercase vowel
isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

// evaluates to 1 (true) if c is an uppercase vowel
isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

// evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true
if (isLowercaseVowel || isUppercaseVowel)
printf("%c is a vowel.",c);
else
printf("%c is a consonant.",c);
return 0;
}

No comments:

Post a Comment