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

Tuesday, November 29, 2011

A C++ program to find out the Prime Factors


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int fact[20],n,num,c=0,j,k;
cout<<"enter the number: ";
cin>>num;
for(n=1;n<=num/2;n++)
{
if(num%n==0)
{
fact[c]=n;
c++;
}
}
cout<<"The Prime Factores Are As Follow"<<endl;
for(n=0;n<c;n++)
{
k=0;
for(j=1;j<=fact[n];j++)
{
if(fact[n]%j==0)
k++;
}
if(k==2)
cout<<fact[n]<<endl;
}
getch();
}

Note:-
            This program is only for few peoples so don't......................... :-)

Monday, October 17, 2011

How to print spaces using nested loop in C++

C++ Program:-



#include<iostream.h>
#include<conio.h>
void main(void)
{
clrscr();
for(int num1=1;num1<=10;num1++)
{
for(int num2=1;num2<=num1;num2++)
{
cout<<" ";
}
cout<<num1<<endl;
}

getch();
}

OUTPUT:-

 1
  2
   3
    4
     5
      6
       7
        8
         9
          10

NOTE:-
            In this program as you can see number of spaces in each line is increasing according to the number. lets discus about the logic of this program.
As you can see we use the first loop i.e  for(int num1=1;num1<=10;num1++) In this loop num1 is starting from 1 and ending at point 10 with the increment of 1. We use loop for following three purposes.
  1. For the printing the value of num1 i.e 1 ,2 ,3, 4.............. 10 . For this purpose we can simply use this statement Cout<<num1; so this line will simply print the number that will save to num1 each time. 
  2. For printing new lines 10 time.This can be done using this statement  Cout<<endl; so this line will always take the cursor to the new line with each value of loop.
  3. For printing the spaces that are increasing according to the number means 5 spaces for number 5 and similarly 10 spaces for number 10. This operation is done by using this nested loop.      for(int num2=1;num2<=num1;num2++)
    {
    cout<<" ";
    }                                       This loop is depending on the value of upper loop mean if the value of num1 is 5 then this loop will run 5 time because it is less than equal to num1.

Saturday, October 15, 2011

A C++ program that use nested loop to print lines 1 to 50

C++ Program :-



#include<iostream.h>
#include<conio.h>
void main(void)
{
clrscr();
int num1,num2,num3=1;
for(num1=1;num1<=5;num1++)
{
cout<<endl;
for(num2=num3;num2<num3+10;num2++)
{
cout<<"  "<<num2;
}


num3+=10;
}
getch();
}

OUTPUT:-

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15..... 20
21..... ....................30
31...........................40
41...........................50

A C++ program that use nested loop to print numbers

C++ Program :-



#include<iostream.h>
#include<conio.h>
void main(void)
{
clrscr();
int num1,num2;
for(num1=1;num1<=5;num1++)
{
for(num2=5;num2>=num1;num2--)
{
 cout<<num2;

}
cout<<endl;
}

getch();
}

OUTPUT :-

                The output of this program will be as following.....
54321
5432
543
54
5

Note:- 
        you can make any changes in the output according to your output requirements by simply changing the values in the loop.

Thursday, October 13, 2011

Assignment for BS(CS)-1 & BS(TN)-2

Assignment # 4
Note:- This is very important assignment follow the following instructions carefully.
  • In Copy/ paste case both students will be assigned zero mark in assignment as well as in lab-sectional. 
  • Accurate and neat assignment will be considered in lab-sectional marking.
  • The assignment will be checked on your next lab during lab-sectional.
  • After lab, no assignment will be acceptable.
  • All these programs should must be made in Turbo-c don't make it in Ms-word.
  • Questions may be asked about your assignment in lab- Viva about your assignment so make sure you know what you make.



Q#1. Make a program that can print the following output with the help of Nested for Loop.


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


Q#2.Make a program that uses loops to convert temperature from centigrade to Fahrenheit from 1 to 20 and the output should be in the following way.

Fahrenheit              Centigrade
1                      .........
2                      .........

Q#3.Use the Nested loop concept to print the following output.


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

Q#4.Make a program using nested loops that print the following the output.

