Vous êtes sur la page 1sur 6

// Program : The one with the votes

#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=0,i;
char c;
for(i=1;i<=100;i++)
{
clrscr();
printf(" TOTAL VOTES :\n A : %d \n B : %d",a,b);
printf("\n Vote no : %d ",i);
printf("\n Press 'a' to vote for 'A' or 'b' to vote for 'B' \n(any other keypres
s would be considered invalid) \n");
c = getch();
if(c == 'a' || c == 'A') a++;
if(c == 'b' || c == 'B') b++;
}
if(a>66)printf("\n\nA is the winner of the election");
else if(b>66) printf("\n\nB is the winner of the election");
else printf(" Candidature failed ");
getch();
}
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
______________________
// Program : a + 1 +b +2 ...
// Note : I have assumed "a+1" as 1 term/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("Enter no. of terms : ");
scanf("%d",&n);
i=0;
do
{
printf("%c + %d + ",'a' + i%26 , i+1);
}
while(++i<n-1);
printf("%c + %d",'a' + i%26 , i+1);
getch();
}
// i%26 ensures that once the 1st set of 26 alphabets is over , the sequence is
restarted
// Loop is used only up to second last term, the last term is printed separately
to avoid the extra plus sign at the end
________________________________________________________________________________
________________________________________________________________________________

___
sum of odd nos frm 1 to 100
#include<stdio.h>
#include<conio.h>
int add(int i)
{
int sum;
if(i>1)
sum = add(i-2) + i;
else
sum = 1;
printf(" %d ",i);
return sum;
}
void main()
{
int sum;
clrscr();
sum = add(99);
printf("\nThe sum of all numbers is %d ",sum);
getch();
}
________________________________________________________________________________
________________________________________________________________________________
___
//The one with summation of factorial series. 1! + 2! + 3! + .....
#include<stdio.h>
#include<conio.h>
long int fact(int n)
{
if(n>1)
return fact(n-1)*n;
else
return 1;
}
long int sum(int n)
{
if(n>1)
return sum(n-1) + fact(n);
else
return 1;
}
void main()
{
int i;
clrscr();
printf("Enter the no. of terms to find sum of : ");
scanf("%d",&i);
printf("The sum is %d",sum(i));
getch();
}
________________________________________________________________________________
________________________________________________________________________________
________
//The one with alternate + - factorial series (array) 1! - 2! + 3! - 4! + .....
#include<stdio.h>

#include<conio.h>
long int fact(int n)
{
if(n>1)
return fact(n-1)*n;
else
return 1;
}
void main()
{
int i,n
long int a[25],sum=0;
clrscr();
printf("Enter the no. of terms to find sum of (max 25) : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i] = fact(i+1);
if(i%2==1)
a[i] = -a[i];
}
for(i=0;i<n;i++)
sum = sum + a[i] ;
printf("The sum is %d",sum);
getch();
}
________________________________________________________________________________
________________________________________________________________________________
________________
//The one with a number raised to itself (array) 1 + 2^2 + 3^3 + .....
#include<stdio.h>
#include<conio.h>
long int powfunc(int n)
{
int i;
long int ans=1;
for(i=0;i<n;i++)
ans = ans*n;
return ans;
}
void main()
{
int i,n;
long int a[25],sum=0;
clrscr();
printf("Enter the no. of terms to find sum of (max 25) : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i] = powfunc(i+1);

}
for(i=0;i<n;i++)
sum = sum + a[i] ;
printf("The sum is %d",sum);
getch();
}
================================================================================
================================================================================
==========================================
// CORRECTION OF TRANSPOSE WALLA PROGRAM
// APPARENTLY , WE HAVE TO DO IT USING A POINTER
// SO HERE'S THE ONE USING A POINTER ...
#include<stdio.h>
#include<conio.h>
void main()
{
int matrix[5][5],transpose[5][5],i,j,n,m,*p;
p = matrix;
printf("\nEnter the no. of rows in the matrix (max 10): ");
scanf("%d",&n);
printf("\nEnter the no. of columns in the matrix (max 10): ");
scanf("%d",&m);
printf("\nEnter %d elements of matrix from left to right and top to down : ",n*m
);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&matrix[i][j]);
printf("\nThe matrix is : \n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf(" %d \t",matrix[i][j]);
printf("\n");
}
for(i=0;i<n;i++)
for(j=0;j<m;j++)
transpose[j][i]=*(p+i*5+j);
printf("\nThe transpose is : \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf(" %d \t",transpose[i][j]);
printf("\n");
}
getch();
}
================================================================================
================================================================================

=========================================
//The one with 3D Matrix ...
#include<stdio.h>
#include<conio.h>
void main()
{
int matrix[25][5][2],i,j,k,n;
clrscr();
printf("Enter the no. of 5x2 matrices (max 25) : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the 10 elements in the matrix %d : ",i+1);
for(j=0;j<5;j++)
for(k=0;k<2;k++)
scanf("%d",&matrix[i][j][k]);
printf("\n\n");
}
for(i=0;i<n;i++)
{
printf("Enter the elements in the matrix %d are : \n",i+1);
for(j=0;j<5;j++)
{
for(k=0;k<2;k++)
printf("%d \t",matrix[i][j][k]);
printf("\n");
}
printf("\n\n\n");
}
getch();
}
================================================================================
================================================================================
===========
//The one with H.C.P.F and L.C.P.F.
//On Observing her 10 and 50 ka example , i concluded that 2 and 5 are the lowes
t and highest PRIME factors of 10 and 50 .. so this is a program for those who f
eel that is what she meant ...
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,h,l=0,i;
printf("Enter two numbers : ");
scanf("%d%d",&a,&b);
for(i=2;i<=a&&i<=b;i++)
{
if(a%i==0&&b%i==0)
{
if(l==0)l=i;

h=i;
a=a/i;
b=b/i;
i=1;
}
}
printf("Highest common prime factor = %d\n Lowest common prime factor = %d",h,l)
;
getch();
}
================================================================================
================================================================================
===================
//The one with the series x + x^2/2! + ....
//I dont know exactly what output she expects , on asking she gave an evil grin
and said "Tum Karo ... ", so this is the nearest i can get ....
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("Enter the no. of terms to print : ");
scanf("%d",&n);
printf("\n\n x");
for(i=2;i<=n;i++)
{
printf(" + (x^%d)/(%d!)",i,i);
}
getch();
}
================================================================================
================================================================================
====================

Vous aimerez peut-être aussi