Showing posts with label print. Show all posts
Showing posts with label print. Show all posts
Monday, February 16, 2015
C Program to Print following series 1 4 7 10 40
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,a=-1,b;
for(i=1;i<=40;i+=3)
{
a*=-1;
b=i;
b*=a;
cout<<b<<" ";
}
getch();
}
C Program to print table of any number
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n;
cout<<"Enter a ny number:";
cin>>n;
cout<<"
";
for(i=1;i<=10;++i)
cout<<" "<<n<<"*"<<i<<"="<<n*i<<"
";
getch();
}
Saturday, February 14, 2015
C Program to Print an Alphabet from A to L of s in Capital Letter

#include<iostream>
using namespace std;
int main()
{
int i,j,k=1;
char ch;
cout<<"Input an Alphabet in capital letters that you want to print:";
cin>>ch;
cout<<"
";
switch(ch)
{
case A:
cout<<" ";
for(i=1;i<=40;++i)
{
for(j=0;j<=22;++j)
{
if(i==1||i==2||i==21||i==20)
cout<<"*";
else
{
if(j==0||j==20)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
case B:
cout<<" ";
while(k<=2)
{
for(i=1;i<=9;++i)
{
for(j=0;j<=i;++j)
{
if(j==0||j==i)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
}
for(i=1;i<=10;++i)
{
if(i==1||i==10)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
for(i=1;i<=10;++i)
{
if(i==1||i==10)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
for(i=9;i>=1;--i)
{
for(j=0;j<=i;++j)
{
if(j==0||j==i)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
}
++k;
}
break;
caseC:
cout<<" ";
for(i=1;i<=40;++i)
{
for(j=0;j<=22;++j)
{
if(i==1||i==2||i==39||i==40)
cout<<"*";
else
{
if(j==0)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
caseD:
cout<<" ";
for(i=1;i<=18;++i)
{
for(j=0;j<=i;++j)
{
if(j==0||j==i)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
}
while(k<=4)
{
for(i=1;i<=19;++i)
{
if(i==1||i==19)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
++k;
}
for(i=18;i>=1;--i)
{
for(j=0;j<=i;++j)
{
if(j==0||j==i)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
}
break;
caseE:
cout<<" ";
for(i=1;i<=39;++i)
{
for(j=1;j<=20;++j)
{
if(i==1||i==2||i==20||i==21||i==38||i==39)
cout<<"*";
else
{
if(j==1)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
caseF:
cout<<" ";
for(i=1;i<=40;++i)
{
for(j=1;j<=20;++j)
{
if(i==1||i==2||i==18||i==19)
cout<<"*";
else
{
if(j==1)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
caseG:
cout<<" ";
for(i=1;i<=25;++i)
{
for(j=1;j<=20;++j)
{
if(i==1||i==2)
cout<<"*";
else
{
if(j==1)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
for(i=1;i<=10;++i)
{
for(j=1;j<=20;++j)
{
if(i==1||i==2)
{ if(j==1||j==14||j==15||j==16)
cout<<"**";
else
cout<<" ";
}
else
{
if(i==9||i==10)
cout<<"*";
else
{ if(j==1||j==18)
cout<<"**";
else
cout<<" ";
}
}
}
cout<<"
";
}
break;
caseH:
cout<<" ";
for(i=1;i<=40;++i)
{
for(j=1;j<=21;++j)
{
if(i==20||i==21)
cout<<"*";
else
{
if(j==1||j==19)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
caseI:
cout<<" ";
for(i=1;i<=40;++i)
{
for(j=1;j<=30;++j)
{
if(i==1||i==2||i==39||i==40)
cout<<"*";
else
{
if(j==15)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
caseJ:
cout<<" ";
for(i=1;i<=30;++i)
{
for(j=1;j<=28;++j)
{
if(i==1||i==2)
cout<<"*";
else
{
if(j==14)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
for(i=1;i<=10;++i)
{
for(j=1;j<=15;++j)
{
if(i==10||i==9)
cout<<"*";
else
{
if(j==1||j==13)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
caseK:
cout<<" ";
for(i=20;i>=1;--i)
{
for(j=0;j<=i;++j)
{
if(j==0||j==i)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
}
for(i=1;i<=20;++i)
{
for(j=0;j<=i;++j)
{
if(j==0||j==i)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
}
break;
caseL:
cout<<" ";
for(i=1;i<=40;++i)
{
for(j=1;j<=25;++j)
{
if(i==39||i==40)
cout<<"*";
else
{
if(j==1)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
}
return 0;
}
Thursday, February 12, 2015
C Program to print first 10 Prime numbers
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,count=1,b=0;
cout<<"First Ten Prime Numbers Are
"<<"2";
for(i=3;i>0;++i)
{
for(j=2;j<=i;++j)
{
if(i%j==0)
b=1;
break;
}
if(b==0)
{
cout<<"
"<<i;
count++;
}
b=0;
if(count==10)
break;
}
getch();
}
Wednesday, February 11, 2015
C program to print table of a given number
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr(); //to clear the screen
printf("Enter any number:");
scanf("%d",&n);
printf("Table of %d is:
",n);
for(i=1;i<=10;++i)
printf("
%d*%d=%d",n,i,n*i);
getch(); //to stop the screen
}
C Program to calculate and print the sum of even and odd integers of the first n natural numbers
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,sumeven=0,sumodd=0;
cout<<"Enter value of n:";
cin>>n;
for(i=1;i<=n;++i)
{
if(i%2==0)
sumeven+=i;
else
sumodd+=i;
}
cout<<"
Sum of even Numbers is "<<sumeven;
cout<<"
SUm of odd Numbers is "<<sumodd;
getch();
}
#include<conio.h>
void main()
{
clrscr();
int n,i,sumeven=0,sumodd=0;
cout<<"Enter value of n:";
cin>>n;
for(i=1;i<=n;++i)
{
if(i%2==0)
sumeven+=i;
else
sumodd+=i;
}
cout<<"
Sum of even Numbers is "<<sumeven;
cout<<"
SUm of odd Numbers is "<<sumodd;
getch();
}
Tuesday, February 10, 2015
C Program to print fibonacci series
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
unsigned long n,first,second,third;
first=0;
second=1;
cout<<"How many numbers?";
cin>>n;
cout<<"Fabonacci series
"<<first<<" "<<second;
for(int i=2;i<n;++i)
{
third=first+second;
cout<<" "<<third;
first=second;
second=third;
}
getch();
}
C Program to print its own Source Code as Output
We can print the source code of a program as an output using quine. So first lets understand what quine is?
What is a Quine?
A quine is a computer program which takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are "self-replicating programs", "self-reproducing programs", and "self-copying programs".
Also Read: List of Biggest and Popular Programming Contests
Also Read: How to Swap Two Numbers Without Using Temporary Variable or Arithmetic Operators?
Quines are named after the American mathematician and logician Willard Van Orman Quine (1908–2000). The interesting thing about quine is that we are not allowed to use open and then print file of the program.
An example of a quine in C language is given below which produces the source code as an output. It can be written in any other language like java, python, etc.
main(a){printf(a="main(a){printf(a=%c%s%c,34,a,34);}",34,a,34);}
If you will compile and run above code then it will show following output

This program can result in undefined behavior as we have used printf function without including its header file. Also, the return type declaration for main has been left off to reduce the length of the program. Two 34s are used to print double quotes around the string s.
Source: http://www.geeksforgeeks.org/quine-a-self-reproducing-program/and http://en.wikipedia.org/wiki/Quine_(computing)
Subscribe to:
Posts (Atom)