Skip to main content

C Language Programs for beginners-1

Control Statements programs:
1 Program to Find Largest of Three Numbers
#include <stdio.h>
#include<conio.h>

void main()
{
int a, b, c;

clrscr();
printf("\nEnter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a>b && a>c)
printf("\n\n%d is greater", a);
else if (b>a && b>c)
printf("\n\n%d is greater", b);
else
printf("\n\n%d is greater", c);
getch();
}
2 Program to Check Whether a Character is Vowel or not by using switch Statement
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;

clrscr();
printf("\nEnter any character: ");
scanf("%c", &ch);
switch (ch)
{
case 'a':
case 'A':
printf("\n\n%c is a vowel", ch);
break;
case 'e':
case 'E':
printf("\n\n%c is a vowel", ch);
break;
case 'i':
case 'I':
printf("\n\n%c is a vowel", ch);
break;
case 'o':
case 'O':
printf("\n\n%c is a vowel", ch);
break;
case 'u':
case 'U':
printf("\n\n%c is a vowel", ch);
break;
default:
printf("\n\n%c is not a vowel", ch);
}
getch();
}
3 Program to Find the Sum of First 100 Positive Integers
#include<conio.h>
#include<stdio.h>
void main()
{
int i, sum=0;
clrscr();
printf("\n\tSum of first 100 positive numbers\n");
for(i=0; i<=100; i++)
sum = sum + i;
printf("\nSum = %d", sum);
getch();
}
4 Program to Find the Sum of Even and Odd Numbers from First 100 Positive Integers
#include<conio.h>
#include<stdio.h>
void main()
{
int i, sumEven=0, sumOdd=0;

clrscr();
for (i=0; i<=100; i++)
if (i%2 == 0)
sumEven = sumEven + i;
else
sumOdd = sumOdd + i;
printf("\nSum of first even 100 numbers: %d\n", sumEven);
printf("\nSum of first odd 100 numbers: %d\n", sumOdd);
getch();
}
5 Program to Find the Sum of Digits of a Positive Integer
#include<conio.h>
#include<stdio.h>
void main()
{
long n;
int digit, sum = 0;
clrscr();
printf("\nEnter any number: ");
scanf("%d", &n);
while (n > 0)
{
digit = n%10;
n = n/10;
sum = sum + digit;
}
printf("\n\nSum of digits: %d", sum);
getch();
}
6 Program to Reverse a Given Number
#include<conio.h>
#include<stdio.h>
void main()
{
long n;
int rev;
clrscr();
printf("\nEnter any number: ");
scanf("%ld", &n);
printf("\nReverse no. is:\n\n");
while (n > 0)
{
rev = n % 10;
n = n / 10;
printf("%d", rev);
}
getch();
}
7 Program to Print First N Prime Numbers
#include<conio.h>
#include<stdio.h>

void main()
{
         int i, j, n;

        clrscr();
        printf("\nEnter how many prime numbers you want to print: ");
        scanf("%d", &n);
        printf("\n");
         for (i=2; i<=n; i++){

             for (j=2; j<=i; j++)
            {
                    if(i%j == 0)
                   break;
                   else
                  {
                                 printf("\n%d", i);
                                 break;
                  }
           }

}
getch();
}
8 Program to Print a Table of any Number
#include<conio.h>
#include<stdio.h>

void main()
{
int n, mul, i;

clrscr();
printf("\nEnter any no.: ");
scanf("%d", &n);
for(i=1; i<=10; i++)
{
mul = n*i;
printf("\n\n%d\tx\t%d\t=\t%d", n, i, mul);
}
getch();
}
9 Program to Check Whether the Given Number is an Armstrong Number
#include<conio.h>
#include<stdio.h>

void main()
{
int n, temp, d, arm=0;

clrscr();
printf("\nEnter any number: ");
scanf("%d", &n);
temp = n;
while (temp > 0)
{
d = temp%10;
temp = temp/10;
arm = arm + (d*d*d);
}
if (arm == n)
printf("\n\n%d is an Armstrong number\n", n);
else
printf("\n\n%d is not an Armstrong number\n", n);
getch();
}
10 Program to Print the Numbers Which are Divisible by 3 and 5 from First 100 Natural Numbers
#include<conio.h>
#include<stdio.h>

