Program to Reverse an Integer

#include<stdio.h>
// Reverse an Integer
int main()
{
int num,rev =0, r;

printf(“Enter any number “);
scanf(“%d”,& num);

while(num>0)
{
r = num % 10;
rev = rev * 10 + r;
num = num /10;

}
printf(“\n\nReverse = %d”, rev);
return 0;

}

Fibonacci Series up to n number of terms

#include <stdio.h>
int main()
{
int i, n, n1 = 0, n2 = 1, sum = 0;
printf(“Enter the number of terms: “);
scanf(“%d”,&n);
printf(“\n\nFibonacci Series: “);
printf(“%d\t%d\t”,n1,n2);
for (i = 1; i <= n; i++)
{
sum = n1+ n2;
printf(“%d\t”,sum);
n1= n2;
n2= sum;
}

return 0;
}

Factorial of a Number

#include<stdio.h>
//factorial
int main()
{

int n, fact = 1,i;
//for
printf(“Using FOR Loop”);
scanf(“%d”,&n);
if(n<0)
{
printf(“invalid number”);
}
else{
for(i=1;i<=n;i++)
fact *= i;
printf(“\n\nFactorial = %d\n\n”, fact);

}
return 0;

}

Program to Sum the Inputs Until User Enters a Positive Integer

#include<stdio.h>

// Program to Sum the Inputs Until User Enters a Positive Integer

int main()
{
int n=0, sum=0;

// for
printf(“Using For loop\n”);
printf(“Enter any positive number “);
for(;n>=0;)
{
sum += n;
scanf(“%d”,&n);
}
printf(“Sum = %d”, sum);
//while
printf(“\n\nUsing While loop\n”);
printf(“Enter any positive number “);
n=0;
sum=0;

while(n>=0)
{
sum += n;
scanf(“%d”,&n);
}
printf(“Sum = %d”, sum);
}

return 0;}

Sum of numbers entered by a user(using for and while)

#include<stdio.h>
// Sum of nums entered by a user
int main()
{
int n, num, sum=0,i;
printf(“Enter how many numbers you want to enter “);
scanf(“%d”,&n);

printf(“Using for loop\n”);
for(i=1;i<=n;i++)
{
printf(“Enter the number “);
scanf(“%d”,&num);
sum= sum + num;
}

printf(“\nSum of %d numbers is %d\n\n”,n,sum);

printf(“Using while loop\n”);
i= 1;
while(i<=n)
{
printf(“Enter the number “);
scanf(“%d”,&num);
sum= sum + num;
i++; // n+=1
}

printf(“Sum of % numbers is %d”,n,sum);

return 0;
}

Pyramid and Pattern

1)

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

#include<stdio.h>
int main(){
int i, j,rows;
printf(“Enter number of rows: “);
scanf(“%d”,&rows);
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf(“*”);
}
printf(“\n”);
}
return 0;
}

2)

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

3)

A
A B
A B C
A B C D
A B C D E

#include<stdio.h>
int main(){
int i, j,rows;
printf(“Enter number of rows: “);
scanf(“%d”,&rows);
for(i=0;i<rows;i++)
{
for(j=0;j<=i;j++)
{
printf(“%c”,(‘A’+j));
}
printf(“\n”);
}
return 0;
}

4)

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

#include<stdio.h>
int main(){
int i, j,rows;
printf(“Enter number of rows: “);
scanf(“%d”,&rows);
for(i=1;i<=rows;i++)
{
for(j=i;j<=rows;j++)
{
printf(“*”);
}
printf(“\n”);
}
return 0;
}

5.

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 number of rows: “);
scanf(“%d”,&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=rows-i+1;j++)
{
printf(“%d”,j);
}
printf(“\n”);
}
return 0;
}

int and char

The char keyword declares a character variable. Variables of type char are 1 byte in length. They can be signed or unsigned, which means they have a range of -128 to 127 and 0 to 255, respectively.  For example:

char alphabet;

Variables of type int are one machine-type word in length. They can be signed (default) or unsigned, which means that in this configuration of the compiler they have by default a range of -32768 to 32767 and 0 to 65535 respectively

int a, b, c;                       // ‘a’, ‘b’, ‘c’ are integer variables