10
99
888
7777
66666
555555
4444444
33333333
222222222
1111111111

Q#5. Make a program using the concept of nested loop that will print the following output.

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


Best of Luck!

The Concept of nested loop in C++

Nested Loop :-
                        A nested loop means a loop inside a loop doesn't matter which type of loop you are using but if you use a loop inside a loop then that loop will be called nested loop.
For Example..

#include<iostream.h>
#include<conio.h>
void main(void)
{
clrscr();
for(int num=1;num<=10;num++)
             {
              for(int num2=1;num2<=10;num2++)
                            {


                             cout<<"\nALLAH";
                             }

            }
 getch();
}


NOTE:-
              This program will print "Allah"   (10 X 10) 100 times because as you can see for each value of first loop the next loop will execute ten times so in this way "Allah" will be printed 100 times.

Thursday, July 28, 2011

A C program that can convert Fahrenheit to degree Celsius using while loop


This is the program that will print and convert from 1 to 10 Fahrenheit to degree Celsius using the "WHILE LOOP" ,output will be the simple.

C Program:-

#include<stdio.h>
#include<conio.h>
void main(void)
{
int farinhiet=1;
float degree;
clrscr();
while(farinhiet<=10)
{
degree=(farinhiet-32)*5/9;
printf("\n\nfarinhiet=%d          degree=%f  ",farinhiet,degree);
farinhiet++;
}
getch();
}

A C program that can find a table using While loop

As we have done the same program of a table using the "FOR LOOP" but we can also make that program using the "WHILE LOOP" it is also very easy and simple and the output of this program is same like we did in the "FOR LOOP" table program so I will not mention the output.

C Program:-


#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int tbl,count=1;
printf("Enter the number which you want to print the table\n\n");
scanf("%d",&tbl);
while(count<=20)
{
printf("\n\n%2d   X   %2d= %3d",tbl,count,tbl*count);
count++;
}
getch();
}

Tuesday, July 26, 2011

Introduction to "WHILE LOOP" in C and C++ programming language

While Loop in C and C++:-
                                                                     "While loop" is a very important  loop in C and C++ programming language it is very similar to "FOR LOOP" the only difference between FOR and WHILE loop is that, that In while loop we do not know the number of repetitions it depends on any type of condition but for loop do not depend on any type of condition a user is aware in advance of the numbers of the repetitions.
The syntax of "While Loop" is given below.

while(condition)
      {

       Body of While loop(block of statements);  

      }

The program related to "While Loop" will be given in the upcoming posts.

Monday, July 25, 2011

A C program that print 1 to 50 by FOR loop

This is a program in which we use the concept of "Nested FOR LOOP" for printing the numbers from 1 to 50 and 10 digits in each line.
C Program:-



#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int n,m;
for(n=1;n<=50;n+=10)
{
printf("\n\n");
for(m=n;m<n+10;m++)
printf("%d\t",m);
}
getch();
}

Output:-
                     This program uses the concept of nested for loop and nested means loop inside a loop now in that type of case first it takes 1 value of upper loop and then execute the whole loop inside its body then again return to take the another value so in that way numbers from 1 to 50 will be printed and and after 10 digits automatically comes to new line.

Sunday, July 24, 2011

A C program that Is a LOVE CALCULATOR

C Program:-



#include<stdio.h>
#include<conio.h>
void main(void)
{


clrscr();
int n,m;
char name1,name2,name3,name4,name5,name6,name7,name8,name9,name10,name11,name12,name13,name14,name15;
int avg;
clrscr();
printf("Enter your name and then press enter and enter your partener name\n\n");
scanf("%c",&name1);
scanf("%c",&name2);
scanf("%c",&name3);
scanf("%c",&name4);
scanf("%c",&name5);
scanf("%c",&name6);
scanf("%c",&name7);
scanf("%c",&name8);
scanf("%c",&name9);
scanf("%c",&name10);
scanf("%c",&name11);
scanf("%c",&name12);
scanf("%c",&name13);
scanf("%c",&name14);
scanf("%c",&name15);
avg=(name1+name2+name3+name4+name5+name6+name7+name8+name9+name10+name11+name12+name13+name14+name15)/15;
if((avg%2)==0)
avg=avg-39;
if((avg%7)==0)
avg=avg-18;
if((avg%5)==0&&avg<56)
avg=avg+45;
clrscr();
printf("\n\nYour love percentage is %d",avg);
if(avg<40)
printf("\n\nTry to Increase your love toooooooooo bad......");
if(avg>=40&&avg<=60)
printf("\n\nHmmmmm well your love is soo soo improve it.........");
if(avg>60&&avg<=90)
printf("\n\nCongtars....V.good...keep it up");
if(avg>90&&avg<=99)
printf("\n\nWOW GREAT..........Fantastic........your next goal is to acchive full 100.......ok");
if(avg==100)
printf("\n\nPERFECT Made for each other true love");
getch();

}