void main()
{
int i;

clrscr();
printf("\nFirst 100 numbers which are divisible by 3 and
5\n\n");
for (i=1; i<=100; i++)
if (i%3==0 && i%5==0)
printf("\t%d", i);
getch();
}
11 Program to Find Whether a Number is Palindrome or Not
#include<conio.h>
#include<stdio.h>

void main()
{
int n, pal=0, temp, x;

clrscr();
printf("\nEnter any number: ");
scanf("%d", &n);
temp = n;
while (temp > 0)
{
x = temp%10;
temp = temp/10;
pal = pal*10 + x;
}
if (pal == n)
printf("\n%d is palindrome", n);
else
printf("\n%d is not palindrome", n);
getch();
}
12 Program to Find Factorial of a Number without using Recursion
#include<conio.h>
#include<stdio.h>

void main()
{
int n, i;
long fact=1;

clrscr();
printf("\nEnter any number: ");
scanf("%d", &n);
for (i=1; i<=n; i++)
fact = fact*i;
printf("\nFactorial = %ld", fact);
getch();
}
13 Program to Find Factorial of a Number using Recursion
#include<conio.h>
#include<stdio.h>
int fact(int num)
{
        int f;
        if(num==1)
        return 1;
        else
        f=num*fact(num-1);
        return f;
}
void main()
{
      int num,res;
      clrscr();
      printf(“\nEnter any number:”);
      scanf(“%d”,&num);
      res=fact(num);
      printf(“\nFatorial=%d”,res);
      getch();
     }

14 Program to Print Fibonacci Series without Recursion
#include<conio.h>
#include<stdio.h>

void main()
{
int x, y, z, n, i;
clrscr();

x=0; y=1;
printf("\nEnter value of n: ");
scanf("%d", &n);
printf("\nFibonacci Series:\n\n");
printf("\n%d", x);
printf("\t%d", y);
for( i=0; i<n-2; i++)
{
z = x+y;
printf("\t%d", z);
x = y;
y = z;
}
getch();
}
15 Program to Print Fibonacci Series using Recursion

#include<conio.h>
#include<stdio.h>
int fibbo(int, int, int, int);
void main()
{
int n, f, x=0, y=1, i=3;
clrscr();
printf("\nEnter value of n: ");
scanf("%d", &n);
printf("\n%d\t%d", x, y);
fibbo(x, y, n, i);
getch();
}
fibbo(int x, int y, int n, int i)
{
int z;
if (i <= n)
{
z = x + y;
printf("\t%d", z);
x = y;
y = z;
i++;
fibbo(x,y,n,i);
}
}
16 Program to Find Value of sinx using Expansion Series given below:
sin(x) = x - x3/3 + x5/5 - x7/7......

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
float base, pwr, sum, c=1, m=2, i=3, g, h;

clrscr();
printf("\nEnter the base value: ");
scanf("%f", &base);
printf("\nEnter the power value: ");
scanf("%f", &pwr);
sum = base;
ab:
m = m * i;
h = pow(-1, c);
g = pow(base, i);
sum = sum + (h * g) / m;
i = i + 2;
c++;
m = m * (i - 1);
if (i <= pwr)
goto ab;
printf("\n\nSum = %f", sum);
getch();
}
17 Program to Print the Pattern:
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321

#include<conio.h>
#include<stdio.h>
void main()
{
int i, j, k, space, n=9;

clrscr();
for (i=1; i<=n; i++)
{
for (j=1; j<=n-i; j++)
putchar(' ');
for (j=1,k=2*i-1; j<=2*i-1; j++,k--)
{
if (j <= k)
printf("%d", j);
else
printf("%d", k);
}
putchar('\n');
}
getch();
}

Comments

Popular posts from this blog

WAP to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for upto 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls.

  #include<iostream.h> #include<conio.h> void main() { int calls; float bill; cout<<" Enter number of calls : "; cin>>calls; if (calls<=100) bill=200; else if (calls>100 && calls<=150) { calls=calls-100; bill=200+(0.60*calls); } else if (calls>150 && calls<=200) { calls=calls-150; bill=200+(0.60*50)+(0.50*calls); } else { calls=calls-200; bill=200+(0.60*50)+(0.50*50)+(0.40*calls); } cout<<" Your bill is Rs. "<<bill; getch(); }   Output: Enter number of calls : 190 Your bill is Rs.250

Write a program to calculate the total expenses. Quantity and price per item are input by the user and discount of 10% is offered if the expense is more than 7000.

  #include<iostream.h> #include<conio.h> void main() { int totalexp, qty, price, discount; cout<<" Enter quantity: "; cin>>qty; cout<<" Enter price: "; cin>>price; totalexp=qty*price; if (totalexp>7000) { discount=(totalexp*0.1); totalexp=totalexp-discount; } cout<<" Total Expense is Rs. "<<totalexp; getch(); }