NOTE:-
                    This program is very interesting program just copy paste this program in turbo C and lets see what happen................. :-) 

A C program that Will print the number.

This is the C language Program that will take the input a number form the user and then print the natural numbers up to that number, you can make it in many ways time we use FOR LOOP it is also very simple method.

C Program:-



#include<stdio.h>
#include<conio.h>
void main(void)
{
int nmr,n;
clrscr();
printf("Enter the number");
scanf("%d",&nmr);
for(n=0;n<nmr;n++)
printf("%d\t",nmr);
getch();
}


Output:-
                   Enter the number=5
                   1     2     3     4     5


NOTE:-
          I do use parentheses in the for loop that is because when we have only one statement inside the body of for loop then there is no need to use parentheses.

Saturday, July 23, 2011

A C program that find sum from 1 to 10

This is the program which uses the help of "For loop" to find the sum of the numbers from 1 to 10.

C Program:-



#include<stdio.h>

#include<conio.h>
void main(void)
{
clrscr();
int n,sum;
for(n=1;n<10;n++)
sum+=n;
printf("\n\nThe total from 1 to 10 is %d",sum);
getch();
}

Same Program in another Way:-

#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int n,sum=0;
for(n=1;n<10;n++)
{
sum=sum+n;
printf("\n\ncount= %d  total= %d  ",n,sum);
}
printf("\n\nThe total from 1 to 10 is %d",sum);
getch();
}

Same program with setting the column width:-


#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int count=1,sum=0;
while(count<=10)
{
sum+=count;
printf("\n\n count=%2d    sum= %2d",count,sum);
count++;
}
getch();
}

Note:-
              In this program we set the column width by "%2d" this with set the column with two digit place.

Friday, July 22, 2011

An Example of "FOR LOOP" in C and C++

As sometimes in a program we need to print same thing over and over or like ten times for example we can make a program that will a line ten times with the help of FOR LOOP.


C Program:-
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int num;
for(num=1;num<=10;num++)
                                {
                                 printf("\n This is a C language program") ;
                                 }
getch();
}


Output:-
                      This is a C language program
                      This is a C language program
                      This is a C language program
                      This is a C language program
                      This is a C language program 
                      This is a C language program 
                      This is a C language program
                      This is a C language program
                      This is a C language program 
                      This is a C language program


NOTE:-
             As you can see in this program in the for loop "num=1" is the starting value and "num<=10" is the ending value and "num++" means the increment of 1 as in recent posts i have explained you the increment and decrements operators in C and C++ programming language.

Introduction to "FOR LOOP" in C and C++ programming language

The general form of the "For loop" statement is

for(exp1;exp2;exp3)
                          {
                           
                           The body of loop (block of statements)


                          } 
It is executed as follow :
  1. Exp1 is evaluated.
  2. Exp2 is evaluated.If it is true (non zero), the body of the for loop is executed, followed by exp3 and this step(2) is repeated. If exp2 is false (zero), the loop terminates and the execution continues with the next statements if any after the for loop.

Thursday, July 21, 2011

Introduction to "LOOPS" in C and C++ language

A loop is a mechanism that enables you to execute the same sequence of the statements repeatedly until a particular condition is met. The statements inside the loop is sometimes called iteration statements.

The loops can be of three types.
  1. sequential
  2. repetition
  3. section
For example you might want to.

  1. Execute a loop a given number of times.
  2. Execute the loop until a given value exceeds another value.
  3. Execute a loop until a particular character is entered.
There are Generally three types of loops in C and C++ programming language.
  1. FOR LOOP
  2. WHILE LOOP
  3. DO WHILE LOOP
We use all these loops to save the time and efforts of the programmer because other wise if we want to write something ten times in the C or C++ program than we have to write it ten times but with the help of loop we will only have to write it once and apply the loop and rest of the work will be done automatically.





















A C program that can calculate gross salary

This is the program to find out the gross income of an employee by taking input of his different data and making the formula for finding it.

C Program:-



#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int b_salery,inc,allowence,bonus;
printf("enter your basic salery=\n");
scanf("%d",&b_salery);
printf("enter your bonus=/n");
scanf("%d",&bonus);
printf("enter yourincrement=/n");
scanf("%d",&inc);
printf("enter your allowence=/n");
scanf("%d",&allowence);
b_salery=b_salery+inc+allowence+bonus;
printf("Your gross salery = %d",b_salery);
getch();
}

NOTE:-
            As the output of this program is completely understandable as i mentioned in a lot of recent posts so i will not show the output.just try it on your compiler and see what happen.

A C program that can find square

This is a program that takes a number as input from the user and then prints its square.

C program:-



#include<stdio.h>
#include<conio.h>
void main(void)
{
//*program that can calculate square of a number
int nmbr,sqr;
clrscr();
printf("Enter the number which you want the square=");
scanf("%d",&nmbr);
sqr=nmbr*nmbr;
printf("\n The square of %d is %d\n",nmbr,sqr);
getch();
}

Output:-
                    Enter the number which you want the square= 3
                    The square of 3 is 9

Second Method:-


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
//*program that can calculate square of a number
int nmbr,sqr;
clrscr();
printf("Enter the number which you want the square\n");
scanf("%d",&nmbr);
sqr=pow(nmbr,2);
printf("the square of %d is %d\n",nmbr,sqr);
getch();
}

NOTE:-
           In this program we use a built in math function "pow" and its syntax is pow(number,exponent); and for using this function we must have to use #include<math.h>

A C program that can find CGPA

This is the program to make a formula to find the CGPA of four semester. This program will takes input GPA of four semester and then find the CGPA and print it on the screen.

C Program:




#include<stdio.h>
#include<conio.h>
void main(void)
{
//*program that can calculate cgpa of four smesters
float gpa1,gpa2,gpa3,gpa4,cgpa;
clrscr();
printf("Enter your first gpa=");
scanf("%f",&gpa1);
printf("\nEnter your 2nd gpa=");
scanf("%f",&gpa2);
printf("\nEnter your 3rd gpa=");
scanf("%f",gpa3);
printf("\nEnter your 4rth gpa=");
scanf("%f",&gpa4);
cgpa=(gpa1+gpa2+gpa3+gpa4)/4;
printf("\nYour CGPA is %f",cgpa);
getch();
}


Output:-
                   Enter your first gpa = 3.82
                   Enter your 2nd gpa = 4
                   Enter your 3rd gpa= 3.9
                   Enter your 4th gpa= 4
                   
                   Your CGPA is 3.93

C Programs for printing integers,floating points,character and strings

These are the samples program to understand how to print the integers, floating points , characters and variables......
C language program to print strings.

#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("my name is %s and i am a student of %s","ammarafzalkhan","BSCS");
getch();
}

C program to print string and integers.

#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("I am a student of class %s and my roll number is %d ","BSCS",014);
getch();
}

OR



#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("%s is %d million miles\nfrom the sun.", " venus",67);
getch();
}


C language program to print floating points.
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("the value of pye is %f ",3.14567);
getch();
}

C language program to print set column floating points.

#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("the value of pye is %.2f ",3.14567);  //this will print only two digits after decimal point
getch();
}

C language program to print two strings.
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("my name is %s\n i am a student of %s","ammarafzalkhan","BSCS");
getch();
}

C language program to print character and strings.
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("the letter %c is",'j');
printf("pronounced %s","jay");
getch();
}